Skip to content

Commit f6710fd

Browse files
🩹 fix: Correct split implementation.
1 parent 205076d commit f6710fd

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/split.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import _split from './_split.js';
99
* @param {Node} z Node to split at.
1010
*/
1111
export default function split(x, z) {
12-
assert(x instanceof Node);
13-
assert(z instanceof Node);
1412
if (x === z) {
1513
return [null, x];
1614
}
1715

16+
assert(x instanceof Node);
17+
if (z === null) {
18+
return [x, null];
19+
}
20+
21+
assert(z instanceof Node);
1822
_split(z);
1923
return [x, z];
2024
}

test/src/split.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import test from 'ava';
2+
3+
import {list} from '@iterable-iterator/list';
4+
5+
import {str} from './_fixtures.js';
6+
7+
import {from, values, split} from '#module';
8+
9+
const macro = test.macro({
10+
exec(t, sequence, i) {
11+
const x = from(sequence);
12+
let y = x;
13+
for (let k = 0; k < i; ++k) y = y.next;
14+
const [s1, s2] = split(x, y);
15+
if (x === y) {
16+
t.is(s1, null);
17+
t.is(s2, x);
18+
} else {
19+
t.is(s1, x);
20+
t.is(s2, y);
21+
}
22+
23+
const left = list(values(s1)).join('');
24+
const right = list(values(s2)).join('');
25+
const expected = [sequence.slice(0, i), sequence.slice(i)];
26+
t.deepEqual([left, right], expected);
27+
},
28+
title(title, sequence, i) {
29+
return title ?? `split(${str(sequence)}, ${i})`;
30+
},
31+
});
32+
33+
test(macro, '', 0);
34+
test(macro, 'a', 0);
35+
test(macro, 'a', 1);
36+
test(macro, 'ab', 0);
37+
test(macro, 'ab', 1);
38+
test(macro, 'ab', 2);
39+
test(macro, 'abc', 0);
40+
test(macro, 'abc', 1);
41+
test(macro, 'abc', 2);
42+
test(macro, 'abc', 3);
43+
test(macro, 'abcd', 0);
44+
test(macro, 'abcd', 1);
45+
test(macro, 'abcd', 2);
46+
test(macro, 'abcd', 3);
47+
test(macro, 'abcd', 4);

0 commit comments

Comments
 (0)