Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "eachy", | ||
"main": "eachy.js", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"homepage": "https://github.com/mmaelzer/eachy", | ||
@@ -9,3 +9,3 @@ "authors": [ | ||
], | ||
"description": "A tiny (16 lines of code) async each implementation that supports CommonJS, AMD, and VanillaJS.", | ||
"description": "A tiny (19 lines of code) async each implementation that supports CommonJS, AMD, and VanillaJS.", | ||
"moduleType": [ | ||
@@ -12,0 +12,0 @@ "amd", |
11
eachy.js
(function(global) { | ||
function eachy(items, iterator, callback) { | ||
var index = 0; | ||
var index = 0, results = []; | ||
(function next(err) { | ||
if (err || index === items.length) return (callback || function(){})(err); | ||
iterator(items[index], next, index++); | ||
if (err || index === items.length) return (callback || function(){})(err, results); | ||
iterator(items[index], function(err, result) { | ||
results[index++] = result; | ||
next(err); | ||
}, index); | ||
})(); | ||
@@ -14,4 +17,4 @@ } | ||
} else { | ||
global.asyncEach = eachy; | ||
global.seriesEach= eachy; | ||
} | ||
})(this); |
@@ -1,1 +0,1 @@ | ||
!function(n){function e(n,e,o){var t=0;!function d(f){return f||t===n.length?(o||function(){})(f):void e(n[t],d,t++)}()}"undefined"!=typeof define&&define.amd?define(function(){return e}):"undefined"!=typeof module&&module.exports?module.exports=e:n.asyncEach=e}(this); | ||
!function(e){function n(e,n,i){var o=0,t=[];!function f(u){return u||o===e.length?(i||function(){})(u,t):void n(e[o],function(e,n){t[o++]=n,f(e)},o)}()}"undefined"!=typeof define&&define.amd?define(function(){return n}):"undefined"!=typeof module&&module.exports?module.exports=n:e.seriesEach=n}(this); |
{ | ||
"name": "eachy", | ||
"version": "1.0.2", | ||
"description": "a tiny (16 lines of code) serial async each module", | ||
"version": "1.0.3", | ||
"description": "a tiny (19 lines of code) serial async each module", | ||
"main": "eachy.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
eachy | ||
===== | ||
A tiny (16 lines of code) serial async each implementation that supports CommonJS, AMD, and VanillaJS. The minified file `eachy.min.js` is just 270 bytes. | ||
A tiny (19 lines of code) serial async each implementation that supports CommonJS, AMD, and VanillaJS. The minified file `eachy.min.js` is just 303 bytes. | ||
@@ -11,2 +11,3 @@ [![build status](https://secure.travis-ci.org/mmaelzer/eachy.png)](http://travis-ci.org/mmaelzer/eachy) | ||
#### npm | ||
``` | ||
@@ -16,2 +17,3 @@ npm install eachy | ||
#### bower | ||
``` | ||
@@ -24,8 +26,8 @@ bower install eachy | ||
### asyncEach(array, iterator, callback) | ||
### seriesEach(array, iterator, callback) | ||
#### Arguments | ||
* `array` - An array to iterate over | ||
* `iterator(item, callback, index)` - A function called for each `item` in the `array`. The `callback(err)` takes a single, optional argument, an error. The `index` value is index of the item in the array. | ||
* `callback(err)` - The callback that is called when all `iterator` functions are finished or an error occurs. | ||
* `iterator(item, callback, index)` - A function called for each `item` in the `array`. The `callback(err, value)` optionally takes an error or data. Data is collected and provided when all iterator functions have finished. The `index` value is the index of the item in the array. | ||
* `callback(err, values)` - The callback that is called when all `iterator` functions are finished or an error occurs. The `values` argument is an array of values collected from iterator functions. The index of the value in values maps to the index of the item provided to the iterator function. | ||
@@ -38,4 +40,4 @@ Examples | ||
<script> | ||
// asyncEach is a global bound to the window object now | ||
asyncEach(['hi', 'i love', 'alerts'], function(word, done) { | ||
// seriesEach is a global bound to the window object now | ||
seriesEach(['hi', 'i love', 'alerts'], function(word, done) { | ||
alert(word); | ||
@@ -49,14 +51,11 @@ done(); | ||
```javascript | ||
var asyncEach = require('eachy'); | ||
var seriesEach = require('eachy'); | ||
var fs = require('fs'); | ||
asyncEach(['robots.txt', 'todo.txt'], function(file, done) { | ||
fs.readFile(file, function(err, data) { | ||
if (err) return done(err); | ||
console.log(data); | ||
done(); | ||
seriesEach(['robots.txt', 'todo.txt'], fs.readFile, function(err, files) { | ||
files.forEach(function(file) { | ||
console.log(file); | ||
}); | ||
}, function(err) { | ||
console.log('You should have all the information you need now.'); | ||
}); | ||
``` |
@@ -5,3 +5,3 @@ var each = require('./'); | ||
test('eachy', function(t) { | ||
t.plan(6); | ||
t.plan(7); | ||
@@ -52,2 +52,8 @@ var result = []; | ||
each([1,2,3], function(n, done) { done(); }); | ||
each([1,2,3], function(n, done) { | ||
done(null, n * 2); | ||
}, function(err, doubles) { | ||
t.deepEqual([2,4,6], doubles, 'Eachy properly returns data provided to the callbacks and respects the item order of the initial array'); | ||
}); | ||
}); |
5318
102
57