Skip to content

Instantly share code, notes, and snippets.

@influx6
Created September 21, 2012 21:14
Show Gist options
  • Save influx6/3763937 to your computer and use it in GitHub Desktop.
Save influx6/3763937 to your computer and use it in GitHub Desktop.
Iterator payload
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