Skip to content

Commit 8852a55

Browse files
✨ feat: Implement from and DoublyLinkedList.from.
Fixes #7.
1 parent 865ebe6 commit 8852a55

File tree

7 files changed

+85
-45
lines changed

7 files changed

+85
-45
lines changed

src/DoublyLinkedList.js

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1+
import Node from './Node.js';
2+
import Iterator from './Iterator.js';
3+
import ReverseIterator from './ReverseIterator.js';
4+
15
/**
26
* Doubly linked list implementation
37
* making use of dummy nodes for the
48
* sake of simplicity.
59
*/
6-
7-
export function DoublyLinkedList() {
10+
export default function DoublyLinkedList() {
811
this.front = new Node(null, null, null);
912
this.back = new Node(this.front, null, null);
1013
this.front.next = this.back;
1114
this.length = 0;
1215
}
1316

14-
export function Node(prev, next, value) {
15-
this.prev = prev;
16-
this.next = next;
17-
this.value = value;
18-
}
19-
20-
export function Iterator(front, back, current) {
21-
this.front = front;
22-
this.back = back;
23-
this.current = current;
24-
}
25-
26-
export function ReverseIterator(front, back, current) {
27-
this.front = front;
28-
this.back = back;
29-
this.current = current;
30-
}
31-
3217
DoublyLinkedList.prototype.insertAfter = function (iterator, value) {
3318
const prev = iterator.current;
3419

@@ -170,31 +155,14 @@ DoublyLinkedList.prototype.rend = function () {
170155
return this.riterator(this.front);
171156
};
172157

173-
Iterator.prototype.copy = function () {
174-
return new Iterator(this.front, this.back, this.current);
175-
};
176-
177-
ReverseIterator.prototype.copy = function () {
178-
return new ReverseIterator(this.front, this.back, this.current);
179-
};
180-
181-
// eslint-disable-next-line no-multi-assign
182-
Iterator.prototype.next = ReverseIterator.prototype.prev = function () {
183-
// eslint-disable-next-line no-multi-assign
184-
const c = (this.current = this.current.next);
185-
186-
return c === this.back ? {done: true} : {value: c.value, done: false};
187-
};
188-
189-
// eslint-disable-next-line no-multi-assign
190-
Iterator.prototype.prev = ReverseIterator.prototype.next = function () {
191-
// eslint-disable-next-line no-multi-assign
192-
const c = (this.current = this.current.prev);
193-
194-
return c === this.front ? {done: true} : {value: c.value, done: false};
158+
const from = (iterable) => {
159+
const list = new DoublyLinkedList();
160+
for (const value of iterable) list.push(value);
161+
return list;
195162
};
196163

197164
DoublyLinkedList.prototype[Symbol.iterator] = DoublyLinkedList.prototype.begin;
198165
DoublyLinkedList.Node = Node;
199166
DoublyLinkedList.Iterator = Iterator;
200167
DoublyLinkedList.ReverseIterator = ReverseIterator;
168+
DoublyLinkedList.from = from;

src/Iterator.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default function Iterator(front, back, current) {
2+
this.front = front;
3+
this.back = back;
4+
this.current = current;
5+
}
6+
7+
Iterator.prototype.copy = function () {
8+
return new Iterator(this.front, this.back, this.current);
9+
};
10+
11+
Iterator.prototype.next = function () {
12+
// eslint-disable-next-line no-multi-assign
13+
const c = (this.current = this.current.next);
14+
15+
return c === this.back ? {done: true} : {value: c.value, done: false};
16+
};
17+
18+
Iterator.prototype.prev = function () {
19+
// eslint-disable-next-line no-multi-assign
20+
const c = (this.current = this.current.prev);
21+
22+
return c === this.front ? {done: true} : {value: c.value, done: false};
23+
};

src/Node.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function Node(prev, next, value) {
2+
this.prev = prev;
3+
this.next = next;
4+
this.value = value;
5+
}

src/ReverseIterator.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Iterator from './Iterator.js';
2+
3+
export default function ReverseIterator(front, back, current) {
4+
this.front = front;
5+
this.back = back;
6+
this.current = current;
7+
}
8+
9+
ReverseIterator.prototype.copy = function () {
10+
return new ReverseIterator(this.front, this.back, this.current);
11+
};
12+
13+
ReverseIterator.prototype.prev = Iterator.prototype.next;
14+
ReverseIterator.prototype.next = Iterator.prototype.prev;

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
export * from './DoublyLinkedList.js';
1+
import DoublyLinkedList from './DoublyLinkedList.js';
2+
3+
export {default as DoublyLinkedList} from './DoublyLinkedList.js';
4+
export {default as Iterator} from './Iterator.js';
5+
export {default as Node} from './Node.js';
6+
export {default as ReverseIterator} from './ReverseIterator.js';
7+
export const from = DoublyLinkedList.from;

test/src/DoublyLinkedList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'ava';
2-
import * as dll from '#module';
2+
import {DoublyLinkedList} from '#module';
33

44
test('DoublyLinkedList', (t) => {
5-
const list = new dll.DoublyLinkedList();
5+
const list = new DoublyLinkedList();
66

77
const a = list.push(1);
88
const b = list.push(2);

test/src/from.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import test from 'ava';
2+
import {from, DoublyLinkedList} from '#module';
3+
4+
test('from is DoublyLinkedList.from', (t) => {
5+
t.is(from, DoublyLinkedList.from);
6+
});
7+
8+
const macro = test.macro({
9+
exec(t, sequence) {
10+
const expected = Array.from(sequence);
11+
const list = DoublyLinkedList.from(sequence);
12+
t.deepEqual(Array.from(list), expected);
13+
},
14+
title(title, sequence) {
15+
return title ?? `from(${JSON.stringify(sequence)})`;
16+
},
17+
});
18+
19+
test(macro, []);
20+
test(macro, [1]);
21+
test(macro, [1, 2, 3]);
22+
test(macro, '');
23+
test(macro, 'a');
24+
test(macro, 'ab');

0 commit comments

Comments
 (0)