Skip to content

Commit

Permalink
Lib and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Sep 21, 2011
1 parent ec25bdc commit 23d2a45
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/each.coffee
@@ -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
81 changes: 81 additions & 0 deletions test/ChainTest.coffee
@@ -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

81 changes: 81 additions & 0 deletions test/ParallelTest.coffee
@@ -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

0 comments on commit 23d2a45

Please sign in to comment.