Skip to content

Commit fe099e8

Browse files
♻️ refactor: Improve and simplify from, push, and unshift.
1 parent 0d49ca5 commit fe099e8

File tree

7 files changed

+55
-24
lines changed

7 files changed

+55
-24
lines changed

src/Node.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
*
44
* @class
55
* @param {any} value The value to hold.
6+
* @param {Node|null} prev The value to hold.
7+
* @param {Node|null} next The value to hold.
68
*/
7-
export default function Node(value) {
9+
// eslint-disable-next-line unicorn/prevent-abbreviations
10+
export default function Node(value, prev, next) {
811
/** @member {any} The value/key held by this node. */
912
this.value = value;
10-
/** @member {Node} Pointer to previous (left) sibling */
11-
this.prev = null;
12-
/** @member {Node} Pointer to next (right) sibling */
13-
this.next = null;
13+
/** @member {Node|null} Pointer to previous (left) sibling */
14+
this.prev = prev;
15+
/** @member {Node|null} Pointer to next (right) sibling */
16+
this.next = next;
1417
}

src/_push.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import assert from 'assert';
2+
import Node from './Node.js';
3+
4+
/**
5+
* Push value to list.
6+
*
7+
* @param {Node} z Last node of first input list (can be null).
8+
* @param {any} value Value to push.
9+
* @return {Node} The node at the front of the list (new node if empty, input
10+
* node otherwise).
11+
*/
12+
export default function _push(z, value) {
13+
assert(z instanceof Node);
14+
assert(z.next === null);
15+
const y = new Node(value, z, null);
16+
z.next = y;
17+
return y;
18+
}

src/_unshift.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import assert from 'assert';
2+
import Node from './Node.js';
3+
4+
/**
5+
* Unshift value to list.
6+
*
7+
* @param {Node} x First node of first input list (can be null).
8+
* @param {any} value Value to unshift.
9+
* @return {Node} The node at the front of the list (hence, the new node).
10+
*/
11+
export default function _unshift(x, value) {
12+
assert(x instanceof Node);
13+
assert(x.prev === null);
14+
const y = new Node(value, null, x);
15+
x.prev = y;
16+
return y;
17+
}

src/from.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Node from './Node.js';
2-
import _concat from './_concat.js';
2+
import _push from './_push.js';
33

44
/**
55
* Creates a list from an input iterable.
@@ -13,13 +13,11 @@ export default function from(iterable) {
1313

1414
if (event.done) return null;
1515

16-
const first = new Node(event.value);
16+
const first = new Node(event.value, null, null);
1717
let last = first;
1818

1919
for (const value of it) {
20-
const next = new Node(value);
21-
_concat(last, next);
22-
last = next;
20+
last = _push(last, value);
2321
}
2422

2523
return first;

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {default as _iter_fast} from './_iter_fast.js';
55
export {default as _last} from './_last.js';
66
export {default as _len} from './_len.js';
77
export {default as _pop} from './_pop.js';
8+
export {default as _push} from './_push.js';
89
export {default as _remove} from './_remove.js';
910
export {default as _rotate_left} from './_rotate_left.js';
1011
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
1516
export {default as _rotate_to} from './_rotate_to.js';
1617
export {default as _shift} from './_shift.js';
1718
export {default as _split} from './_split.js';
19+
export {default as _unshift} from './_unshift.js';
1820
export {default as concat} from './concat.js';
1921
export {default as empty} from './empty.js';
2022
export {default as from} from './from.js';

src/push.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
22
import Node from './Node.js';
3-
import _concat from './_concat.js';
3+
import _push from './_push.js';
44

55
/**
66
* Push value to list.
@@ -12,11 +12,8 @@ import _concat from './_concat.js';
1212
* node otherwise).
1313
*/
1414
export default function push(x, z, value) {
15-
if (x === null) return new Node(value);
15+
if (x === null) return new Node(value, null, null);
1616
assert(x instanceof Node);
17-
assert(z instanceof Node);
18-
assert(z.next === null);
19-
const y = new Node(value);
20-
_concat(z, y);
17+
_push(z, value);
2118
return x;
2219
}

src/unshift.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import assert from 'assert';
21
import Node from './Node.js';
3-
import _concat from './_concat.js';
2+
import _unshift from './_unshift.js';
43

54
/**
65
* Unshift value to list.
76
*
87
* @param {Node} x First node of first input list (can be null).
9-
* @param {Object} value Value to unshift.
8+
* @param {any} value Value to unshift.
109
* @return {Node} The node at the front of the list (hence, the new node).
1110
*/
1211
export default function unshift(x, value) {
13-
if (x === null) return new Node(value);
14-
assert(x instanceof Node);
15-
const y = new Node(value);
16-
_concat(y, x);
17-
return y;
12+
if (x === null) return new Node(value, null, null);
13+
return _unshift(x, value);
1814
}

0 commit comments

Comments
 (0)