Skip to content

Commit

Permalink
fix: update similar results in python (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
FajarKim committed Jun 28, 2024
1 parent f4c754e commit f954df3
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
* @return {Array<string>} The array of lines, with or without line breaks based on the `keeplinebreaks` parameter.
*/
String.prototype.splitLines = function(keeplinebreaks = false) {
const array = this.split(/(\r?\n|\r|\n)/).reduce((acc, curr, index, array) => {
if (index % 2 === 0) {
acc.push(curr + (array[index + 1] || ""));
}
return acc;
}, []).filter(Boolean);

if (keeplinebreaks === true) {
return this.split(/(\r?\n|\r|\n)/).reduce((acc, curr, index, array) => {
if (index % 2 === 0) {
acc.push(curr + (array[index + 1] || ""));
}
return acc;
}, []).filter(Boolean);
return array;
} else {
return this.split(/\r?\n|\r|\n/).filter(Boolean);
return array.map(line => line.replace(/\r?\n|\r|\n/g, ""));
}
}

0 comments on commit f954df3

Please sign in to comment.