We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7b49232 commit 916d451Copy full SHA for 916d451
src/Node.js
@@ -2,8 +2,9 @@
2
* Node.
3
*
4
* @param {any} value
5
+ * @param {Node} next
6
*/
-export default function Node(value) {
7
+export default function Node(value, next) {
8
this._value = value;
- this._next = null;
9
+ this._next = next;
10
}
src/_insertAfter.js
@@ -0,0 +1,19 @@
1
+import assert from 'assert';
+
+// eslint-disable-next-line no-unused-vars
+import Node from './Node.js';
+import isNonEmpty from './isNonEmpty.js';
+import unshift from './unshift.js';
+import _setNext from './_setNext.js';
+import _shift from './_shift.js';
+/**
11
+ * Inserts value just after input node (cannot be null).
12
+ *
13
+ * @param {Node} x Input node (cannot be null).
14
+ * @param {any} value
15
+ */
16
+export default function _insertAfter(x, value) {
17
+ assert(isNonEmpty(x));
18
+ _setNext(x, unshift(_shift(x), value));
19
+}
src/index.js
@@ -1,4 +1,5 @@
export {default as Node} from './Node.js';
+export {default as _insertAfter} from './_insertAfter.js';
export {default as _isLast} from './_isLast.js';
export {default as _iter} from './_iter.js';
export {default as _iter_fast} from './_iter_fast.js';
@@ -12,6 +13,7 @@ export {default as _value} from './_value.js';
export {default as concat} from './concat.js';
export {default as empty} from './empty.js';
export {default as from} from './from.js';
+export {default as insertAfter} from './insertAfter.js';
export {default as isEmpty} from './isEmpty.js';
export {default as isLast} from './isLast.js';
export {default as isList} from './isList.js';
src/insertAfter.js
+import isEmpty from './isEmpty.js';
+import _insertAfter from './_insertAfter.js';
+ * Inserts value just after input node. Throws if input list is empty
+ * @param {Node} x Input node.
+export default function insertAfter(x, value) {
+ if (isEmpty(x)) throw new Error('input list is empty');
+ _insertAfter(x, value);
src/single.js
@@ -7,5 +7,5 @@ import Node from './Node.js';
* @return {Node} The node at the front of the list.
export default function single(value) {
- return new Node(value);
+ return new Node(value, null);
src/unshift.js
@@ -1,10 +1,7 @@
import assert from 'assert';
-// eslint-disable-next-line no-unused-vars
import Node from './Node.js';
import isList from './isList.js';
-import _setNext from './_setNext.js';
-import single from './single.js';
/**
* Unshift value to list.
@@ -15,7 +12,5 @@ import single from './single.js';
export default function unshift(x, value) {
assert(isList(x));
- const y = single(value);
- _setNext(y, x);
20
- return y;
+ return new Node(value, x);
21
test/src/api.js
@@ -15,6 +15,7 @@ import {
value,
setValue,
values,
+ insertAfter,
} from '../../src/index.js';
const toArray = (first) => list(values(first));
@@ -28,6 +29,7 @@ test('API', (t) => {
28
29
t.throws(() => shift(A), {message: /empty/});
30
t.throws(() => value(A), {message: /empty/});
31
t.throws(() => setValue(A, {}), {message: /empty/});
32
+ t.throws(() => insertAfter(A, {}), {message: /empty/});
33
t.throws(() => last(A), {message: /empty/});
34
35
t.false(isLast(A));
@@ -73,4 +75,9 @@ test('API', (t) => {
73
75
74
76
t.deepEqual(toArray(A), [8, 2, 1, 4, 3, 7]);
77
t.deepEqual(toArray(B), []);
78
79
+ insertAfter(A, 9);
80
+ t.deepEqual(toArray(A), [8, 9, 2, 1, 4, 3, 7]);
81
+ insertAfter(last(A), 5);
82
+ t.deepEqual(toArray(A), [8, 9, 2, 1, 4, 3, 7, 5]);
83
});
0 commit comments