|
| 1 | +import isArrayLike from 'lodash/isArrayLike'; |
| 2 | + |
1 | 3 | import eachOfLimit from './eachOfLimit';
|
2 | 4 | import doLimit from './internal/doLimit';
|
| 5 | +import noop from 'lodash/noop'; |
| 6 | +import once from 'lodash/once'; |
| 7 | +import onlyOnce from './internal/onlyOnce'; |
| 8 | + |
| 9 | +// eachOf implementation optimized for array-likes |
| 10 | +function eachOfArrayLike(coll, iteratee, callback) { |
| 11 | + callback = once(callback || noop); |
| 12 | + var index = 0, |
| 13 | + completed = 0, |
| 14 | + length = coll.length; |
| 15 | + if (length === 0) { |
| 16 | + callback(null); |
| 17 | + } |
| 18 | + |
| 19 | + function iteratorCallback(err) { |
| 20 | + if (err) { |
| 21 | + callback(err); |
| 22 | + } else if (++completed === length) { |
| 23 | + callback(null); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + for (; index < length; index++) { |
| 28 | + iteratee(coll[index], index, onlyOnce(iteratorCallback)); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// a generic version of eachOf which can handle array, object, and iterator cases. |
| 33 | +var eachOfGeneric = doLimit(eachOfLimit, Infinity); |
3 | 34 |
|
4 | 35 | /**
|
5 | 36 | * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
|
@@ -42,4 +73,7 @@ import doLimit from './internal/doLimit';
|
42 | 73 | * doSomethingWith(configs);
|
43 | 74 | * });
|
44 | 75 | */
|
45 |
| -export default doLimit(eachOfLimit, Infinity); |
| 76 | +export default function(coll, iteratee, callback) { |
| 77 | + var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric; |
| 78 | + eachOfImplementation(coll, iteratee, callback); |
| 79 | +} |
0 commit comments