Created
September 21, 2012 21:14
-
-
Save influx6/3763937 to your computer and use it in GitHub Desktop.
Iterator payload
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
iterable: function(collection,eachfunc,finish){ | |
if(!collection || !eachfunc) return; | |
//handles management of next call for collection,be it arrays or objects | |
var len,keys,self = this,isArray = false; | |
if(this.isObject(collection)){ keys = this.keys(collection); len = keys.length; } | |
if(this.isArray(collection)){ isArray = true; len = collection.length;} | |
eachfunc.collection = collection; | |
eachfunc.size = len; | |
eachfunc.__ri__ = len - 1; | |
eachfunc.pos = 0; | |
eachfunc.finish = finish; | |
eachfunc.item = null; | |
eachfunc.next = function(){ | |
var item,key; | |
if(!isArray) key = keys[eachfunc.pos]; item = eachfunc.collection[key]; | |
if(isArray) key = eachfunc.pos; item = eachfunc.collection[key]; | |
if(eachfunc.pos >= eachfunc.size){ | |
if(eachfunc.finish) eachfunc.finish(eachfunc.item,key,eachfunc.collection); | |
return false; | |
} | |
eachfunc.pos++; | |
if(!item) return false; | |
eachfunc.item = item; | |
eachfunc(item,key,eachfunc.collection); | |
return item; | |
}; | |
return eachfunc; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment