diff options
author | Graeme Yeates <yeatesgraeme@gmail.com> | 2015-07-03 11:27:42 -0400 |
---|---|---|
committer | Graeme Yeates <yeatesgraeme@gmail.com> | 2015-07-03 19:49:00 -0400 |
commit | 6ef3167ea5e37a0e9c9589102fb0a36f7eb37ba4 (patch) | |
tree | 061d66c3b0ba84708b7b15b5665f25c921aea3f3 | |
parent | e3f6537560e34cd8748214dc762bf18189e9fb54 (diff) | |
download | async-6ef3167ea5e37a0e9c9589102fb0a36f7eb37ba4.tar.gz |
Add filter and reject limit
-rw-r--r-- | lib/async.js | 12 | ||||
-rwxr-xr-x | test/test-async.js | 33 |
2 files changed, 40 insertions, 5 deletions
diff --git a/lib/async.js b/lib/async.js index f6ef7c1..cbd2678 100644 --- a/lib/async.js +++ b/lib/async.js @@ -335,8 +335,8 @@ return fn(async.eachOf, obj, iterator, callback); }; } - function doParallelLimit(limit, fn) { - return function (obj, iterator, callback) { + function doParallelLimit(fn) { + return function (obj, limit, iterator, callback) { return fn(_eachOfLimit(limit), obj, iterator, callback); }; } @@ -361,9 +361,7 @@ async.map = doParallel(_asyncMap); async.mapSeries = doSeries(_asyncMap); - async.mapLimit = function (arr, limit, iterator, callback) { - return doParallelLimit(limit, _asyncMap)(arr, iterator, callback); - }; + async.mapLimit = doParallelLimit(_asyncMap); // reduce only has a series version, as doing reduce in parallel won't // work in many situations. @@ -409,6 +407,9 @@ async.select = async.filter = doParallel(_filter); + async.selectLimit = + async.filterLimit = doParallelLimit(_filter); + async.selectSeries = async.filterSeries = doSeries(_filter); @@ -420,6 +421,7 @@ }, callback); } async.reject = doParallel(_reject); + async.rejectLimit = doParallelLimit(_reject); async.rejectSeries = doSeries(_reject); function _createTester(eachfn, check, getResult) { diff --git a/test/test-async.js b/test/test-async.js index 07b72e7..cd7a2a1 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2199,6 +2199,39 @@ exports['rejectSeries'] = function(test){ }); }; +function testLimit(test, arr, limitFunc, limit, iter, done) { + var args = []; + + limitFunc(arr, limit, function(x) { + args.push(x); + iter.apply(this, arguments); + }, function() { + test.same(args, arr); + if (done) done.apply(this, arguments); + else test.done(); + }); +} + +exports['rejectLimit'] = function(test) { + test.expect(2); + testLimit(test, [5, 4, 3, 2, 1], async.rejectLimit, 2, function(v, next) { + next(v % 2); + }, function(x) { + test.same(x, [4, 2]); + test.done(); + }); +}; + +exports['filterLimit'] = function(test) { + test.expect(2); + testLimit(test, [5, 4, 3, 2, 1], async.filterLimit, 2, function(v, next) { + next(v % 2); + }, function(x) { + test.same(x, [5, 3, 1]); + test.done(); + }); +}; + exports['some true'] = function(test){ test.expect(1); async.some([3,1,2], function(x, callback){ |