Last active
June 21, 2019 16:53
-
-
Save juanlatorre/3a0a897e448a918cb57ac2f35618e766 to your computer and use it in GitHub Desktop.
wrapper to add .next() and .prev() and current() to array, without prototype
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
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