Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
### | ||
each(elements, parallel=false, callback) | ||
Chained and parallel async iterator in one elegant function | ||
### | ||
module.exports = (elements, parallel, callback) -> | ||
unless callback | ||
callback = parallel | ||
parallel = false | ||
type = typeof elements | ||
if elements is null or type is 'undefined' or type is 'number' or type is 'string' | ||
elements = [elements] | ||
else unless Array.isArray elements | ||
isObject = true | ||
i = 0 | ||
keys = Object.keys elements if isObject | ||
l = if keys then keys.length else elements.length | ||
if parallel | ||
next = () -> | ||
i++ | ||
return callback null, null if i is l | ||
for key in keys ? elements | ||
if keys | ||
then args = [key, elements[key], next] | ||
else args = [key, next] | ||
callback.apply null, args | ||
else | ||
next = () -> | ||
return callback null, null if i is l | ||
if keys | ||
then args = [keys[i], elements[keys[i++]], next] | ||
else args = [elements[i++], next] | ||
callback.apply null, args | ||
process.nextTick next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
assert = require 'assert' | ||
each = require '../index' | ||
|
||
module.exports = | ||
'Chain # array': (next) -> | ||
current = 0 | ||
each [ | ||
{id: 1} | ||
{id: 2} | ||
{id: 3} | ||
], (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 4 | ||
return next() | ||
assert.eql current, element.id | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # object': (next) -> | ||
current = 0 | ||
each | ||
id_1: 1 | ||
id_2: 2 | ||
id_3: 3 | ||
, (key, value, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 4 | ||
return next() | ||
assert.eql "id_#{current}", key | ||
assert.eql current, value | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # undefined': (next) -> | ||
current = 0 | ||
each undefined, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql undefined, element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # null': (next) -> | ||
current = 0 | ||
each null, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql null, element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # string': (next) -> | ||
current = 0 | ||
each 'id_1', (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql "id_1", element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # number': (next) -> | ||
current = 0 | ||
each 3.14, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql 3.14, element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
assert = require 'assert' | ||
each = require '../index' | ||
|
||
module.exports = | ||
'Chain # array': (next) -> | ||
current = 0 | ||
each [ | ||
{id: 1} | ||
{id: 2} | ||
{id: 3} | ||
], true, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 4 | ||
return next() | ||
assert.eql current, element.id | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # object': (next) -> | ||
current = 0 | ||
each | ||
id_1: 1 | ||
id_2: 2 | ||
id_3: 3 | ||
, true, (key, value, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 4 | ||
return next() | ||
assert.eql "id_#{current}", key | ||
assert.eql current, value | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # undefined': (next) -> | ||
current = 0 | ||
each undefined, true, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql undefined, element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # null': (next) -> | ||
current = 0 | ||
each null, true, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql null, element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # string': (next) -> | ||
current = 0 | ||
each 'id_1', true, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql "id_1", element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
'Chain # number': (next) -> | ||
current = 0 | ||
each 3.14, true, (element, n) -> | ||
current++ | ||
unless n | ||
assert.eql current, 2 | ||
return next() | ||
assert.eql 3.14, element | ||
setTimeout () -> | ||
n() | ||
, 100 | ||
|