forked from leebyron/iterall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
359 lines (345 loc) · 11.7 KB
/
index.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
/**
* Copyright (c) 2016, Lee Byron
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @ignore
*/
/**
* [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterator)
* is a *protocol* which describes a standard way to produce a sequence of
* values, typically the values of the Iterable represented by this Iterator.
*
* While described by the [ES2015 version of JavaScript](http://www.ecma-international.org/ecma-262/6.0/#sec-iterator-interface)
* it can be utilized by any version of JavaScript.
*
* @typedef {Object} Iterator
* @template T The type of each iterated value
* @property {function (): { value: T, done: boolean }} next
* A method which produces either the next value in a sequence or a result
* where the `done` property is `true` indicating the end of the Iterator.
*/
/**
* [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable)
* is a *protocol* which when implemented allows a JavaScript object to define
* their iteration behavior, such as what values are looped over in a `for..of`
* loop or `iterall`'s `forEach` function. Many [built-in types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#Builtin_iterables)
* implement the Iterable protocol, including `Array` and `Map`.
*
* While described by the [ES2015 version of JavaScript](http://www.ecma-international.org/ecma-262/6.0/#sec-iterable-interface)
* it can be utilized by any version of JavaScript.
*
* @typedef {Object} Iterable
* @template T The type of each iterated value
* @property {function (): Iterator<T>} Symbol.iterator
* A method which produces an Iterator for this Iterable.
*/
// In ES2015 (or a polyfilled) environment, this will be Symbol.iterator
var SYMBOL_ITERATOR = typeof Symbol === 'function' && Symbol.iterator
/**
* A property name to be used as the name of an Iterable's method responsible
* for producing an Iterator, referred to as `@@iterator`. Typically represents
* the value `Symbol.iterator` but falls back to the string `"@@iterator"` when
* `Symbol.iterator` is not defined.
*
* Use `$$iterator` for defining new Iterables instead of `Symbol.iterator`,
* but do not use it for accessing existing Iterables, instead use
* `getIterator()` or `isIterable()`.
*
* @example
*
* var $$iterator = require('iterall').$$iterator
*
* function Counter (to) {
* this.to = to
* }
*
* Counter.prototype[$$iterator] = function () {
* return {
* to: this.to,
* num: 0,
* next () {
* if (this.num >= this.to) {
* return { value: undefined, done: true }
* }
* return { value: this.num++, done: false }
* }
* }
* }
*
* var counter = new Counter(3)
* for (var number of counter) {
* console.log(number) // 0 ... 1 ... 2
* }
*
* @type {Symbol|string}
*/
var $$iterator = SYMBOL_ITERATOR || '@@iterator'
exports.$$iterator = $$iterator
/**
* Returns true if the provided object implements the Iterator protocol via
* either implementing a `Symbol.iterator` or `"@@iterator"` method.
*
* @example
*
* var isIterable = require('iterall').isIterable
* isIterable([ 1, 2, 3 ]) // true
* isIterable('ABC') // true
* isIterable({ length: 1, 0: 'Alpha' }) // false
* isIterable({ key: 'value' }) // false
* isIterable(new Map()) // true
*
* @param obj
* A value which might implement the Iterable protocol.
* @return {boolean} true if Iterable.
*/
function isIterable (obj) {
return !!getIteratorMethod(obj)
}
exports.isIterable = isIterable
/**
* Returns true if the provided object implements the Array-like protocol via
* defining a positive-integer `length` property.
*
* @example
*
* var isArrayLike = require('iterall').isArrayLike
* isArrayLike([ 1, 2, 3 ]) // true
* isArrayLike('ABC') // true
* isArrayLike({ length: 1, 0: 'Alpha' }) // true
* isArrayLike({ key: 'value' }) // false
* isArrayLike(new Map()) // false
*
* @param obj
* A value which might implement the Array-like protocol.
* @return {boolean} true if Array-like.
*/
function isArrayLike (obj) {
var length = obj != null && obj.length
return typeof length === 'number' && length >= 0 && length % 1 === 0
}
exports.isArrayLike = isArrayLike
/**
* Returns true if the provided object is an Object (i.e. not a string literal)
* and is either Iterable or Array-like.
*
* This may be used in place of [Array.isArray()][isArray] to determine if an
* object should be iterated-over. It always excludes string literals and
* includes Arrays (regardless of if it is Iterable). It also includes other
* Array-like objects such as NodeList, TypedArray, and Buffer.
*
* @example
*
* var isCollection = require('iterall').isCollection
* isCollection([ 1, 2, 3 ]) // true
* isCollection('ABC') // false
* isCollection({ length: 1, 0: 'Alpha' }) // true
* isCollection({ key: 'value' }) // false
* isCollection(new Map()) // true
*
* @example
*
* var forEach = require('iterall').forEach
* if (isCollection(obj)) {
* forEach(obj, function (value) {
* console.log(value)
* })
* }
*
* @param obj
* An Object value which might implement the Iterable or Array-like protocols.
* @return {boolean} true if Iterable or Array-like Object.
*/
function isCollection (obj) {
return Object(obj) === obj && (isArrayLike(obj) || isIterable(obj))
}
exports.isCollection = isCollection
/**
* If the provided object implements the Iterator protocol, its Iterator object
* is returned. Otherwise returns undefined.
*
* @example
*
* var getIterator = require('iterall').getIterator
* var iterator = getIterator([ 1, 2, 3 ])
* iterator.next() // { value: 1, done: false }
* iterator.next() // { value: 2, done: false }
* iterator.next() // { value: 3, done: false }
* iterator.next() // { value: undefined, done: true }
*
* @template T the type of each iterated value
* @param {Iterable<T>} iterable
* An Iterable object which is the source of an Iterator.
* @return {Iterator<T>} new Iterator instance.
*/
function getIterator (iterable) {
var method = getIteratorMethod(iterable)
if (method) {
return method.call(iterable)
}
}
exports.getIterator = getIterator
/**
* If the provided object implements the Iterator protocol, the method
* responsible for producing its Iterator object is returned.
*
* This is used in rare cases for performance tuning. This method must be called
* with obj as the contextual this-argument.
*
* @example
*
* var getIteratorMethod = require('iterall').getIteratorMethod
* var myArray = [ 1, 2, 3 ]
* var method = getIteratorMethod(myArray)
* if (method) {
* var iterator = method.call(myArray)
* }
*
* @template T the type of each iterated value
* @param {Iterable<T>} iterable
* An Iterable object which defines an `@@iterator` method.
* @return {function(): Iterator<T>} `@@iterator` method.
*/
function getIteratorMethod (iterable) {
if (iterable != null) {
var method = SYMBOL_ITERATOR && iterable[SYMBOL_ITERATOR] || iterable['@@iterator']
if (typeof method === 'function') {
return method
}
}
}
exports.getIteratorMethod = getIteratorMethod
/**
* Given an object which either implements the Iterable protocol or is
* Array-like, iterate over it, calling the `callback` at each iteration.
*
* Use `forEach` where you would expect to use a `for ... of` loop in ES6.
* However `forEach` adheres to the behavior of [Array#forEach][] described in
* the ECMAScript specification, skipping over "holes" in Array-likes. It will
* also delegate to a `forEach` method on `collection` if one is defined,
* ensuring native performance for `Arrays`.
*
* Similar to [Array#forEach][], the `callback` function accepts three
* arguments, and is provided with `thisArg` as the calling context.
*
* Note: providing an infinite Iterator to forEach will produce an error.
*
* [Array#forEach]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
*
* @example
*
* var forEach = require('iterall').forEach
*
* forEach(myIterable, function (value, index, iterable) {
* console.log(value, index, iterable === myIterable)
* })
*
* @example
*
* // ES6:
* for (let value of myIterable) {
* console.log(value)
* }
*
* // Any JavaScript environment:
* forEach(myIterable, function (value) {
* console.log(value)
* })
*
* @template T the type of each iterated value
* @param {Iterable<T>|{ length: number }} collection
* The Iterable or array to iterate over.
* @param {function(T, number, object)} callback
* Function to execute for each iteration, taking up to three arguments
* @param [thisArg]
* Optional. Value to use as `this` when executing `callback`.
*/
function forEach (collection, callback, thisArg) {
if (collection != null) {
if (typeof collection.forEach === 'function') {
return collection.forEach(callback, thisArg)
}
var i = 0
var iterator = getIterator(collection)
if (iterator) {
var step
while (!(step = iterator.next()).done) {
callback.call(thisArg, step.value, i++, collection)
// Infinite Iterators could cause forEach to run forever.
// After a very large number of iterations, produce an error.
/* istanbul ignore if */
if (i > 9999999) {
throw new TypeError('Near-infinite iteration.')
}
}
} else if (isArrayLike(collection)) {
for (; i < collection.length; i++) {
if (collection.hasOwnProperty(i)) {
callback.call(thisArg, collection[i], i, collection)
}
}
}
}
}
exports.forEach = forEach
/**
* Similar to `getIterator()`, this method returns a new Iterator given an
* Iterable. However it will also create an Iterator for a non-Iterable
* Array-like collection, such as Array in a non-ES2015 environment.
*
* `createIterator` is complimentary to `forEach`, but allows a "pull"-based
* iteration as opposed to `forEach`'s "push"-based iteration.
*
* `createIterator` produces an Iterator for Array-likes with the same behavior
* as ArrayIteratorPrototype described in the ECMAScript specification, and
* does *not* skip over "holes".
*
* @example
*
* var createIterator = require('iterall').createIterator
*
* var myArraylike = { length: 3, 0: 'Alpha', 1: 'Bravo', 2: 'Charlie' }
* var iterator = createIterator(myArraylike)
* iterator.next() // { value: 'Alpha', done: false }
* iterator.next() // { value: 'Bravo', done: false }
* iterator.next() // { value: 'Charlie', done: false }
* iterator.next() // { value: undefined, done: true }
*
* @template T the type of each iterated value
* @param {Iterable<T>|{ length: number }} collection
* An Iterable or Array-like object to produce an Iterator.
* @return {Iterator<T>} new Iterator instance.
*/
function createIterator (collection) {
if (collection != null) {
var iterator = getIterator(collection)
if (iterator) {
return iterator
}
if (isArrayLike(collection)) {
return new ArrayLikeIterator(collection)
}
}
}
exports.createIterator = createIterator
// When the object provided to `createIterator` is not Iterable but is
// Array-like, this simple Iterator is created.
function ArrayLikeIterator (obj) {
this._o = obj
this._i = 0
}
// Note: all Iterators are themselves Iterable.
ArrayLikeIterator.prototype[$$iterator] = function () {
return this
}
// A simple state-machine determines the IteratorResult returned, yielding
// each value in the Array-like object in order of their indicies.
ArrayLikeIterator.prototype.next = function () {
if (this._o === void 0 || this._i >= this._o.length) {
this._o = void 0
return { value: void 0, done: true }
}
return { value: this._o[this._i++], done: false }
}