Permalink
Browse files

Handle zero length arrays properly

  • Loading branch information...
1 parent 9d4a046 commit 647dc9cc0cabdac94467c113fabf7e95b7683dfd @kriszyp committed Jul 28, 2011
Showing with 5 additions and 5 deletions.
  1. +5 −5 each.js
View
10 each.js
@@ -18,30 +18,30 @@ define([], function(){
if(callback.length > 2){
emit = getEmit(result = []);
}
- if(array.length > -1){
+ if(length > -1){
if(thisObject){
// iterate over array
- do{
+ for(;i < length; i++){
// call the callback
var newValue = callback.call(thisObject, array[i], i, emit);
// if a value was returned, examine it
if(newValue !== undefined){
// defined value breaks out of loop
return newValue;
}
- }while(++i < length);
+ }
}else{
// we do a separate branch for when thisObject isn't provided because
// it is faster to avoid the .call()
- do{
+ for(;i < length; i++){
// call the callback
var newValue = callback(array[i], i, emit);
// if a value was returned, examine it
if(newValue !== undefined){
// defined value breaks out of loop
return newValue;
}
- }while(++i < length);
+ }
}
}else{
// not an array, iterate over an object

0 comments on commit 647dc9c

Please sign in to comment.