[Pkg-javascript-commits] [node-async] 25/480: better support empty objects and arrays as arguments

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:08 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-async.

commit c682809128a6f39b0cc79f7aaae2d97c903d188c
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Sat Jun 12 12:52:54 2010 +0100

    better support empty objects and arrays as arguments
---
 lib/async.js       |  5 ++++-
 test/test-async.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/lib/async.js b/lib/async.js
index 7f216b1..58213bb 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -2,6 +2,7 @@ var events = require('events');
 
 
 exports.forEach = function(arr, iterator, callback){
+    if(!arr.length) return callback();
     var completed = 0;
     arr.forEach(function(x){
         iterator(x, function(err){
@@ -18,6 +19,7 @@ exports.forEach = function(arr, iterator, callback){
 };
 
 exports.forEachSeries = function(arr, iterator, callback){
+    if(!arr.length) return callback();
     var completed = 0;
     var iterate = function(){
         iterator(arr[completed], function(err){
@@ -179,6 +181,7 @@ exports.auto = function(tasks, callback){
 };
 
 exports.waterfall = function(tasks, callback){
+    if(!tasks.length) return callback();
     callback = callback || function(){};
     var wrapIterator = function(iterator){
         return function(err){
@@ -223,7 +226,7 @@ exports.series = function(tasks, callback){
 exports.iterator = function(tasks){
     var makeCallback = function(index){
         var fn = function(){
-            tasks[index].apply(null, arguments);
+            if(tasks.length) tasks[index].apply(null, arguments);
             return fn.next();
         }
         fn.next = function(){
diff --git a/test/test-async.js b/test/test-async.js
index 1191305..6387fca 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -32,6 +32,12 @@ exports['auto'] = function(test){
     });
 };
 
+exports['auto empty object'] = function(test){
+    async.auto({}, function(err){
+        test.done();
+    });
+};
+
 exports['auto error'] = function(test){
     test.expect(1);
     async.auto({
@@ -90,6 +96,12 @@ exports['waterfall'] = function(test){
     });
 };
 
+exports['waterfall empty array'] = function(test){
+    async.waterfall([], function(err){
+        test.done();
+    });
+};
+
 exports['waterfall no callback'] = function(test){
     async.waterfall([
         function(callback){callback();},
@@ -192,6 +204,14 @@ exports['parallel'] = function(test){
     });
 };
 
+exports['parallel empty array'] = function(test){
+    async.parallel([], function(err, results){
+        test.equals(err, null);
+        test.same(results, []);
+        test.done();
+    });
+};
+
 exports['parallel error'] = function(test){
     async.parallel([
         function(callback){
@@ -244,6 +264,14 @@ exports['series'] = function(test){
     });
 };
 
+exports['series empty array'] = function(test){
+    async.series([], function(err, results){
+        test.equals(err, null);
+        test.same(results, []);
+        test.done();
+    });
+};
+
 exports['series error'] = function(test){
     test.expect(1);
     async.series([
@@ -294,6 +322,13 @@ exports['iterator'] = function(test){
     test.done();
 };
 
+exports['iterator empty array'] = function(test){
+    var iterator = async.iterator([]);
+    test.equals(iterator(), undefined);
+    test.equals(iterator.next(), undefined);
+    test.done();
+};
+
 exports['iterator.next'] = function(test){
     var call_order = [];
     var iterator = async.iterator([
@@ -330,6 +365,17 @@ exports['forEach'] = function(test){
     });
 };
 
+exports['forEach empty array'] = function(test){
+    test.expect(1);
+    async.forEach([], function(x, callback){
+        test.ok(false, 'iterator should not be called');
+        callback();
+    }, function(err){
+        test.ok(true, 'should call callback');
+    });
+    setTimeout(test.done, 25);
+};
+
 exports['forEach error'] = function(test){
     test.expect(1);
     async.forEach([1,2,3], function(x, callback){
@@ -353,6 +399,17 @@ exports['forEachSeries'] = function(test){
     });
 };
 
+exports['forEachSeries empty array'] = function(test){
+    test.expect(1);
+    async.forEachSeries([], function(x, callback){
+        test.ok(false, 'iterator should not be called');
+        callback();
+    }, function(err){
+        test.ok(true, 'should call callback');
+    });
+    setTimeout(test.done, 25);
+};
+
 exports['forEachSeries error'] = function(test){
     test.expect(2);
     var call_order = [];

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git



More information about the Pkg-javascript-commits mailing list