From fe099e843f69e2344a8c2a6f840e2c4815241f2b 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 17:56:51 +0100 Subject: [PATCH] :recycle: refactor: Improve and simplify from, push, and unshift. --- src/Node.js | 13 ++++++++----- src/_push.js | 18 ++++++++++++++++++ src/_unshift.js | 17 +++++++++++++++++ src/from.js | 8 +++----- src/index.js | 2 ++ src/push.js | 9 +++------ src/unshift.js | 12 ++++-------- 7 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 src/_push.js create mode 100644 src/_unshift.js diff --git a/src/Node.js b/src/Node.js index d9af82c..8a9e68d 100644 --- a/src/Node.js +++ b/src/Node.js @@ -3,12 +3,15 @@ * * @class * @param {any} value The value to hold. + * @param {Node|null} prev The value to hold. + * @param {Node|null} next The value to hold. */ -export default function Node(value) { +// eslint-disable-next-line unicorn/prevent-abbreviations +export default function Node(value, prev, next) { /** @member {any} The value/key held by this node. */ this.value = value; - /** @member {Node} Pointer to previous (left) sibling */ - this.prev = null; - /** @member {Node} Pointer to next (right) sibling */ - this.next = null; + /** @member {Node|null} Pointer to previous (left) sibling */ + this.prev = prev; + /** @member {Node|null} Pointer to next (right) sibling */ + this.next = next; } diff --git a/src/_push.js b/src/_push.js new file mode 100644 index 0000000..4850886 --- /dev/null +++ b/src/_push.js @@ -0,0 +1,18 @@ +import assert from 'assert'; +import Node from './Node.js'; + +/** + * Push value to list. + * + * @param {Node} z Last node of first input list (can be null). + * @param {any} value Value to push. + * @return {Node} The node at the front of the list (new node if empty, input + * node otherwise). + */ +export default function _push(z, value) { + assert(z instanceof Node); + assert(z.next === null); + const y = new Node(value, z, null); + z.next = y; + return y; +} diff --git a/src/_unshift.js b/src/_unshift.js new file mode 100644 index 0000000..27d6383 --- /dev/null +++ b/src/_unshift.js @@ -0,0 +1,17 @@ +import assert from 'assert'; +import Node from './Node.js'; + +/** + * Unshift value to list. + * + * @param {Node} x First node of first input list (can be null). + * @param {any} value Value to unshift. + * @return {Node} The node at the front of the list (hence, the new node). + */ +export default function _unshift(x, value) { + assert(x instanceof Node); + assert(x.prev === null); + const y = new Node(value, null, x); + x.prev = y; + return y; +} diff --git a/src/from.js b/src/from.js index 2431bb4..19d903d 100644 --- a/src/from.js +++ b/src/from.js @@ -1,5 +1,5 @@ import Node from './Node.js'; -import _concat from './_concat.js'; +import _push from './_push.js'; /** * Creates a list from an input iterable. @@ -13,13 +13,11 @@ export default function from(iterable) { if (event.done) return null; - const first = new Node(event.value); + const first = new Node(event.value, null, null); let last = first; for (const value of it) { - const next = new Node(value); - _concat(last, next); - last = next; + last = _push(last, value); } return first; diff --git a/src/index.js b/src/index.js index 8800bfb..a767383 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ export {default as _iter_fast} from './_iter_fast.js'; export {default as _last} from './_last.js'; export {default as _len} from './_len.js'; export {default as _pop} from './_pop.js'; +export {default as _push} from './_push.js'; export {default as _remove} from './_remove.js'; export {default as _rotate_left} from './_rotate_left.js'; export {default as _rotate_left_modulo} from './_rotate_left_modulo.js'; @@ -15,6 +16,7 @@ export {default as _rotate_right_unknown_length} from './_rotate_right_unknown_l export {default as _rotate_to} from './_rotate_to.js'; export {default as _shift} from './_shift.js'; export {default as _split} from './_split.js'; +export {default as _unshift} from './_unshift.js'; export {default as concat} from './concat.js'; export {default as empty} from './empty.js'; export {default as from} from './from.js'; diff --git a/src/push.js b/src/push.js index 5623471..8aaab0e 100644 --- a/src/push.js +++ b/src/push.js @@ -1,6 +1,6 @@ import assert from 'assert'; import Node from './Node.js'; -import _concat from './_concat.js'; +import _push from './_push.js'; /** * Push value to list. @@ -12,11 +12,8 @@ import _concat from './_concat.js'; * node otherwise). */ export default function push(x, z, value) { - if (x === null) return new Node(value); + if (x === null) return new Node(value, null, null); assert(x instanceof Node); - assert(z instanceof Node); - assert(z.next === null); - const y = new Node(value); - _concat(z, y); + _push(z, value); return x; } diff --git a/src/unshift.js b/src/unshift.js index 1b8ab0f..b4a0d5c 100644 --- a/src/unshift.js +++ b/src/unshift.js @@ -1,18 +1,14 @@ -import assert from 'assert'; import Node from './Node.js'; -import _concat from './_concat.js'; +import _unshift from './_unshift.js'; /** * Unshift value to list. * * @param {Node} x First node of first input list (can be null). - * @param {Object} value Value to unshift. + * @param {any} value Value to unshift. * @return {Node} The node at the front of the list (hence, the new node). */ export default function unshift(x, value) { - if (x === null) return new Node(value); - assert(x instanceof Node); - const y = new Node(value); - _concat(y, x); - return y; + if (x === null) return new Node(value, null, null); + return _unshift(x, value); }