-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathiterator.js
380 lines (333 loc) · 11.1 KB
/
iterator.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"use strict";
module.exports = Iterator;
var Object = require("./shim-object");
var GenericCollection = require("./generic-collection");
// upgrades an iterable to a Iterator
function Iterator(iterable, standardMode) {
/*
standardMode should be passed as true by a collection that uses Iterator
to provide a polyfill of standard iterations methods like entries() and values(),
as Collection's iterator behaves differently than standards ones when it comes to sparse arrays.
without passing standardMode, new Iterator instances will behave as intended independently of standards.
*/
var values = standardMode && iterable && iterable.values && iterable.values();
if(values && typeof values.next === "function" ) {
return values;
}
if (!(this instanceof Iterator)) {
return new Iterator(iterable);
}
if (Array.isArray(iterable) || typeof iterable === "string")
return Iterator.iterate(iterable);
iterable = Object(iterable);
if (iterable instanceof Iterator) {
return iterable;
} else if (iterable.next) {
this.next = function () {
return iterable.next();
};
} else if (iterable.iterate) {
var iterator = iterable.iterate();
this.next = function () {
return iterator.next();
};
} else if (Object.prototype.toString.call(iterable) === "[object Function]") {
this.next = iterable;
} else {
throw new TypeError("Can't iterate " + iterable);
}
}
Iterator.prototype.forEach = GenericCollection.prototype.forEach;
Iterator.prototype.map = GenericCollection.prototype.map;
Iterator.prototype.filter = GenericCollection.prototype.filter;
Iterator.prototype.every = GenericCollection.prototype.every;
Iterator.prototype.some = GenericCollection.prototype.some;
Iterator.prototype.any = GenericCollection.prototype.any;
Iterator.prototype.all = GenericCollection.prototype.all;
Iterator.prototype.min = GenericCollection.prototype.min;
Iterator.prototype.max = GenericCollection.prototype.max;
Iterator.prototype.sum = GenericCollection.prototype.sum;
Iterator.prototype.average = GenericCollection.prototype.average;
Iterator.prototype.flatten = GenericCollection.prototype.flatten;
Iterator.prototype.zip = GenericCollection.prototype.zip;
Iterator.prototype.enumerate = GenericCollection.prototype.enumerate;
Iterator.prototype.sorted = GenericCollection.prototype.sorted;
Iterator.prototype.group = GenericCollection.prototype.group;
Iterator.prototype.reversed = GenericCollection.prototype.reversed;
Iterator.prototype.toArray = GenericCollection.prototype.toArray;
Iterator.prototype.toObject = GenericCollection.prototype.toObject;
Iterator.prototype.iterator = GenericCollection.prototype.iterator;
Iterator.prototype.__iterationObject = null;
Object.defineProperty(Iterator.prototype,"_iterationObject", {
get: function() {
return this.__iterationObject || (this.__iterationObject = { done: false, value:void 0});
}
});
// this is a bit of a cheat so flatten and such work with the generic
// reducible
Iterator.prototype.constructClone = function (values) {
var clone = [];
clone.addEach(values);
return clone;
};
Iterator.prototype.mapIterator = function (callback /*, thisp*/) {
var self = Iterator(this),
thisp = arguments[1],
i = 0;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
return new self.constructor(function () {
if(self._iterationObject.done !== true) {
var callbackValue = callback.call(thisp, self.next().value, i++, self);
self._iterationObject.value = callbackValue;
}
return self._iterationObject;
});
};
Iterator.prototype.filterIterator = function (callback /*, thisp*/) {
var self = Iterator(this),
thisp = arguments[1],
i = 0;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
return new self.constructor(function () {
var nextEntry;
while (true) {
nextEntry = self.next();
if(nextEntry.done !== true) {
if (callback.call(thisp, nextEntry.value, i++, self))
return nextEntry;
}
else {
//done true and value undefined at this point
return nextEntry;
}
}
});
};
Iterator.prototype.reduce = function (callback /*, initial, thisp*/) {
var self = Iterator(this),
result = arguments[1],
thisp = arguments[2],
i = 0,
nextEntry;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
// first iteration unrolled
nextEntry = self.next();
if(nextEntry.done === true) {
if (arguments.length > 1) {
return arguments[1]; // initial
} else {
throw TypeError("cannot reduce a value from an empty iterator with no initial value");
}
}
if (arguments.length > 1) {
result = callback.call(thisp, result, nextEntry.value, i, self);
} else {
result = nextEntry.value;
}
i++;
// remaining entries
while (true) {
nextEntry = self.next();
if(nextEntry.done === true) {
return result;
}
result = callback.call(thisp, result, nextEntry.value, i, self);
i++;
}
};
Iterator.prototype.concat = function () {
return Iterator.concat(
Array.prototype.concat.apply(this, arguments)
);
};
Iterator.prototype.dropWhile = function (callback /*, thisp */) {
var self = Iterator(this),
thisp = arguments[1],
stopped = false,
stopValue,
nextEntry,
i = 0;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
while (true) {
nextEntry = self.next();
if(nextEntry.done === true) {
break;
}
if (!callback.call(thisp, nextEntry.value, i, self)) {
stopped = true;
stopValue = nextEntry.value;
break;
}
i++;
}
if (stopped) {
return self.constructor([stopValue]).concat(self);
} else {
return self.constructor([]);
}
};
Iterator.prototype.takeWhile = function (callback /*, thisp*/) {
var self = Iterator(this),
thisp = arguments[1],
nextEntry,
i = 0;
if (Object.prototype.toString.call(callback) != "[object Function]")
throw new TypeError();
return new self.constructor(function () {
if(self._iterationObject.done !== true) {
var value = self.next().value;
if(callback.call(thisp, value, i++, self)) {
self._iterationObject.value = value;
}
else {
self._iterationObject.done = true;
self._iterationObject.value = void 0;
}
}
return self._iterationObject;
});
};
Iterator.prototype.zipIterator = function () {
return Iterator.unzip(
Array.prototype.concat.apply(this, arguments)
);
};
Iterator.prototype.enumerateIterator = function (start) {
return Iterator.count(start).zipIterator(this);
};
// creates an iterator for Array and String
Iterator.iterate = function (iterable) {
var start;
start = 0;
return new Iterator(function () {
// advance to next owned entry
if (typeof iterable === "object") {
while (!(start in iterable)) {
// deliberately late bound
if (start >= iterable.length) {
this._iterationObject.done = true;
this._iterationObject.value = void 0;
break;
}
else start += 1;
}
} else if (start >= iterable.length) {
this._iterationObject.done = true;
this._iterationObject.value = void 0;
}
if(!this._iterationObject.done) {
this._iterationObject.value = iterable[start];
start += 1;
}
return this._iterationObject;
});
};
Iterator.cycle = function (cycle, times) {
var next;
if (arguments.length < 2)
times = Infinity;
//cycle = Iterator(cycle).toArray();
return new Iterator(function () {
var iteration, nextEntry;
if(next) {
nextEntry = next();
}
if(!next || nextEntry.done === true) {
if (times > 0) {
times--;
iteration = Iterator.iterate(cycle);
nextEntry = (next = iteration.next.bind(iteration))();
}
else {
this._iterationObject.done = true;
nextEntry = this._iterationObject; }
}
return nextEntry;
});
};
Iterator.concat = function (iterators) {
iterators = Iterator(iterators);
var next;
return new Iterator(function (){
var iteration, nextEntry;
if(next) nextEntry = next();
if(!nextEntry || nextEntry.done === true) {
nextEntry = iterators.next();
if(nextEntry.done === false) {
iteration = Iterator(nextEntry.value);
next = iteration.next.bind(iteration);
return next();
}
else {
return nextEntry;
}
}
else return nextEntry;
});
};
Iterator.unzip = function (iterators) {
iterators = Iterator(iterators).map(Iterator);
if (iterators.length === 0)
return new Iterator([]);
return new Iterator(function () {
var stopped, nextEntry;
var result = iterators.map(function (iterator) {
nextEntry = iterator.next();
if (nextEntry.done === true ) {
stopped = true;
}
return nextEntry.value;
});
if (stopped) {
this._iterationObject.done = true;
this._iterationObject.value = void 0;
}
else {
this._iterationObject.value = result;
}
return this._iterationObject;
});
};
Iterator.zip = function () {
return Iterator.unzip(
Array.prototype.slice.call(arguments)
);
};
Iterator.chain = function () {
return Iterator.concat(
Array.prototype.slice.call(arguments)
);
};
Iterator.range = function (start, stop, step) {
if (arguments.length < 3) {
step = 1;
}
if (arguments.length < 2) {
stop = start;
start = 0;
}
start = start || 0;
step = step || 1;
return new Iterator(function () {
if (start >= stop) {
this._iterationObject.done = true;
this._iterationObject.value = void 0;
}
var result = start;
start += step;
this._iterationObject.value = result;
return this._iterationObject;
});
};
Iterator.count = function (start, step) {
return Iterator.range(start, Infinity, step);
};
Iterator.repeat = function (value, times) {
return new Iterator.range(times).mapIterator(function () {
return value;
});
};