array-jumper
Solves an annoying problem I've run into frequently with various cases where I want to get another item in an array based on some current value that I know is in the array.
installing
npm install array-jumper
how to use
var jumper = ; // our demo arrayvar arr = 'a' 'b' 'c' 'd' 'e' 'f' 'g'; // If non are specified jumper should return first; // returns 'a' // If no jump amount specified jumper should return// next item in array // returns 'b' // If jump amount specified is in range, jump to item // returns 'c' // Should work for negative // returns 'a' // Should work for zero // returns 'a' // Should loop around if given positive jump number that// is out of range. // returns 'd' // Should loop around if given negative jump number that// is out of range // returns 'e' // should work for item not found with no jump value // returns 'a' // should work for item not found with jump value 1 // returns 'a' // should work for item not found with jump value 2 // returns 'b'
credits
If you like this, follow @HenrikJoreteg on twitter.
Check out similar resources at: http://resources.humanjavascript.com.
And check out my book: http://humanjavascript.com
unit tests?
run them with npm test
read them here: test.js
changelog
- 1.0.0: no longer throwing errors, and now handling missing args better
- 0.0.1: initial release
license
MIT