Skip to content

Instantly share code, notes, and snippets.

@juanlatorre
Last active June 21, 2019 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanlatorre/3a0a897e448a918cb57ac2f35618e766 to your computer and use it in GitHub Desktop.
Save juanlatorre/3a0a897e448a918cb57ac2f35618e766 to your computer and use it in GitHub Desktop.
wrapper to add .next() and .prev() and current() to array, without prototype
const iterifyArr = function(arr) {
var cur = 0;
arr.next = function() {
if (++cur <= this.length - 1) {
return this[cur];
} else {
cur = 0;
return this[cur];
}
};
arr.prev = function() {
if (--cur < 0) {
cur = this.length - 1;
return this[cur];
} else {
return this[cur];
}
};
arr.current = function() {
return this[cur];
};
return arr;
};
module.exports = iterifyArr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment