-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext-value.js
74 lines (61 loc) · 1.76 KB
/
ext-value.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
import ExtSList, {Ptr} from './ext.js';
import {ValueNode} from './nodes.js';
import {addAlias, mapIterator, normalizeIterator} from '../meta-utils.js';
export class ExtValueSList extends ExtSList {
adoptValue(value) {
if (value instanceof Ptr) {
if (!this.isCompatiblePtr(value)) throw new Error('Incompatible pointer');
if (value.node instanceof ValueNode) {
value.list = this;
return super.adoptNode(value);
}
return new ValueNode(value.node, this);
}
if (value instanceof ValueNode) {
if (!this.isNodeLike(value)) throw new Error('Incompatible node');
return super.adoptNode(value);
}
return new ValueNode(value, this);
}
// iterators
[Symbol.iterator]() {
let current = this.head,
readyToStop = this.isEmpty;
return normalizeIterator({
next: () => {
if (readyToStop && current === this.head) return {done: true};
readyToStop = true;
const value = current.value;
current = current[this.nextName];
return {value};
}
});
}
getValueIterator(range) {
return mapIterator(this.getNodeIterator(range), node => node.value);
}
// meta helpers
clone() {
return new ExtValueSList(this);
}
make(head = null) {
return new ExtValueSList(head, this);
}
makeFrom(values) {
return ExtValueSList.from(values, this);
}
static from(values, options) {
const list = new ExtValueSList(null, options);
for (const value of values) {
list.addAfter(value);
list.next();
}
return list.next();
}
}
ExtValueSList.Ptr = Ptr;
ExtValueSList.ValueNode = ValueNode;
addAlias(ExtValueSList.prototype, 'getIterator', 'getValueIterator');
export {ValueNode};
export default ExtValueSList;