From d3e6cf9e0fc0cc9bf4b631988525e271d4504d37 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:53:26 +0100 Subject: [PATCH] :recycle: refactor: Extract _extend from from. --- src/_extend.js | 21 +++++++++++++++++++++ src/from.js | 9 ++------- src/index.js | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/_extend.js diff --git a/src/_extend.js b/src/_extend.js new file mode 100644 index 0000000..bcfbae8 --- /dev/null +++ b/src/_extend.js @@ -0,0 +1,21 @@ +import assert from 'assert'; +import Node from './Node.js'; +import _push from './_push.js'; + +/** + * Extend a list with an iterable. + * + * @param {Node} z The last node of the list to extend. + * @param {Iterable} iterable The input iterable. + * @return {Node} Last node of the extended list. + */ +export default function _extend(z, iterable) { + assert(z instanceof Node); + let y = z; + + for (const value of iterable) { + y = _push(y, value); + } + + return y; +} diff --git a/src/from.js b/src/from.js index 19d903d..f769fff 100644 --- a/src/from.js +++ b/src/from.js @@ -1,5 +1,5 @@ import Node from './Node.js'; -import _push from './_push.js'; +import _extend from './_extend.js'; /** * Creates a list from an input iterable. @@ -14,11 +14,6 @@ export default function from(iterable) { if (event.done) return null; const first = new Node(event.value, null, null); - let last = first; - - for (const value of it) { - last = _push(last, value); - } - + _extend(first, it); return first; } diff --git a/src/index.js b/src/index.js index a767383..043019f 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 _extend} from './_extend.js'; export {default as _iter} from './_iter.js'; export {default as _iter_fast} from './_iter_fast.js'; export {default as _last} from './_last.js';