Permalink
Browse files

Fix `.each` can set Array

  • Loading branch information...
1 parent d1ff0c7 commit 4dd04642334cd7226abe61a821189a9ed3bf69f3 @hokaccha committed Mar 12, 2012
Showing with 80 additions and 8 deletions.
  1. +7 −4 lib/chain-tiny.js
  2. +73 −4 test/each.js
View
@@ -145,12 +145,13 @@ Chain.prototype.each = function(fn) {
return self.chain(function(obj, next) {
var _chain = Chain();
- var results = {};
+ var isArray = Array.isArray(obj);
+ var results = isArray ? [] : {};
Object.keys(obj).forEach(function(key) {
var val = obj[key];
_chain.chain(function(_next) {
fn.call(null, key, val, function(err, result) {
- results[key] = result;
+ isArray ? results.push(result) : results[key] = result;
_next(err);
});
});
@@ -175,14 +176,16 @@ Chain.prototype.eachParallel = function(fn) {
return self.chain(function(obj, next) {
var _chain = Chain();
- var q = {};
+ var isArray = Array.isArray(obj);
+ var q = isArray ? [] : {};
Object.keys(obj).forEach(function(key) {
var val = obj[key];
- q[key] = function(_next) {
+ var _fn = function(_next) {
fn.call(null, key, val, function(err, result) {
_next(err, result);
});
};
+ isArray ? q.push(_fn) : q[key] = _fn;
});
Chain.parallel(q).end(next);
View
@@ -2,7 +2,7 @@ var nodeunit = require('nodeunit');
var chain = require('../index');
module.exports = nodeunit.testCase({
- '.each()': function(t) {
+ '.each() object': function(t) {
var start = Date.now();
chain(function(next) {
next(null, { foo: 'bar', hoge: 'fuga' });
@@ -20,7 +20,25 @@ module.exports = nodeunit.testCase({
})
.end(t.done);
},
- '.eachParallel()': function(t) {
+ '.each() array': function(t) {
+ var start = Date.now();
+ chain(function(next) {
+ next(null, ['foo', 'bar']);
+ })
+ .each(function(key, val, next) {
+ setTimeout(function() {
+ next(null, key + '/' + val);
+ }, 100);
+ })
+ .chain(function(results, next) {
+ t.equal(results.length, 2);
+ t.equal(results[0], '0/foo');
+ t.equal(results[1], '1/bar');
+ next();
+ })
+ .end(t.done);
+ },
+ '.eachParallel() object': function(t) {
var start = Date.now();
chain(function(next) {
next(null, { foo: 'bar', hoge: 'fuga'});
@@ -38,7 +56,26 @@ module.exports = nodeunit.testCase({
})
.end(t.done);
},
- 'Chain.each()': function(t) {
+ '.eachParallel() array': function(t) {
+ var start = Date.now();
+ chain(function(next) {
+ next(null, ['foo', 'bar']);
+ })
+ .eachParallel(function(key, val, next) {
+ setTimeout(function() {
+ next(null, key + '/' + val);
+ }, 100);
+ })
+ .chain(function(results, next) {
+ t.equal(results.length, 2);
+ t.equal(results[0], '0/foo');
+ t.equal(results[1], '1/bar');
+ t.ok( Date.now() - start < 190 );
+ next();
+ })
+ .end(t.done);
+ },
+ 'Chain.each() object': function(t) {
var start = Date.now();
chain.each({ foo: 'bar', hoge: 'fuga' }, function(key, val, next) {
setTimeout(function() {
@@ -53,7 +90,23 @@ module.exports = nodeunit.testCase({
})
.end(t.done);
},
- 'Chain.eachParallel()': function(t) {
+ 'Chain.each() array': function(t) {
+ var start = Date.now();
+ chain.each(['foo', 'bar'], function(key, val, next) {
+ setTimeout(function() {
+ next(null, key + '/' + val);
+ }, 100);
+ })
+ .chain(function(results, next) {
+ t.equal(results.length, 2);
+ t.equal(results[0], '0/foo');
+ t.equal(results[1], '1/bar');
+ t.ok( Date.now() - start >= 190 );
+ next();
+ })
+ .end(t.done);
+ },
+ 'Chain.eachParallel() object': function(t) {
var start = Date.now();
chain.eachParallel({ foo: 'bar', hoge: 'fuga'}, function(key, val, next) {
setTimeout(function() {
@@ -68,4 +121,20 @@ module.exports = nodeunit.testCase({
})
.end(t.done);
},
+ 'Chain.eachParallel() array': function(t) {
+ var start = Date.now();
+ chain.eachParallel(['foo', 'bar'], function(key, val, next) {
+ setTimeout(function() {
+ next(null, key + '/' + val);
+ }, 100);
+ })
+ .chain(function(results, next) {
+ t.equal(results.length, 2);
+ t.equal(results[0], '0/foo');
+ t.equal(results[1], '1/bar');
+ t.ok( Date.now() - start < 190 );
+ next();
+ })
+ .end(t.done);
+ },
});

0 comments on commit 4dd0464

Please sign in to comment.