1+ var assert = require ( 'assert' ) ;
2+
3+ // Basic case
4+ assert . deepStrictEqual ( 'foo\nbar\nbaz\nrainbow' . split ( / \r ? \n / ) , [ 'foo' , 'bar' , 'baz' , 'rainbow' ] ) ;
5+
6+ // Preserve newlines case
7+ assert . deepStrictEqual ( 'foo\r\nbar\r\nbaz\nrainbow' . split ( / ( \r ? \n ) / ) . reduce ( ( acc , part , index , array ) => {
8+ if ( index % 2 === 0 ) {
9+ acc . push ( part + ( array [ index + 1 ] || "" ) ) ;
10+ }
11+
12+ return acc ;
13+ } , [ ] ) , [ 'foo\r\n' , 'bar\r\n' , 'baz\n' , 'rainbow' ] ) ;
14+
15+ // Empty string
16+ assert . deepStrictEqual ( '' . split ( / \r ? \n / ) , [ '' ] ) ;
17+
18+ // Single line
19+ assert . deepStrictEqual ( 'foo' . split ( / \r ? \n / ) , [ 'foo' ] ) ;
20+
21+ // Only newline characters
22+ assert . deepStrictEqual ( '\n' . split ( / \r ? \n / ) , [ '' , '' ] ) ;
23+ assert . deepStrictEqual ( '\r\n' . split ( / \r ? \n / ) , [ '' , '' ] ) ;
24+
25+ // Newlines at the start and end
26+ assert . deepStrictEqual ( '\nfoo\n' . split ( / \r ? \n / ) , [ '' , 'foo' , '' ] ) ;
27+ assert . deepStrictEqual ( '\r\nfoo\r\n' . split ( / \r ? \n / ) , [ '' , 'foo' , '' ] ) ;
28+
29+ // Mixed newlines
30+ assert . deepStrictEqual ( 'foo\r\nbar\r\nbaz\r\n' . split ( / ( \r ? \n ) / ) . reduce ( ( acc , part , index , array ) => {
31+ if ( index % 2 === 0 ) {
32+ acc . push ( part + ( array [ index + 1 ] || "" ) ) ;
33+ }
34+
35+ return acc ;
36+ } , [ ] ) , [ 'foo\r\n' , 'bar\r\n' , 'baz\r\n' , '' ] ) ;
37+
38+ // Newlines with additional properties
39+ assert . deepStrictEqual ( 'foo\nbar\nbaz\nrainbow' . split ( / ( \r ? \n ) / ) . reduce ( ( acc , part , index , array ) => {
40+ if ( index % 2 === 0 ) {
41+ acc . push ( part + ( array [ index + 1 ] || "" ) ) ;
42+ }
43+
44+ return acc ;
45+ } , [ ] ) , [ 'foo\n' , 'bar\n' , 'baz\n' , 'rainbow' ] ) ;
0 commit comments