Skip to content

Commit

Permalink
🩹 fix: Correct split implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Nov 13, 2022
1 parent 205076d commit f6710fd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import _split from './_split.js';
* @param {Node} z Node to split at.
*/
export default function split(x, z) {
assert(x instanceof Node);
assert(z instanceof Node);
if (x === z) {
return [null, x];
}

assert(x instanceof Node);
if (z === null) {
return [x, null];
}

assert(z instanceof Node);
_split(z);
return [x, z];
}
47 changes: 47 additions & 0 deletions test/src/split.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import test from 'ava';

import {list} from '@iterable-iterator/list';

import {str} from './_fixtures.js';

import {from, values, split} from '#module';

const macro = test.macro({
exec(t, sequence, i) {
const x = from(sequence);
let y = x;
for (let k = 0; k < i; ++k) y = y.next;
const [s1, s2] = split(x, y);
if (x === y) {
t.is(s1, null);
t.is(s2, x);
} else {
t.is(s1, x);
t.is(s2, y);
}

const left = list(values(s1)).join('');
const right = list(values(s2)).join('');
const expected = [sequence.slice(0, i), sequence.slice(i)];
t.deepEqual([left, right], expected);
},
title(title, sequence, i) {
return title ?? `split(${str(sequence)}, ${i})`;
},
});

test(macro, '', 0);
test(macro, 'a', 0);
test(macro, 'a', 1);
test(macro, 'ab', 0);
test(macro, 'ab', 1);
test(macro, 'ab', 2);
test(macro, 'abc', 0);
test(macro, 'abc', 1);
test(macro, 'abc', 2);
test(macro, 'abc', 3);
test(macro, 'abcd', 0);
test(macro, 'abcd', 1);
test(macro, 'abcd', 2);
test(macro, 'abcd', 3);
test(macro, 'abcd', 4);

0 comments on commit f6710fd

Please sign in to comment.