Skip to content

Commit f34d173

Browse files
📚 docs: Improve signatures and assertions.
1 parent 554b934 commit f34d173

File tree

12 files changed

+54
-29
lines changed

12 files changed

+54
-29
lines changed

src/Node.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/**
22
* Base node class.
33
*
4-
* @param {Object} value - The value to hold.
4+
* @class
5+
* @param {any} value The value to hold.
56
*/
67
export default function Node(value) {
7-
this.value = value; // Key
8-
this.prev = this; // Pointer to previous (left) sibling
9-
this.next = this; // Pointer to next (right) sibling
8+
/** @member {any} The value/key held by this node. */
9+
this.value = value;
10+
/** @member {Node} Pointer to previous (left) sibling */
11+
this.prev = this;
12+
/** @member {Node} Pointer to next (right) sibling */
13+
this.next = this;
1014
}

src/_append.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import assert from 'assert';
2+
import Node from './Node.js';
23

34
/**
45
* Optimization of {@link _concat} when <code>x</code> contains a single element.
56
* Works even if <code>x</code> has dangling pointers <code>x.next</code> and <code>x.prev</code>.
67
*
7-
* @param {Node} a - First node of list.
8-
* @param {Node} x - Node to be inserted at the end of list.
8+
* @param {Node} a First node of list.
9+
* @param {Node} x Node to be inserted at the end of list.
910
*/
1011
export default function _append(a, x) {
11-
assert(a !== null && x !== null);
12-
assert(x.next === x && x.prev === x);
12+
assert(a instanceof Node);
13+
assert(x instanceof Node);
14+
assert(x.next === x);
15+
assert(x.prev === x);
1316
// FROM a - b - c - d - a x
1417
// TO a - b - c - d - x - a
1518
const d = a.prev;

src/_concat.js

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

34
/**
45
* Concatenate two input lists.
56
*
6-
* @param {Node} x - First node of first input list.
7-
* @param {Node} y - First node of second input list.
7+
* @param {Node} x First node of first input list.
8+
* @param {Node} y First node of second input list.
89
*/
910
export default function _concat(x, y) {
10-
assert(x !== null && y !== null);
11+
assert(x instanceof Node);
12+
assert(y instanceof Node);
1113
// FROM x - b - c - d - x y - f - g - h - y
1214
// TO x - b - c - d - y - f - g - h - x
1315
const d = x.prev;

src/_iter.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import assert from 'assert';
2+
import Node from './Node.js';
23

34
/**
45
* Generator of nodes in list in order.
56
*
6-
* @param {Node} first - First node of the list.
7-
* @return {Iterable}
7+
* TODO yield values/keys instead?
8+
*
9+
* @param {Node} first First node of the list.
10+
* @return {IterableIterator} Yields nodes of a list in order.
811
*/
912
export default function* _iter(first) {
10-
assert(first !== null);
13+
assert(first instanceof Node);
1114
let next = first;
1215

1316
do {

src/_remove.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import assert from 'assert';
2+
import Node from './Node.js';
23

34
/**
45
* Removes input {@link Node} from its list.
56
*
67
* /!\ Pointers in the extracted node are left unchanged.
78
* /!\ <code>x</code> will have dangling pointers after removal if not single element.
89
*
9-
* @param {Node} x - Node to remove.
10+
* @param {Node} x Node to remove.
1011
*/
1112
export default function _remove(x) {
12-
assert(x !== null);
13+
assert(x instanceof Node);
1314
x.prev.next = x.next;
1415
x.next.prev = x.prev;
1516
}

src/concat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import _concat from './_concat.js';
55
/**
66
* Concatenate two input lists.
77
*
8-
* @param {Node} x - First node of first input list (can be null).
9-
* @param {Node} y - First node of second input list (can be null).
8+
* @param {Node} x First node of first input list (can be null).
9+
* @param {Node} y First node of second input list (can be null).
10+
* @return {Node} First node of the output list (or null if empty).
1011
*/
1112
export default function concat(x, y) {
1213
if (x === null) return y;

src/from.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import _append from './_append.js';
44
/**
55
* Creates a list from an input iterable.
66
*
7-
* @param {Iterable} iterable - The input iterable.
7+
* @param {Iterable} iterable The input iterable.
8+
* @return {Node} First node of the newly created list (or null if empty list).
89
*/
910
export default function from(iterable) {
1011
const it = iterable[Symbol.iterator]();

src/iter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import assert from 'assert';
12
import _iter from './_iter.js';
3+
import Node from './Node.js';
4+
25
/**
36
* Generator of nodes in list in order.
47
*
5-
* @param {Node} first - First node of the list (can be null).
6-
* @return {Iterable}
8+
* @param {Node} first First node of the list (can be null).
9+
* @return {IterableIterator}
710
*/
811
export default function* iter(first) {
12+
assert(first === null || first instanceof Node);
913
if (first !== null) yield* _iter(first);
1014
}

src/pop.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import Node from './Node.js';
33
import _remove from './_remove.js';
44

55
/**
6-
* Removes last {@link Node} from a list.
6+
* Removes last {@link Node} from a list. Throws if input list is empty.
77
*
8-
* @param {Node} x - First node .
8+
* @param {Node} x First node.
99
* @return {[Node, Node]} New list (possibly null) and popped node.
1010
*/
1111
export default function pop(x) {

src/push.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import _append from './_append.js';
55
/**
66
* Push value to list.
77
*
8-
* @param {Node} x - First node of first input list (can be null).
9-
* @param {Object} value - Value to push.
8+
* @param {Node} x First node of first input list (can be null).
9+
* @param {any} value Value to push.
10+
* @return {Node} The node at the front of the list (new node if empty, input
11+
* node otherwise).
1012
*/
1113
export default function push(x, value) {
1214
if (x === null) return new Node(value);

0 commit comments

Comments
 (0)