From 9785a9111626506ae4d1bfe769e4f0a6e93c0e24 Mon Sep 17 00:00:00 2001 From: drewlesueur Date: Sat, 28 May 2011 08:11:54 +0000 Subject: [PATCH] exposed eachSeries --- nimble.js | 1 + test/test.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/nimble.js b/nimble.js index 1c9132b..e35b1a1 100644 --- a/nimble.js +++ b/nimble.js @@ -191,6 +191,7 @@ exports.each = function (obj, iterator, callback) { return (callback ? eachParallel: eachSync)(obj, iterator, callback); }; + exports.eachSeries = eachSeries; exports.map = function (obj, iterator, callback) { return (callback ? mapAsync(eachParallel): mapSync)(obj, iterator, callback); }; diff --git a/test/test.js b/test/test.js index 8c71bdb..47d1a7b 100644 --- a/test/test.js +++ b/test/test.js @@ -134,6 +134,71 @@ exports['each - async, error'] = function(test){ }); }; +exports['eachSeries - async'] = function (test) { + var calls = []; + _.eachSeries([1,2,3], function (value, index, arr, cb) { + calls.push(toArray(arguments).slice(0, 3)); + setTimeout(cb, 0); + }, function () { + test.same(calls, [ + [1, 0, [1,2,3]], + [2, 1, [1,2,3]], + [3, 2, [1,2,3]] + ]); + test.done(); + }); +}; + +exports['eachSeries - async, object'] = function (test) { + var calls = []; + var obj = { + a: 1, + b: 2, + c: 3 + }; + _.eachSeries(obj, function (value, index, arr, cb) { + calls.push(toArray(arguments).slice(0, 3)); + setTimeout(cb, 0); + }, function () { + test.same(calls, [ + [1, 'a', obj], + [2, 'b', obj], + [3, 'c', obj] + ]); + test.done(); + }); +}; + +exports['eachSeries - async, reduced arity'] = function (test) { + var calls = []; + _.eachSeries([1,2,3], function (value, cb) { + calls.push(value); + setTimeout(cb, 0); + }, function () { + test.same(calls, [1,2,3]); + test.done(); + }); +}; + +exports['eachSeries - async, empty array'] = function(test){ + _.eachSeries([], function(x, callback){ + test.ok(false, 'iterator should not be called'); + callback(); + }, function(err){ + test.ok(true, 'should call callback'); + test.done(); + }); +}; + +exports['eachSeries - async, error'] = function(test){ + _.eachSeries([1,2,3], function(x, callback){ + callback('error'); + }, function(err){ + test.equals(err, 'error'); + test.done(); + }); +}; + exports['map - sync'] = function (test) { var calls = []; test.same(