|
| 1 | +// @flow strict |
| 2 | + |
| 3 | +const SYMBOL = typeof Symbol === 'function' ? Symbol : undefined; |
| 4 | +const SYMBOL_ITERATOR = SYMBOL && SYMBOL.iterator; |
| 5 | +const SYMBOL_ASYNC_ITERATOR = SYMBOL && SYMBOL.asyncIterator; |
| 6 | + |
| 7 | +function AsyncFromSyncIterator(iterator) { |
| 8 | + this._i = iterator; |
| 9 | +} |
| 10 | + |
| 11 | +AsyncFromSyncIterator.prototype[$$asyncIterator] = function() { |
| 12 | + return this; |
| 13 | +}; |
| 14 | + |
| 15 | +AsyncFromSyncIterator.prototype.next = function() { |
| 16 | + const step = this._i.next(); |
| 17 | + return Promise.resolve(step.value).then(value => ({ |
| 18 | + value, |
| 19 | + done: step.done, |
| 20 | + })); |
| 21 | +}; |
| 22 | + |
| 23 | +function ArrayLikeIterator(obj) { |
| 24 | + this._o = obj; |
| 25 | + this._i = 0; |
| 26 | +} |
| 27 | + |
| 28 | +ArrayLikeIterator.prototype[$$iterator] = function() { |
| 29 | + return this; |
| 30 | +}; |
| 31 | + |
| 32 | +ArrayLikeIterator.prototype.next = function() { |
| 33 | + if (this._o === undefined || this._i >= this._o.length) { |
| 34 | + this._o = undefined; |
| 35 | + return { value: undefined, done: true }; |
| 36 | + } |
| 37 | + return { value: this._o[this._i++], done: false }; |
| 38 | +}; |
| 39 | + |
| 40 | +export const $$iterator = SYMBOL_ITERATOR || '@@iterator'; |
| 41 | + |
| 42 | +export function isIterable(obj) { |
| 43 | + return Boolean(getIteratorMethod(obj)); |
| 44 | +} |
| 45 | + |
| 46 | +export function isArrayLike(obj) { |
| 47 | + const length = obj != null && obj.length; |
| 48 | + return typeof length === 'number' && length >= 0 && length % 1 === 0; |
| 49 | +} |
| 50 | + |
| 51 | +export function isCollection(obj) { |
| 52 | + return Object(obj) === obj && (isArrayLike(obj) || isIterable(obj)); |
| 53 | +} |
| 54 | + |
| 55 | +export function getIterator(iterable) { |
| 56 | + const method = getIteratorMethod(iterable); |
| 57 | + if (method) { |
| 58 | + return method.call(iterable); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export function getIteratorMethod(iterable) { |
| 63 | + if (iterable != null) { |
| 64 | + const method = |
| 65 | + (SYMBOL_ITERATOR && iterable[SYMBOL_ITERATOR]) || iterable['@@iterator']; |
| 66 | + if (typeof method === 'function') { |
| 67 | + return method; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export function createIterator(collection) { |
| 73 | + if (collection != null) { |
| 74 | + const iterator = getIterator(collection); |
| 75 | + if (iterator) { |
| 76 | + return iterator; |
| 77 | + } |
| 78 | + if (isArrayLike(collection)) { |
| 79 | + return new ArrayLikeIterator(collection); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export function forEach(collection, callback, thisArg) { |
| 85 | + if (collection != null) { |
| 86 | + if (typeof collection.forEach === 'function') { |
| 87 | + return collection.forEach(callback, thisArg); |
| 88 | + } |
| 89 | + let i = 0; |
| 90 | + const iterator = getIterator(collection); |
| 91 | + if (iterator) { |
| 92 | + let step; |
| 93 | + while (!(step = iterator.next()).done) { |
| 94 | + callback.call(thisArg, step.value, i++, collection); |
| 95 | + if (i > 9999999) { |
| 96 | + throw new TypeError('Near-infinite iteration.'); |
| 97 | + } |
| 98 | + } |
| 99 | + } else if (isArrayLike(collection)) { |
| 100 | + for (; i < collection.length; i++) { |
| 101 | + if (Object.prototype.hasOwnProperty.call(collection, i)) { |
| 102 | + callback.call(thisArg, collection[i], i, collection); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export const $$asyncIterator = SYMBOL_ASYNC_ITERATOR || '@@asyncIterator'; |
| 110 | + |
| 111 | +export const isAsyncIterable = obj => Boolean(getAsyncIteratorMethod(obj)); |
| 112 | + |
| 113 | +export const getAsyncIterator = asyncIterable => { |
| 114 | + const method = getAsyncIteratorMethod(asyncIterable); |
| 115 | + if (method) { |
| 116 | + return method.call(asyncIterable); |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +export const getAsyncIteratorMethod = asyncIterable => { |
| 121 | + if (asyncIterable != null) { |
| 122 | + const method = |
| 123 | + (SYMBOL_ASYNC_ITERATOR && asyncIterable[SYMBOL_ASYNC_ITERATOR]) || |
| 124 | + asyncIterable['@@asyncIterator']; |
| 125 | + if (typeof method === 'function') { |
| 126 | + return method; |
| 127 | + } |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +export const createAsyncIterator = source => { |
| 132 | + if (source != null) { |
| 133 | + const asyncIterator = getAsyncIterator(source); |
| 134 | + if (asyncIterator) { |
| 135 | + return asyncIterator; |
| 136 | + } |
| 137 | + const iterator = createIterator(source); |
| 138 | + if (iterator) { |
| 139 | + return new AsyncFromSyncIterator(iterator); |
| 140 | + } |
| 141 | + } |
| 142 | +}; |
| 143 | + |
| 144 | +export const forAwaitEach = (source, callback, thisArg) => { |
| 145 | + const asyncIterator = createAsyncIterator(source); |
| 146 | + if (asyncIterator) { |
| 147 | + let i = 0; |
| 148 | + return new Promise((resolve, reject) => { |
| 149 | + function next() { |
| 150 | + asyncIterator |
| 151 | + .next() |
| 152 | + .then(step => { |
| 153 | + if (!step.done) { |
| 154 | + Promise.resolve(callback.call(thisArg, step.value, i++, source)) |
| 155 | + .then(next) |
| 156 | + .catch(reject); |
| 157 | + } else { |
| 158 | + resolve(); |
| 159 | + } |
| 160 | + return null; |
| 161 | + }) |
| 162 | + .catch(reject); |
| 163 | + return null; |
| 164 | + } |
| 165 | + next(); |
| 166 | + }); |
| 167 | + } |
| 168 | +}; |
0 commit comments