Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`#each`, `#eachParallel` can recieve obj argument.

  • Loading branch information...
commit a0fb8285643c432d025d08e468cad96139045ac3 1 parent e11112a
@hokaccha authored
Showing with 72 additions and 15 deletions.
  1. +25 −15 lib/chain-tiny.js
  2. +47 −0 test/each.js
View
40 lib/chain-tiny.js
@@ -86,11 +86,18 @@ Chain.prototype.end = function(fn) {
* @return {Object} chain
* @api public
*/
-Chain.prototype.each = function(fn) {
+Chain.prototype.each = function(obj, fn) {
var self = this;
- return self.chain(function(obj, origNext) {
+ if (typeof obj === 'function') {
+ fn = obj;
+ obj = null;
+ }
+
+ return self.chain(function(_obj) {
+ obj = obj || _obj;
var _chain = Chain();
+ var origNext = this.next;
var isArray = Array.isArray(obj);
var results = isArray ? [] : {};
Object.keys(obj).forEach(function(key) {
@@ -124,10 +131,17 @@ Chain.prototype.each = function(fn) {
* @return {Object} chain
* @api public
*/
-Chain.prototype.eachParallel = function(fn) {
+Chain.prototype.eachParallel = function(obj, fn) {
var self = this;
- return self.chain(function(obj, origNext) {
+ if (typeof obj === 'function') {
+ fn = obj;
+ obj = null;
+ }
+
+ return self.chain(function(_obj) {
+ obj = obj || _obj;
+ var origNext = this.next;
var isArray = Array.isArray(obj);
var q = isArray ? [] : {};
@@ -199,40 +213,36 @@ Chain.prototype.parallel = function(obj) {
/**
* Static method for `.each()`.
*
- * @param {Object} Object
+ * @param {Object} obj
* @param {Function} fn
* @return {Object} chain
* @api public
*/
Chain.each = function(obj, fn) {
- return Chain(function(next) {
- next(null, obj);
- }).each(fn);
+ return Chain().each(obj, fn);
};
/**
* Static method for `.eachParallel()`.
*
- * @param {Object} Object
+ * @param {Object} obj
* @param {Function} fn
* @return {Object} chain
* @api public
*/
Chain.eachParallel = function(obj, fn) {
- return Chain(function(next) {
- next(null, obj);
- }).eachParallel(fn);
+ return Chain().eachParallel(obj, fn);
};
/**
* Static method for `.parallel()`.
*
- * @param {time} Int
+ * @param {Object} obj
* @return {Object} chain
* @api public
*/
-Chain.parallel = function(ary) {
- return Chain().parallel(ary);
+Chain.parallel = function(obj) {
+ return Chain().parallel(obj);
};
View
47 test/each.js
@@ -17,6 +17,17 @@ module.exports = nodeunit.testCase({
t.equal(results.foo, 'foo/bar');
t.equal(results.hoge, 'hoge/fuga');
t.ok(Date.now() - start >= 190 );
+ next(null, { a: 'b' });
+ })
+ .each({ foo: 'bar', hoge: 'fuga' }, function(key, val, next) {
+ t.equal(this.next, next);
+ setTimeout(function() {
+ next(null, key + '/' + val);
+ }, 100);
+ })
+ .chain(function(results, next) {
+ t.equal(results.foo, 'foo/bar');
+ t.equal(results.hoge, 'hoge/fuga');
next();
})
.end(t.done);
@@ -36,6 +47,19 @@ module.exports = nodeunit.testCase({
t.equal(results.length, 2);
t.equal(results[0], '0/foo');
t.equal(results[1], '1/bar');
+ t.ok(Date.now() - start >= 190 );
+ next(null, [1, 2]);
+ })
+ .each(['foo', 'bar'], function(key, val, next) {
+ t.equal(this.next, 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);
@@ -55,6 +79,17 @@ module.exports = nodeunit.testCase({
t.equal(results.foo, 'foo/bar');
t.equal(results.hoge, 'hoge/fuga');
t.ok( Date.now() - start < 190 );
+ next(null, { a: 'b', c: 'd' });
+ })
+ .eachParallel({ foo: 'bar', hoge: 'fuga'}, function(key, val, next) {
+ t.equal(this.next, next);
+ setTimeout(function() {
+ next(null, key + '/' + val);
+ }, 100);
+ })
+ .chain(function(results, next) {
+ t.equal(results.foo, 'foo/bar');
+ t.equal(results.hoge, 'hoge/fuga');
next();
})
.end(t.done);
@@ -75,6 +110,18 @@ module.exports = nodeunit.testCase({
t.equal(results[0], '0/foo');
t.equal(results[1], '1/bar');
t.ok( Date.now() - start < 190 );
+ next(null, [1, 2]);
+ })
+ .eachParallel(['foo', 'bar'], function(key, val, next) {
+ t.equal(this.next, 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);
Please sign in to comment.
Something went wrong with that request. Please try again.