diff options
Diffstat (limited to 'lib/eachOf.js')
-rw-r--r-- | lib/eachOf.js | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/eachOf.js b/lib/eachOf.js index 4d453c5..43accc7 100644 --- a/lib/eachOf.js +++ b/lib/eachOf.js @@ -1,5 +1,36 @@ +import isArrayLike from 'lodash/isArrayLike'; + import eachOfLimit from './eachOfLimit'; import doLimit from './internal/doLimit'; +import noop from 'lodash/noop'; +import once from 'lodash/once'; +import onlyOnce from './internal/onlyOnce'; + +// eachOf implementation optimized for array-likes +function eachOfArrayLike(coll, iteratee, callback) { + callback = once(callback || noop); + var index = 0, + completed = 0, + length = coll.length; + if (length === 0) { + callback(null); + } + + function iteratorCallback(err) { + if (err) { + callback(err); + } else if (++completed === length) { + callback(null); + } + } + + for (; index < length; index++) { + iteratee(coll[index], index, onlyOnce(iteratorCallback)); + } +} + +// a generic version of eachOf which can handle array, object, and iterator cases. +var eachOfGeneric = doLimit(eachOfLimit, Infinity); /** * 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'; * doSomethingWith(configs); * }); */ -export default doLimit(eachOfLimit, Infinity); +export default function(coll, iteratee, callback) { + var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric; + eachOfImplementation(coll, iteratee, callback); +} |