Comparing version 0.0.0 to 0.0.1
50
map.js
module.exports = map; | ||
/** | ||
* Apply async function to every item of iterable, receiving a callback function which takes error (if there is) and replacement parameters. | ||
* | ||
* @param {Function} fn | ||
* @param {Array, Object} iterable | ||
* @param {Function} callback | ||
*/ | ||
function map(fn, iterable, callback){ | ||
var clone, iter, i, len, key, value, | ||
list = isArray(iterable); | ||
function newCallback(id, mem, array, results, callback){ | ||
return function(error, result){ | ||
iter = list ? Array.prototype.slice.call(iterable) : objectKeys(iterable); | ||
clone = list ? iter : {}; | ||
if(mem.ret || ~mem.indexOf(id)) return; | ||
(function next(i, error, rpl){ | ||
if(error) { | ||
mem.ret = true; | ||
callback(error, results); | ||
return; | ||
} | ||
key = list ? i-1 : iter[i-1]; | ||
value = list ? clone[i] : iterable[ iter[i] ]; | ||
results[id] = result; | ||
mem.push(id); | ||
arguments.length > 2 && ( clone[key] = rpl ); | ||
if(mem.length == array.length) callback(undefined, results); | ||
if(error || i>=iter.length){ | ||
callback(error, clone); | ||
return; | ||
} | ||
}; | ||
} | ||
fn(value, next.bind(undefined, i+1)); | ||
function map(fn, array, callback){ | ||
})(0); | ||
}; | ||
var result = [], | ||
mem = [], | ||
i = array.length; | ||
function objectKeys(obj){ | ||
var keys = [], key; | ||
for(key in obj){ | ||
keys.push( key ); | ||
while( i-- ) { | ||
fn(array[i], newCallback(i, mem, array, result, callback)); | ||
} | ||
return keys; | ||
} | ||
function isArray(obj){ | ||
return Object.prototype.toString.call(obj) == '[object Array]'; | ||
} |
{ | ||
"name": "map", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "async map. iterates both array and objects.", | ||
@@ -5,0 +5,0 @@ "main": "map.js", |
@@ -1,2 +0,2 @@ | ||
Async [Map](http://en.wikipedia.org/wiki/Map_(higher-order_function)). Iterates arrays or objects. | ||
Async [Map](http://en.wikipedia.org/wiki/Map_\(higher-order_function\)). Iterates arrays and objects. | ||
@@ -9,24 +9,9 @@ ## Install | ||
## Usage | ||
``` | ||
var keywords = ['foo', 'bar'], | ||
people = { lennon: "John Lennon", best: "George Best" }; | ||
```js | ||
var keywords = ['foo', 'bar']; | ||
map(searchGoogle, keywords, function(error, results){ | ||
map(search, keywords, function(error, results){ | ||
error.should.not.exist(); | ||
results.should.have.length(2); | ||
}); | ||
map(searchGoogle, people, function(error, results){ | ||
error.should.not.exist(); | ||
results.should.contain('lennon'); | ||
results.should.contain('best'); | ||
}); | ||
``` | ||
Since iterables aren't first parameters, new functions can be created from map; | ||
``` | ||
searchGoogle = map.bind(null, searchGoogle); | ||
searchGoogle(['hello', 'kitty'], function(){}); // or | ||
``` |
34
test.js
@@ -1,2 +0,2 @@ | ||
var map = require('./map'), | ||
var map = require('./map'), | ||
assert = require('assert'); | ||
@@ -29,27 +29,2 @@ | ||
it('iterates objects', function(done){ | ||
var range = { 'a': 3, 'b': 1, 'c': 4 }; | ||
function fn(el,callback){ callback(undefined, el*el); } | ||
map(fn, range, function(error, seq){ | ||
if(error){ | ||
throw error; | ||
} | ||
assert.equal(range.a, 3); | ||
assert.equal(range.b, 1); | ||
assert.equal(range.c, 4); | ||
assert.equal(seq.a, 9); | ||
assert.equal(seq.b, 1); | ||
assert.equal(seq.c, 16); | ||
done(); | ||
}); | ||
}); | ||
it('stops iteration when an error is returned', function(done){ | ||
@@ -61,10 +36,5 @@ function fn(_,callback){ | ||
map(fn, [3, 1, 4], function(error, list){ | ||
assert.equal(list[0], 9); | ||
assert.equal(list[1], 1); | ||
assert.equal(list[2], 4); | ||
assert.equal(error.message, 'foobar'); | ||
assert.deepEqual(list, []); | ||
done(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1963
49
17