Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Null and undefined are no longer iterated

  • Loading branch information...
commit 0b95ce4dccf971e1b95b8b98aa9229d9f470e06f 1 parent 1c86e0c
@wdavidw authored
View
16 lib/each.js
@@ -17,7 +17,9 @@ Chained and parallel async iterator in one elegant function
module.exports = function(elements) {
var arglength, eacher, endable, errors, events, isObject, keys, next, parallel, run, times, type;
type = typeof elements;
- if (elements === null || type === 'undefined' || type === 'number' || type === 'string' || type === 'function' || type === 'boolean') {
+ if (elements === null || type === 'undefined') {
+ elements = [];
+ } else if (type === 'number' || type === 'string' || type === 'function' || type === 'boolean') {
elements = [elements];
} else if (!Array.isArray(elements)) {
isObject = true;
@@ -43,6 +45,10 @@ module.exports = function(elements) {
endable = 1;
eacher.paused = 0;
eacher.readable = true;
+ eacher.write = function(item) {
+ elements.push(item);
+ return eacher.total++;
+ };
eacher.pause = function() {
return eacher.paused++;
};
@@ -62,6 +68,9 @@ module.exports = function(elements) {
};
eacher.times = function(t) {
times = t;
+ if (elements.length === 0) {
+ eacher.write(null);
+ }
return eacher;
};
eacher.files = function(pattern) {
@@ -74,11 +83,6 @@ module.exports = function(elements) {
return this;
}
endable--;
- if (arglength === 0) {
- arglength = null;
- eacher.total = 0;
- elements = [];
- }
glob(pattern, function(err, files) {
var file, _j, _len1;
eacher.total += files.length;
View
16 src/each.coffee
@@ -12,7 +12,9 @@ Chained and parallel async iterator in one elegant function
###
module.exports = (elements) ->
type = typeof elements
- if elements is null or type is 'undefined' or type is 'number' or type is 'string' or type is 'function' or type is 'boolean'
+ if elements is null or type is 'undefined'
+ elements = []
+ else if type is 'number' or type is 'string' or type is 'function' or type is 'boolean'
elements = [elements]
else unless Array.isArray elements
isObject = true
@@ -34,6 +36,9 @@ module.exports = (elements) ->
endable = 1
eacher.paused = 0
eacher.readable = true
+ eacher.write = (item) ->
+ elements.push item
+ eacher.total++
eacher.pause = ->
eacher.paused++
eacher.resume = ->
@@ -50,16 +55,17 @@ module.exports = (elements) ->
eacher
eacher.times = (t) ->
times = t
+ eacher.write null if elements.length is 0
eacher
eacher.files = (pattern) ->
if Array.isArray pattern
for p in pattern then @files p
return @
endable--
- if arglength is 0
- arglength = null
- eacher.total = 0
- elements = []
+ # if arglength is 0
+ # arglength = null
+ # eacher.total = 0
+ # elements = []
glob pattern, (err, files) ->
eacher.total += files.length
for file in files
View
14 test/parallel.coffee
@@ -43,32 +43,22 @@ describe 'Parallel', ->
current.should.eql 3
next()
it 'Parallel # undefined', (next) ->
- current = 0
each( undefined )
.parallel( true )
.on 'item', (element, index, next) ->
- index.should.eql current
- current++
- should.not.exist element
- setTimeout next, 100
+ should.not.exist true
.on 'error', (err) ->
should.not.exist err
.on 'end', ->
- current.should.eql 1
next()
it 'Parallel # null', (next) ->
- current = 0
each( null )
.parallel( true )
.on 'item', (element, index, next) ->
- index.should.eql current
- current++
- should.not.exist element
- setTimeout next, 100
+ should.not.exist true
.on 'error', (err) ->
should.not.exist err
.on 'end', ->
- current.should.eql 1
next()
it 'Parallel # string', (next) ->
current = 0
View
14 test/sequential.coffee
@@ -49,27 +49,21 @@ describe 'Sequential', ->
current = 0
each( undefined )
.on 'item', (element, index, next) ->
- index.should.eql current
- current++
- should.not.exist element
- setTimeout next, 100
+ should.not.exist true
.on 'error', (err) ->
should.not.exist err
.on 'end', ->
- current.should.eql 1
+ current.should.eql 0
next()
it 'null', (next) ->
current = 0
each( null )
.on 'item', (element, index, next) ->
- index.should.eql current
- current++
- should.not.exist element
- setTimeout next, 100
+ should.not.exist true
.on 'error', (err) ->
should.not.exist err
.on 'end', ->
- current.should.eql 1
+ current.should.eql 0
next()
it 'string', (next) ->
current = 0
Please sign in to comment.
Something went wrong with that request. Please try again.