diff --git a/lib/each.js b/lib/each.js index dc8c5019b..5bf6bd4d6 100644 --- a/lib/each.js +++ b/lib/each.js @@ -1,5 +1,5 @@ -import eachLimit from './eachLimit'; -import doLimit from './internal/doLimit'; +import eachOf from './eachOf'; +import withoutIndex from './internal/withoutIndex'; /** * Applies the function `iteratee` to each item in `coll`, in parallel. @@ -60,4 +60,6 @@ import doLimit from './internal/doLimit'; * } * }); */ -export default doLimit(eachLimit, Infinity); +export default function eachLimit(coll, iteratee, callback) { + eachOf(coll, withoutIndex(iteratee), callback); +} diff --git a/lib/eachOf.js b/lib/eachOf.js index 4d453c56e..43accc7fe 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); +} diff --git a/lib/every.js b/lib/every.js index 8b2ad0541..83307184a 100644 --- a/lib/every.js +++ b/lib/every.js @@ -1,5 +1,6 @@ -import everyLimit from './everyLimit'; -import doLimit from './internal/doLimit'; +import createTester from './internal/createTester'; +import eachOf from './eachOf'; +import notId from './internal/notId'; /** * Returns `true` if every element in `coll` satisfies an async test. If any @@ -29,4 +30,4 @@ import doLimit from './internal/doLimit'; * // if result is true then every file exists * }); */ -export default doLimit(everyLimit, Infinity); +export default createTester(eachOf, notId, notId); diff --git a/lib/filter.js b/lib/filter.js index e6d3010da..4f0cbf183 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -1,5 +1,5 @@ -import filterLimit from './filterLimit'; -import doLimit from './internal/doLimit'; +import filter from './internal/filter'; +import doParallel from './internal/doParallel'; /** * Returns a new array of all the values in `coll` which pass an async truth @@ -28,4 +28,4 @@ import doLimit from './internal/doLimit'; * // results now equals an array of the existing files * }); */ -export default doLimit(filterLimit, Infinity); +export default doParallel(filter); diff --git a/lib/map.js b/lib/map.js index eecab03b1..4c0b22685 100644 --- a/lib/map.js +++ b/lib/map.js @@ -1,5 +1,5 @@ -import mapLimit from './mapLimit'; -import doLimit from './internal/doLimit'; +import doParallel from './internal/doParallel'; +import map from './internal/map'; /** * Produces a new collection of values by mapping each value in `coll` through @@ -37,4 +37,4 @@ import doLimit from './internal/doLimit'; * // results is now an array of stats for each file * }); */ -export default doLimit(mapLimit, Infinity); +export default doParallel(map); diff --git a/lib/parallel.js b/lib/parallel.js index 67d618014..506f47545 100644 --- a/lib/parallel.js +++ b/lib/parallel.js @@ -1,5 +1,5 @@ -import parallelLimit from './parallelLimit'; -import doLimit from './internal/doLimit'; +import eachOf from './eachOf'; +import parallel from './internal/parallel'; /** * Run the `tasks` collection of functions in parallel, without waiting until @@ -67,4 +67,6 @@ import doLimit from './internal/doLimit'; * // results is now equals to: {one: 1, two: 2} * }); */ -export default doLimit(parallelLimit, Infinity); +export default function parallelLimit(tasks, callback) { + parallel(eachOf, tasks, callback); +} diff --git a/lib/reject.js b/lib/reject.js index 860761456..0ddbaf0ac 100644 --- a/lib/reject.js +++ b/lib/reject.js @@ -1,5 +1,5 @@ -import rejectLimit from './rejectLimit'; -import doLimit from './internal/doLimit'; +import reject from './internal/reject'; +import doParallel from './internal/doParallel'; /** * The opposite of [`filter`]{@link module:Collections.filter}. Removes values that pass an `async` truth test. @@ -27,4 +27,4 @@ import doLimit from './internal/doLimit'; * createFiles(results); * }); */ -export default doLimit(rejectLimit, Infinity); +export default doParallel(reject); diff --git a/lib/some.js b/lib/some.js index 7b5341c33..fd1780f35 100644 --- a/lib/some.js +++ b/lib/some.js @@ -1,5 +1,6 @@ -import someLimit from './someLimit'; -import doLimit from './internal/doLimit'; +import createTester from './internal/createTester'; +import eachOf from './eachOf'; +import identity from 'lodash/identity'; /** * Returns `true` if at least one element in the `coll` satisfies an async test. @@ -31,4 +32,4 @@ import doLimit from './internal/doLimit'; * // if result is true then at least one of the files exists * }); */ -export default doLimit(someLimit, Infinity); +export default createTester(eachOf, Boolean, identity);