Skip to content

Commit

Permalink
✨ feat: Refactor _remove as _erase via generic _concat.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Nov 13, 2022
1 parent d3e6cf9 commit 127b5f8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/_concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Node from './Node.js';
* @param {Node} y First node of second input list.
*/
export default function _concat(z, y) {
assert(z instanceof Node && z.next === null);
assert(y instanceof Node && y.prev === null);
assert(z instanceof Node);
assert(y instanceof Node);
z.next = y;
y.prev = z;
}
14 changes: 14 additions & 0 deletions src/_erase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from 'assert';
import Node from './Node.js';
import _concat from './_concat.js';

/**
* Erase range [x, y) from list. Range cannot be empty.
*
* @param {Node} x Inclusive beginning of range to erase.
* @param {Node} y Exclusive end of range to erase.
*/
export default function _erase(x, y) {
assert(x instanceof Node);
_concat(x.prev, y);
}
4 changes: 2 additions & 2 deletions src/_remove.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import Node from './Node.js';
import _erase from './_erase.js';

/**
* Removes input {@link Node} from its list. Cannot be first or last, use
Expand All @@ -12,6 +13,5 @@ import Node from './Node.js';
*/
export default function _remove(x) {
assert(x instanceof Node);
x.prev.next = x.next;
x.next.prev = x.prev;
_erase(x, x.next);
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {default as Node} from './Node.js';
export {default as _concat} from './_concat.js';
export {default as _erase} from './_erase.js';
export {default as _extend} from './_extend.js';
export {default as _iter} from './_iter.js';
export {default as _iter_fast} from './_iter_fast.js';
Expand Down
11 changes: 11 additions & 0 deletions test/src/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'ava';

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

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

test('_remove', (t) => {
const l = from('abc');
_remove(l.next);
t.is(list(values(l)).join(''), 'ac');
});

0 comments on commit 127b5f8

Please sign in to comment.