UNPKG

922 BJavaScriptView Raw
1var _ = require("./_");
2
3// Hack: we don't create new object to pass the newly iterated object.
4var $ArrIterContainer = {};
5
6var ArrIter = _.extendPrototype(function (arr) {
7 this.arr = arr;
8 this.len = arr.length;
9}, {
10 i: 0,
11 next: function () {
12 var self = this;
13 $ArrIterContainer.value = self.arr[self.i++];
14 $ArrIterContainer.done = self.i > self.len;
15 return $ArrIterContainer;
16 }
17});
18
19/**
20 * Generate a iterator
21 * @param {Any} obj
22 * @return {Function}
23 */
24function genIterator (obj) {
25 if (obj) {
26 var gen = obj[_.Promise.Symbol.iterator];
27 if (_.isFunction(gen)) {
28 return gen.call(obj);
29 }
30
31 if (obj instanceof Array) {
32 return new ArrIter(obj);
33 }
34
35 if (_.isFunction(obj.next)) {
36 return obj;
37 }
38 }
39 throw new TypeError("invalid_argument");
40}
41
42module.exports = genIterator;