diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/async.js | 3 | ||||
-rwxr-xr-x | test/test-async.js | 52 |
3 files changed, 56 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f6960bc..d637ab4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ New Features: - `cargo` now supports all of the same methods and event callbacks as `queue`. - Added `ensureAsync` - A wrapper that ensures an async function calls its callback on a later tick. (#769) - Optimized `map`, `eachOf`, and `waterfall` families of functions +- Passing a `null` or `undefined` array to `map`, `each`, `parallel` and families will be treated as an empty array (#667). - Reduced file size by 4kb, (minified version by 1kb) - Added code coverage through `nyc` and `coveralls` (#768) diff --git a/lib/async.js b/lib/async.js index fd7a953..68b9c54 100644 --- a/lib/async.js +++ b/lib/async.js @@ -207,6 +207,7 @@ async.forEachOf = async.eachOf = function (object, iterator, callback) { callback = callback || noop; + object = object || []; var size = object.length || _keys(object).length; var completed = 0; if (!size) { @@ -232,6 +233,7 @@ async.forEachOfSeries = async.eachOfSeries = function (obj, iterator, callback) { callback = callback || noop; + obj = obj || []; var nextKey = _keyIterator(obj); function iterate() { var sync = true; @@ -269,6 +271,7 @@ return function (obj, iterator, callback) { callback = callback || noop; + obj = obj || []; var nextKey = _keyIterator(obj); if (limit <= 0) { return callback(null); diff --git a/test/test-async.js b/test/test-async.js index bdb8147..ce2fc8d 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1780,6 +1780,27 @@ exports['map'] = { setTimeout(test.done, 50); }, +'map undefined array': function(test){ + test.expect(2); + async.map(undefined, function(x, callback){ + callback(); + }, function(err, result){ + test.equals(err, null); + test.same(result, []); + }); + setTimeout(test.done, 50); +}, + +'map object': function (test) { + async.map({a: 1, b: 2, c: 3}, function (val, callback) { + callback(null, val * 2); + }, function (err, result) { + if (err) throw err; + test.same(result, {a: 2, b: 4, c: 6}); + test.done(); + }); +}, + 'mapSeries': function(test){ var call_order = []; async.mapSeries([1,3,2], mapIterator.bind(this, call_order), function(err, results){ @@ -1800,6 +1821,26 @@ exports['map'] = { setTimeout(test.done, 50); }, +'mapSeries undefined array': function(test){ + test.expect(2); + async.mapSeries(undefined, function(x, callback){ + callback(); + }, function(err, result){ + test.equals(err, null); + test.same(result, []); + }); + setTimeout(test.done, 50); +}, + +'mapSeries object': function (test) { + async.mapSeries({a: 1, b: 2, c: 3}, function (val, callback) { + callback(null, val * 2); + }, function (err, result) { + if (err) throw err; + test.same(result, {a: 2, b: 4, c: 6}); + test.done(); + }); +}, 'mapLimit': function(test){ var call_order = []; @@ -1823,6 +1864,17 @@ exports['map'] = { setTimeout(test.done, 25); }, +'mapLimit undefined array': function(test){ + test.expect(2); + async.mapLimit(undefined, 2, function(x, callback){ + callback(); + }, function(err, result){ + test.equals(err, null); + test.same(result, []); + }); + setTimeout(test.done, 50); +}, + 'mapLimit limit exceeds size': function(test){ var call_order = []; async.mapLimit([0,1,2,3,4,5,6,7,8,9], 20, mapIterator.bind(this, call_order), function(err, results){ |