Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

core: Changed iterator of map()/runMap() to consist with Array.map()

  • Loading branch information...
commit 8d9c05353c54693bd19d498455bedc75841ac22c 1 parent 5990361
@koichik authored
Showing with 36 additions and 6 deletions.
  1. +1 −0  CHANGELOG.md
  2. +11 −3 README.md
  3. +12 −3 lib/flowless.js
  4. +12 −0 test/map.js
View
1  CHANGELOG.md
@@ -4,6 +4,7 @@
- core: Added logging for debug.
- core: Introduced custom Error object.
- core: Fixed case where `functions` or `array` is empty.
+ - core: Changed iterator of `map()/runMap()` to consist with `Array.map()`.
- extras: Moved into `extras` namespace.
- extras: Added `flattenFirst()`, `flattenSecond()` and `flattenThird()`.
View
14 README.md
@@ -382,12 +382,16 @@ by mapping each value in the given array through the returned function.
* An asynchronous style function which produces a new array of values
by mapping each value in the given `array` through `fn`.
-`fn` should be:
+`fn` should be one of:
function(element, cb)
+ function(element, index, cb)
+ function(element, index, array, cb)
* Arguments
- * `element`: an element of `array` passed to returned function.
+ * `element`: An element of `array` passed to returned function.
+ * `index`: An index number of the element.
+ * `array`: An array passed to returned function.
* `cb`: A callback function.
Returned function is:
@@ -414,12 +418,16 @@ through the returned function.
It can be an array as a *Function Template* that is explained below.
* `cb`: A callback function.
-`fn` should be:
+`fn` should be one of:
function(element, cb)
+ function(element, index, cb)
+ function(element, index, array, cb)
* Arguments
* `element`: an element of `array`.
+ * `index`: An index number of the element.
+ * `array`: `array` passed to `runMap()`.
* `cb`: A callback function.
Example:
View
15 lib/flowless.js
@@ -123,7 +123,7 @@ function setupPar(ref, functions) {
assert(typeof cb === 'function', 'last argument must be a function');
invokeParFuncs(operator, location, functions.length, functions,
- function invokeFunc(fn, next) {
+ function invokeFunc(fn, index, next) {
fn = utils.makeFunc(fn);
fn.apply(null, args.slice().concat(next));
}, cb);
@@ -140,7 +140,16 @@ function setupMap(ref, concurrency, fn) {
return function doMap(array, cb) {
assert(Array.isArray(array), 'first argument must be an array');
assert(typeof cb === 'function', 'last argument must be a function');
- invokeParFuncs(operator, location, concurrency, array, fn, cb);
+ invokeParFuncs(operator, location, concurrency, array,
+ function invokeFunc(value, index, next) {
+ if (fn.length === 2) {
+ fn(value, next);
+ } else if (fn.length === 3) {
+ fn(value, index, next);
+ } else {
+ fn(value, index, array, next);
+ }
+ }, cb);
};
}
@@ -171,7 +180,7 @@ function invokeParFuncs(operator, location, concurrency, array,
operator, index, location, array[index]);
var returnedInner = false;
try {
- invokeFunction(array[index], next);
+ invokeFunction(array[index], index, next);
} catch (err) {
utils.debug('failed %s[%d] at %s thrown:', operator, index, location,
util.isError(err) ? err.message : err);
View
12 test/map.js
@@ -86,4 +86,16 @@ describe('map', function() {
});
returned = true;
});
+
+ it('should call function as same as Array.map()', function(done) {
+ var data = ['foo', 'bar'];
+ flowless.runMap(data, function(value, index, array, cb) {
+ value.should.equal(data[index]);
+ array.should.equal(data);
+ cb(null);
+ }, function(err, results) {
+ should.not.exist(err);
+ done();
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.