Skip to content
Browse files

New write function to add array and key/value elements

  • Loading branch information...
1 parent 0b95ce4 commit dbfce1c4a973ce52396e11bfd2ae0c0b4fb21b16 @wdavidw committed Feb 7, 2013
Showing with 53 additions and 5 deletions.
  1. +5 −2 doc/index.md
  2. +13 −2 lib/each.js
  3. +8 −1 src/each.coffee
  4. +27 −0 test/write.coffee
View
7 doc/index.md
@@ -85,8 +85,9 @@ API
The `each` function signature is: `each(subject)`.
- `subject`
- The subject to iterate. It can be an array, an object or
- any other types in which case the behavior is similar to the one of an array.
+ The subject to iterate. It is usually an array or an object. Inserting a number
+ or a string will behave like an array of one element and inserting null or undefined
+ wont iterate over any element.
The return object is an instance of `EventEmitter`.
@@ -102,6 +103,8 @@ The following properties are available:
The following functions are available:
+- `write`
+ Add array elements or key/value pairs to the iteration.
- `parallel`
The second argument is optional and indicate wether or not you want the
iteration to run in `sequential`, `parallel` or `concurrent` mode. See below
View
15 lib/each.js
@@ -46,8 +46,19 @@ module.exports = function(elements) {
eacher.paused = 0;
eacher.readable = true;
eacher.write = function(item) {
- elements.push(item);
- return eacher.total++;
+ var l;
+ l = arguments.length;
+ if (l === 1) {
+ elements.push(arguments[0]);
+ } else if (l === 2) {
+ if (!keys) {
+ keys = [];
+ }
+ keys.push(arguments[0]);
+ elements[arguments[0]] = arguments[1];
+ }
+ eacher.total++;
+ return eacher;
};
eacher.pause = function() {
return eacher.paused++;
View
9 src/each.coffee
@@ -37,8 +37,15 @@ module.exports = (elements) ->
eacher.paused = 0
eacher.readable = true
eacher.write = (item) ->
- elements.push item
+ l = arguments.length
+ if l is 1
+ elements.push arguments[0]
+ else if l is 2
+ keys = [] if not keys
+ keys.push arguments[0]
+ elements[arguments[0]] = arguments[1]
eacher.total++
+ eacher
eacher.pause = ->
eacher.paused++
eacher.resume = ->
View
27 test/write.coffee
@@ -0,0 +1,27 @@
+
+should = require 'should'
+each = if process.env.EACH_COV then require '../lib-cov/each' else require '../lib/each'
+
+describe 'Write', ->
+
+ it 'accept array elements', (next) ->
+ each()
+ .write('hello')
+ .write('each')
+ .on 'item', (item, index, next) ->
+ item.should.eql 'hello' if index is 0
+ next()
+ .on 'end', (count) ->
+ count.should.eql 2
+ next()
+
+ it 'accept key value elements', (next) ->
+ each()
+ .write('hello', 'each')
+ .write('youre', 'welcome')
+ .on 'item', (key, value, next) ->
+ value.should.eql 'each' if key is 'hello'
+ next()
+ .on 'end', (count) ->
+ count.should.eql 2
+ next()

0 comments on commit dbfce1c

Please sign in to comment.
Something went wrong with that request. Please try again.