From acd656ddfda189fadfe4505f2b9e3eae6512f4dc Mon Sep 17 00:00:00 2001 From: charlierudolph Date: Tue, 1 Mar 2016 08:40:20 -0800 Subject: [PATCH] implement and Series with Limit --- lib/each.js | 8 +++----- lib/eachOf.js | 34 +++------------------------------- lib/eachOfSeries.js | 40 +++------------------------------------- lib/eachSeries.js | 8 +++----- lib/every.js | 7 +++---- lib/filter.js | 6 +++--- lib/filterSeries.js | 6 +++--- lib/internal/doLimit.js | 7 +++++++ lib/map.js | 6 +++--- lib/mapSeries.js | 6 +++--- lib/parallel.js | 8 +++----- lib/reject.js | 6 +++--- lib/rejectSeries.js | 6 +++--- lib/some.js | 8 +++----- lib/times.js | 8 +++----- lib/timesSeries.js | 8 +++----- test/test-async.js | 6 ++++-- 17 files changed, 56 insertions(+), 122 deletions(-) create mode 100644 lib/internal/doLimit.js diff --git a/lib/each.js b/lib/each.js index 384bb0a89..d81763adb 100644 --- a/lib/each.js +++ b/lib/each.js @@ -1,8 +1,6 @@ 'use strict'; -import eachOf from './eachOf'; -import withoutIndex from './internal/withoutIndex'; +import eachLimit from './eachLimit'; +import doLimit from './internal/doLimit'; -export default function each(arr, iterator, cb) { - return eachOf(arr, withoutIndex(iterator), cb); -} +export default doLimit(eachLimit, Infinity); diff --git a/lib/eachOf.js b/lib/eachOf.js index c370bbb1b..d16e1b33d 100644 --- a/lib/eachOf.js +++ b/lib/eachOf.js @@ -1,34 +1,6 @@ 'use strict'; -import once from 'lodash/once'; -import noop from 'lodash/noop'; +import eachOfLimit from './eachOfLimit'; +import doLimit from './internal/doLimit'; -import keyIterator from './internal/keyIterator'; -import onlyOnce from './internal/onlyOnce'; - -export default function eachOf(object, iterator, callback) { - callback = once(callback || noop); - object = object || []; - - var iter = keyIterator(object); - var key, completed = 0; - - while ((key = iter()) != null) { - completed += 1; - iterator(object[key], key, onlyOnce(done)); - } - - if (completed === 0) callback(null); - - function done(err) { - completed--; - if (err) { - callback(err); - } - // Check key is null in case iterator isn't exhausted - // and done resolved synchronously. - else if (key === null && completed <= 0) { - callback(null); - } - } -} +export default doLimit(eachOfLimit, Infinity); diff --git a/lib/eachOfSeries.js b/lib/eachOfSeries.js index bcfa0296d..47787bb1c 100644 --- a/lib/eachOfSeries.js +++ b/lib/eachOfSeries.js @@ -1,40 +1,6 @@ 'use strict'; -import once from 'lodash/once'; -import noop from 'lodash/noop'; +import eachOfLimit from './eachOfLimit'; +import doLimit from './internal/doLimit'; -import keyIterator from './internal/keyIterator'; -import onlyOnce from './internal/onlyOnce'; -import setImmediate from './setImmediate'; - -export default function eachOfSeries(obj, iterator, callback) { - callback = once(callback || noop); - obj = obj || []; - var nextKey = keyIterator(obj); - var key = nextKey(); - - function iterate() { - var sync = true; - if (key === null) { - return callback(null); - } - iterator(obj[key], key, onlyOnce(function(err) { - if (err) { - callback(err); - } else { - key = nextKey(); - if (key === null) { - return callback(null); - } else { - if (sync) { - setImmediate(iterate); - } else { - iterate(); - } - } - } - })); - sync = false; - } - iterate(); -} +export default doLimit(eachOfLimit, 1); diff --git a/lib/eachSeries.js b/lib/eachSeries.js index fd08c0407..39dc81fa0 100644 --- a/lib/eachSeries.js +++ b/lib/eachSeries.js @@ -1,8 +1,6 @@ 'use strict'; -import eachOfSeries from './eachOfSeries'; -import withoutIndex from './internal/withoutIndex'; +import eachLimit from './eachLimit'; +import doLimit from './internal/doLimit'; -export default function eachSeries(arr, iterator, cb) { - return eachOfSeries(arr, withoutIndex(iterator), cb); -} +export default doLimit(eachLimit, 1); diff --git a/lib/every.js b/lib/every.js index 7ee371376..b2d2abd04 100644 --- a/lib/every.js +++ b/lib/every.js @@ -1,7 +1,6 @@ 'use strict'; -import createTester from './internal/createTester'; -import eachOf from './eachOf'; -import notId from './internal/notId'; +import everyLimit from './everyLimit'; +import doLimit from './internal/doLimit'; -export default createTester(eachOf, notId, notId); +export default doLimit(everyLimit, Infinity); diff --git a/lib/filter.js b/lib/filter.js index 36af2b1c8..f3813d496 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -1,6 +1,6 @@ 'use strict'; -import filter from './internal/filter'; -import doParallel from './internal/doParallel'; +import filterLimit from './filterLimit'; +import doLimit from './internal/doLimit'; -export default doParallel(filter); +export default doLimit(filterLimit, Infinity); diff --git a/lib/filterSeries.js b/lib/filterSeries.js index 861ca8b20..d3995c9bc 100644 --- a/lib/filterSeries.js +++ b/lib/filterSeries.js @@ -1,6 +1,6 @@ 'use strict'; -import filter from './internal/filter'; -import doSeries from './internal/doSeries'; +import filterLimit from './filterLimit'; +import doLimit from './internal/doLimit'; -export default doSeries(filter); +export default doLimit(filterLimit, 1); diff --git a/lib/internal/doLimit.js b/lib/internal/doLimit.js new file mode 100644 index 000000000..16bfa42d3 --- /dev/null +++ b/lib/internal/doLimit.js @@ -0,0 +1,7 @@ +'use strict'; + +export default function doLimit(fn, limit) { + return function (iterable, iterator, callback) { + return fn(iterable, limit, iterator, callback); + }; +} diff --git a/lib/map.js b/lib/map.js index 2a8449c25..cb3f56d94 100644 --- a/lib/map.js +++ b/lib/map.js @@ -1,6 +1,6 @@ 'use strict'; -import doParallel from './internal/doParallel'; -import map from './internal/map'; +import mapLimit from './mapLimit'; +import doLimit from './internal/doLimit'; -export default doParallel(map); +export default doLimit(mapLimit, Infinity); diff --git a/lib/mapSeries.js b/lib/mapSeries.js index bfcdaa23c..9a6bd1146 100644 --- a/lib/mapSeries.js +++ b/lib/mapSeries.js @@ -1,6 +1,6 @@ 'use strict'; -import map from './internal/map'; -import doSeries from './internal/doSeries'; +import mapLimit from './mapLimit'; +import doLimit from './internal/doLimit'; -export default doSeries(map); +export default doLimit(mapLimit, 1); diff --git a/lib/parallel.js b/lib/parallel.js index d3eec16a4..6114d6ae3 100644 --- a/lib/parallel.js +++ b/lib/parallel.js @@ -1,8 +1,6 @@ 'use strict'; -import _parallel from './internal/parallel'; -import eachOf from './eachOf'; +import parallelLimit from './parallelLimit'; +import doLimit from './internal/doLimit'; -export default function parallel(tasks, cb) { - return _parallel(eachOf, tasks, cb); -} +export default doLimit(parallelLimit, Infinity); diff --git a/lib/reject.js b/lib/reject.js index fb5b87f3f..970a0ecb3 100644 --- a/lib/reject.js +++ b/lib/reject.js @@ -1,6 +1,6 @@ 'use strict'; -import reject from './internal/reject'; -import doParallel from './internal/doParallel'; +import rejectLimit from './rejectLimit'; +import doLimit from './internal/doLimit'; -export default doParallel(reject); +export default doLimit(rejectLimit, Infinity); diff --git a/lib/rejectSeries.js b/lib/rejectSeries.js index e02adeadd..00c145fa6 100644 --- a/lib/rejectSeries.js +++ b/lib/rejectSeries.js @@ -1,6 +1,6 @@ 'use strict'; -import reject from './internal/reject'; -import doSeries from './internal/doSeries'; +import rejectLimit from './rejectLimit'; +import doLimit from './internal/doLimit'; -export default doSeries(reject); +export default doLimit(rejectLimit, 1); diff --git a/lib/some.js b/lib/some.js index 23b884b9a..000f2e211 100644 --- a/lib/some.js +++ b/lib/some.js @@ -1,8 +1,6 @@ 'use strict'; -import identity from 'lodash/identity'; +import someLimit from './someLimit'; +import doLimit from './internal/doLimit'; -import createTester from './internal/createTester'; -import eachOf from './eachOf'; - -export default createTester(eachOf, Boolean, identity); +export default doLimit(someLimit, Infinity); diff --git a/lib/times.js b/lib/times.js index 72e844c88..2cbd230ac 100644 --- a/lib/times.js +++ b/lib/times.js @@ -1,8 +1,6 @@ 'use strict'; -import map from './map'; -import range from 'lodash/_baseRange'; +import timesLimit from './timesLimit'; +import doLimit from './internal/doLimit'; -export default function (count, iterator, callback) { - map(range(0, count, 1), iterator, callback); -} +export default doLimit(timesLimit, Infinity); diff --git a/lib/timesSeries.js b/lib/timesSeries.js index 792727066..af2219bfc 100644 --- a/lib/timesSeries.js +++ b/lib/timesSeries.js @@ -1,8 +1,6 @@ 'use strict'; -import mapSeries from './mapSeries'; -import range from 'lodash/_baseRange'; +import timesLimit from './timesLimit'; +import doLimit from './internal/doLimit'; -export default function (count, iterator, callback) { - mapSeries(range(0, count, 1), iterator, callback); -} +export default doLimit(timesLimit, 1); diff --git a/test/test-async.js b/test/test-async.js index 8eaa06fd5..bd103bfc2 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -998,9 +998,10 @@ exports['each extra callback'] = function(test){ var count = 0; async.each([1,3,2], function(val, callback) { count++; + var done = count == 3; callback(); test.throws(callback); - if (count == 3) { + if (done) { test.done(); } }); @@ -1521,8 +1522,9 @@ exports['map'] = { var r = []; async.map(a, function(x, callback){ r.push(x); + var done = r.length == a.length; callback(null); - if (r.length >= a.length) { + if (done) { test.same(r, a); test.done(); }