Skip to content

Commit

Permalink
set default values for each functions. Fixes #667
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Early committed Jun 1, 2015
1 parent 5244f77 commit 63beb5f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -269,6 +271,7 @@

return function (obj, iterator, callback) {
callback = callback || noop;
obj = obj || [];
var nextKey = _keyIterator(obj);
if (limit <= 0) {
return callback(null);
Expand Down
52 changes: 52 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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 = [];
Expand All @@ -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){
Expand Down

0 comments on commit 63beb5f

Please sign in to comment.