From 127b5f89130ca4b7653473eb0ae17cc1dd207d91 Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Sun, 13 Nov 2022 18:55:09 +0100 Subject: [PATCH] :sparkles: feat: Refactor _remove as _erase via generic _concat. --- src/_concat.js | 4 ++-- src/_erase.js | 14 ++++++++++++++ src/_remove.js | 4 ++-- src/index.js | 1 + test/src/remove.js | 11 +++++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/_erase.js create mode 100644 test/src/remove.js diff --git a/src/_concat.js b/src/_concat.js index a86cf7c..ca14d4a 100644 --- a/src/_concat.js +++ b/src/_concat.js @@ -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; } diff --git a/src/_erase.js b/src/_erase.js new file mode 100644 index 0000000..d016cfb --- /dev/null +++ b/src/_erase.js @@ -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); +} diff --git a/src/_remove.js b/src/_remove.js index f94c1b3..6766625 100644 --- a/src/_remove.js +++ b/src/_remove.js @@ -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 @@ -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); } diff --git a/src/index.js b/src/index.js index 043019f..6654aa7 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; diff --git a/test/src/remove.js b/test/src/remove.js new file mode 100644 index 0000000..6b506bb --- /dev/null +++ b/test/src/remove.js @@ -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'); +});