summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2015-07-30 19:26:43 -0700
committerAlexander Early <alexander.early@gmail.com>2015-07-30 19:26:43 -0700
commit7cb31f809ae4b853b6912600ebcdf121ef3f0a78 (patch)
tree68ead9fba21e84cfe1a307e1de32663770e13909
parent1f97538586e09ce76168fa1ceb04288fd958b0a0 (diff)
parentf353d7ab30e27d22f2e1cd60c8e39cdedbb2d4c7 (diff)
downloadasync-7cb31f809ae4b853b6912600ebcdf121ef3f0a78.tar.gz
Merge pull request #866 from markyen/detect-limit
Add detectLimit
-rw-r--r--lib/async.js1
-rwxr-xr-xtest/test-async.js37
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/async.js b/lib/async.js
index 9c2b6af..2a4b1e1 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -467,6 +467,7 @@
}
async.detect = _createTester(async.eachOf, identity, _findGetResult);
async.detectSeries = _createTester(async.eachOfSeries, identity, _findGetResult);
+ async.detectLimit = _createTester(async.eachOfLimit, identity, _findGetResult);
async.sortBy = function (arr, iterator, callback) {
async.map(arr, function (x, callback) {
diff --git a/test/test-async.js b/test/test-async.js
index dc7d949..18894b0 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2329,6 +2329,43 @@ exports['detectSeries - ensure stop'] = function (test) {
});
};
+exports['detectLimit'] = function(test){
+ test.expect(2);
+ var call_order = [];
+ async.detectLimit([3, 2, 1], 2, detectIterator.bind(this, call_order), function(result) {
+ call_order.push('callback');
+ test.equals(result, 2);
+ });
+ setTimeout(function() {
+ test.same(call_order, [2, 'callback', 3]);
+ test.done();
+ }, 100);
+};
+
+exports['detectLimit - multiple matches'] = function(test){
+ test.expect(2);
+ var call_order = [];
+ async.detectLimit([3,2,2,1,2], 2, detectIterator.bind(this, call_order), function(result){
+ call_order.push('callback');
+ test.equals(result, 2);
+ });
+ setTimeout(function(){
+ test.same(call_order, [2, 'callback', 3]);
+ test.done();
+ }, 100);
+};
+
+exports['detectLimit - ensure stop'] = function (test) {
+ test.expect(1);
+ async.detectLimit([1, 2, 3, 4, 5], 2, function (num, cb) {
+ if (num > 4) throw new Error("detectLimit did not stop iterating");
+ cb(num === 3);
+ }, function (result) {
+ test.equals(result, 3);
+ test.done();
+ });
+};
+
exports['sortBy'] = function(test){
test.expect(2);