Skip to content

Commit 855e416

Browse files
✨ feat: Add _pop and _shift. Refactor pop and shift.
Fixes #47.
1 parent 5ccbf6a commit 855e416

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/_pop.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from 'assert';
2+
import Node from './Node.js';
3+
import _remove from './_remove.js';
4+
5+
/**
6+
* Removes last {@link Node} from a non-empty list.
7+
*
8+
* @param {Node} x First node (not null).
9+
* @return {Node} New list (possibly null).
10+
*/
11+
export default function _pop(x) {
12+
assert(x instanceof Node);
13+
if (x === x.prev) return null;
14+
_remove(x.prev);
15+
return x;
16+
}

src/_shift.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from 'assert';
2+
import Node from './Node.js';
3+
import _remove from './_remove.js';
4+
5+
/**
6+
* Removes first {@link Node} from a non-empty list.
7+
*
8+
* @param {Node} x First node (not null).
9+
* @return {Node} New list (possibly null).
10+
*/
11+
export default function _shift(x) {
12+
assert(x instanceof Node);
13+
if (x === x.next) return null;
14+
_remove(x);
15+
return x.next;
16+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export {default as _concat} from './_concat.js';
44
export {default as _iter} from './_iter.js';
55
export {default as _iter_fast} from './_iter_fast.js';
66
export {default as _len} from './_len.js';
7+
export {default as _pop} from './_pop.js';
78
export {default as _remove} from './_remove.js';
9+
export {default as _shift} from './_shift.js';
810
export {default as concat} from './concat.js';
911
export {default as empty} from './empty.js';
1012
export {default as from} from './from.js';

src/pop.js

Lines changed: 2 additions & 3 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 _remove from './_remove.js';
3+
import _pop from './_pop.js';
44

55
/**
66
* Removes last {@link Node} from a list. Throws if input list is empty.
@@ -12,6 +12,5 @@ export default function pop(x) {
1212
if (x === null) throw new Error('input list is empty');
1313
assert(x instanceof Node);
1414
const node = x.prev;
15-
_remove(node);
16-
return [x === node ? null : x, node];
15+
return [_pop(x), node];
1716
}

src/shift.js

Lines changed: 2 additions & 3 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 _remove from './_remove.js';
3+
import _shift from './_shift.js';
44

55
/**
66
* Removes first {@link Node} from a list. Throws if input list is empty.
@@ -11,6 +11,5 @@ import _remove from './_remove.js';
1111
export default function shift(x) {
1212
if (x === null) throw new Error('input list is empty');
1313
assert(x instanceof Node);
14-
_remove(x);
15-
return [x === x.next ? null : x.next, x];
14+
return [_shift(x), x];
1615
}

0 commit comments

Comments
 (0)