Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add each, eachParallel

  • Loading branch information...
commit aa593f78d53c1038f434080d7a7475dd6a3eba11 1 parent af66c54
@hokaccha authored
Showing with 88 additions and 0 deletions.
  1. +88 −0 lib/chain-tiny.js
View
88 lib/chain-tiny.js
@@ -133,6 +133,64 @@ Chain.prototype.forEachParallel = function(fn) {
/**
+ *
+ * Iterator function to each item in an object (plain hash). Array recieved before next function args.
+ *
+ * @param {Object} obj
+ * @return {Object} chain
+ * @api public
+ */
+Chain.prototype.each = function(fn) {
+ var self = this;
+
+ return self.chain(function(obj, next) {
+ var _chain = Chain();
+ var results = {};
+ 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;
+ _next(err);
+ });
+ });
+ });
+ _chain.end(function(err) {
+ next(err, results);
+ });
+ });
+};
+
+
+/**
+ *
+ * Iterator function to each item in an object (plain hash) parallel. Array recieved before next function args.
+ *
+ * @param {Object} obj
+ * @return {Object} chain
+ * @api public
+ */
+Chain.prototype.eachParallel = function(fn) {
+ var self = this;
+
+ return self.chain(function(obj, next) {
+ var _chain = Chain();
+ var q = {};
+ Object.keys(obj).forEach(function(key) {
+ var val = obj[key];
+ q[key] = function(_next) {
+ fn.call(null, key, val, function(err, result) {
+ _next(err, result);
+ });
+ };
+ });
+
+ Chain.parallel(q).end(next);
+ });
+};
+
+
+/**
* Parallel exec functions.
*
* @param {Object} obj
@@ -221,6 +279,36 @@ Chain.forEachParallel = function(ary, fn) {
/**
+ * Static method for `.each()`.
+ *
+ * @param {Object} Object
+ * @param {Function} fn
+ * @return {Object} chain
+ * @api public
+ */
+Chain.each = function(obj, fn) {
+ return Chain(function(next) {
+ next(null, obj);
+ }).each(fn);
+};
+
+
+/**
+ * Static method for `.eachParallel()`.
+ *
+ * @param {Object} Object
+ * @param {Function} fn
+ * @return {Object} chain
+ * @api public
+ */
+Chain.eachParallel = function(obj, fn) {
+ return Chain(function(next) {
+ next(null, obj);
+ }).eachParallel(fn);
+};
+
+
+/**
* Static method for `.parallel()`.
*
* @param {time} Int
Please sign in to comment.
Something went wrong with that request. Please try again.