forked from immutable-js/immutable-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seq.js
343 lines (303 loc) · 7.83 KB
/
Seq.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { wrapIndex } from './TrieUtils';
import { Collection } from './Collection';
import { IS_SEQ_SYMBOL, isSeq } from './predicates/isSeq';
import { isImmutable } from './predicates/isImmutable';
import { isCollection } from './predicates/isCollection';
import { isKeyed } from './predicates/isKeyed';
import { isAssociative } from './predicates/isAssociative';
import { isRecord } from './predicates/isRecord';
import { IS_ORDERED_SYMBOL } from './predicates/isOrdered';
import {
Iterator,
iteratorValue,
iteratorDone,
hasIterator,
isIterator,
getIterator,
isEntriesIterable,
isKeysIterable,
} from './Iterator';
import hasOwnProperty from './utils/hasOwnProperty';
import isArrayLike from './utils/isArrayLike';
export class Seq extends Collection {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return value === undefined || value === null
? emptySequence()
: isImmutable(value)
? value.toSeq()
: seqFromValue(value);
}
toSeq() {
return this;
}
toString() {
return this.__toString('Seq {', '}');
}
cacheResult() {
if (!this._cache && this.__iterateUncached) {
this._cache = this.entrySeq().toArray();
this.size = this._cache.length;
}
return this;
}
// abstract __iterateUncached(fn, reverse)
__iterate(fn, reverse) {
const cache = this._cache;
if (cache) {
const size = cache.length;
let i = 0;
while (i !== size) {
const entry = cache[reverse ? size - ++i : i++];
if (fn(entry[1], entry[0], this) === false) {
break;
}
}
return i;
}
return this.__iterateUncached(fn, reverse);
}
// abstract __iteratorUncached(type, reverse)
__iterator(type, reverse) {
const cache = this._cache;
if (cache) {
const size = cache.length;
let i = 0;
return new Iterator(() => {
if (i === size) {
return iteratorDone();
}
const entry = cache[reverse ? size - ++i : i++];
return iteratorValue(type, entry[0], entry[1]);
});
}
return this.__iteratorUncached(type, reverse);
}
}
export class KeyedSeq extends Seq {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return value === undefined || value === null
? emptySequence().toKeyedSeq()
: isCollection(value)
? isKeyed(value)
? value.toSeq()
: value.fromEntrySeq()
: isRecord(value)
? value.toSeq()
: keyedSeqFromValue(value);
}
toKeyedSeq() {
return this;
}
}
export class IndexedSeq extends Seq {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return value === undefined || value === null
? emptySequence()
: isCollection(value)
? isKeyed(value)
? value.entrySeq()
: value.toIndexedSeq()
: isRecord(value)
? value.toSeq().entrySeq()
: indexedSeqFromValue(value);
}
static of(/*...values*/) {
return IndexedSeq(arguments);
}
toIndexedSeq() {
return this;
}
toString() {
return this.__toString('Seq [', ']');
}
}
export class SetSeq extends Seq {
constructor(value) {
// eslint-disable-next-line no-constructor-return
return (
isCollection(value) && !isAssociative(value) ? value : IndexedSeq(value)
).toSetSeq();
}
static of(/*...values*/) {
return SetSeq(arguments);
}
toSetSeq() {
return this;
}
}
Seq.isSeq = isSeq;
Seq.Keyed = KeyedSeq;
Seq.Set = SetSeq;
Seq.Indexed = IndexedSeq;
Seq.prototype[IS_SEQ_SYMBOL] = true;
// #pragma Root Sequences
export class ArraySeq extends IndexedSeq {
constructor(array) {
this._array = array;
this.size = array.length;
}
get(index, notSetValue) {
return this.has(index) ? this._array[wrapIndex(this, index)] : notSetValue;
}
__iterate(fn, reverse) {
const array = this._array;
const size = array.length;
let i = 0;
while (i !== size) {
const ii = reverse ? size - ++i : i++;
if (fn(array[ii], ii, this) === false) {
break;
}
}
return i;
}
__iterator(type, reverse) {
const array = this._array;
const size = array.length;
let i = 0;
return new Iterator(() => {
if (i === size) {
return iteratorDone();
}
const ii = reverse ? size - ++i : i++;
return iteratorValue(type, ii, array[ii]);
});
}
}
class ObjectSeq extends KeyedSeq {
constructor(object) {
const keys = Object.keys(object).concat(
Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : []
);
this._object = object;
this._keys = keys;
this.size = keys.length;
}
get(key, notSetValue) {
if (notSetValue !== undefined && !this.has(key)) {
return notSetValue;
}
return this._object[key];
}
has(key) {
return hasOwnProperty.call(this._object, key);
}
__iterate(fn, reverse) {
const object = this._object;
const keys = this._keys;
const size = keys.length;
let i = 0;
while (i !== size) {
const key = keys[reverse ? size - ++i : i++];
if (fn(object[key], key, this) === false) {
break;
}
}
return i;
}
__iterator(type, reverse) {
const object = this._object;
const keys = this._keys;
const size = keys.length;
let i = 0;
return new Iterator(() => {
if (i === size) {
return iteratorDone();
}
const key = keys[reverse ? size - ++i : i++];
return iteratorValue(type, key, object[key]);
});
}
}
ObjectSeq.prototype[IS_ORDERED_SYMBOL] = true;
class CollectionSeq extends IndexedSeq {
constructor(collection) {
this._collection = collection;
this.size = collection.length || collection.size;
}
__iterateUncached(fn, reverse) {
if (reverse) {
return this.cacheResult().__iterate(fn, reverse);
}
const collection = this._collection;
const iterator = getIterator(collection);
let iterations = 0;
if (isIterator(iterator)) {
let step;
while (!(step = iterator.next()).done) {
if (fn(step.value, iterations++, this) === false) {
break;
}
}
}
return iterations;
}
__iteratorUncached(type, reverse) {
if (reverse) {
return this.cacheResult().__iterator(type, reverse);
}
const collection = this._collection;
const iterator = getIterator(collection);
if (!isIterator(iterator)) {
return new Iterator(iteratorDone);
}
let iterations = 0;
return new Iterator(() => {
const step = iterator.next();
return step.done ? step : iteratorValue(type, iterations++, step.value);
});
}
}
// # pragma Helper functions
let EMPTY_SEQ;
function emptySequence() {
return EMPTY_SEQ || (EMPTY_SEQ = new ArraySeq([]));
}
export function keyedSeqFromValue(value) {
const seq = maybeIndexedSeqFromValue(value);
if (seq) {
return seq.fromEntrySeq();
}
if (typeof value === 'object') {
return new ObjectSeq(value);
}
throw new TypeError(
'Expected Array or collection object of [k, v] entries, or keyed object: ' +
value
);
}
export function indexedSeqFromValue(value) {
const seq = maybeIndexedSeqFromValue(value);
if (seq) {
return seq;
}
throw new TypeError(
'Expected Array or collection object of values: ' + value
);
}
function seqFromValue(value) {
const seq = maybeIndexedSeqFromValue(value);
if (seq) {
return isEntriesIterable(value)
? seq.fromEntrySeq()
: isKeysIterable(value)
? seq.toSetSeq()
: seq;
}
if (typeof value === 'object') {
return new ObjectSeq(value);
}
throw new TypeError(
'Expected Array or collection object of values, or keyed object: ' + value
);
}
function maybeIndexedSeqFromValue(value) {
return isArrayLike(value)
? new ArraySeq(value)
: hasIterator(value)
? new CollectionSeq(value)
: undefined;
}