Skip to content

Commit

Permalink
Add filter and reject limit
Browse files Browse the repository at this point in the history
  • Loading branch information
megawac committed Jul 3, 2015
1 parent cd4d65d commit 651f301
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,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);
};
}
Expand All @@ -377,9 +377,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.
Expand Down Expand Up @@ -425,6 +423,9 @@
async.select =
async.filter = doParallel(_filter);

async.selectLimit =
async.filterLimit = doParallelLimit(_filter);

async.selectSeries =
async.filterSeries = doSeries(_filter);

Expand All @@ -436,6 +437,7 @@
}, callback);
}
async.reject = doParallel(_reject);
async.rejectLimit = doParallelLimit(_reject);
async.rejectSeries = doSeries(_reject);

function _createTester(eachfn, check, getResult) {
Expand Down
33 changes: 33 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

0 comments on commit 651f301

Please sign in to comment.