Skip to content

Commit 0d3f7cf

Browse files
committed
Added str.splitlines() method (issue #733)
1 parent f59867c commit 0d3f7cf

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

transcrypt/development/automated_tests/transcrypt/module_builtin/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def canonizeString (aString):
66
if __envir__.executor_name == 'transcrypt':
7-
return aString.replace ('\t', '\\t') .replace ('\n', '\\n')
7+
return aString.replace ('\t', '\\t').replace ('\n', '\\n').replace ('\r', '\\r')
88
else:
99
return aString
1010

@@ -135,6 +135,23 @@ def run (autoTester):
135135
'<br>'
136136
)
137137

138+
lines_to_split = ['', '\n',
139+
'abc\n',
140+
'abc\ndef',
141+
'abc\rdef',
142+
'abc\r\ndef',
143+
'\nabc',
144+
'\nabc\n',
145+
'abc\ndef\r\nghi\rjkl\n',
146+
'abc\ndef\n\nghi\njkl'
147+
]
148+
autoTester.check ('<br>splitlines')
149+
for line in lines_to_split:
150+
autoTester.check (
151+
canonizeStringList(line.splitlines ()),
152+
canonizeStringList(line.splitlines (True)),
153+
)
154+
138155
autoTester.check("isalpha",
139156
"".isalpha(),
140157
"123".isalpha(),

transcrypt/modules/org/transcrypt/__builtin__.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,19 @@ String.prototype.py_split = function (sep, maxsplit) { // Combination of genera
17901790
}
17911791
};
17921792

1793+
String.prototype.splitlines = function (keepends) {
1794+
if (this.length === 0) {
1795+
return [];
1796+
}
1797+
1798+
if (keepends === undefined || keepends === null || keepends === false) {
1799+
return this.trimEnd().split(/\r?\n|\r|\n/g);
1800+
}
1801+
else {
1802+
return this.split(/(?<=\n)(?=\n)|(?<=[\r\n])(?=[^\r\n])/g);
1803+
}
1804+
};
1805+
17931806
String.prototype.startswith = function (prefix, start=0, end) {
17941807
if (end === undefined) {end = this.length}
17951808
const str = this.slice(start, end)

0 commit comments

Comments
 (0)