Jump forward or backward through an array by an index based on an item already in the array.
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.
npm install array-jumper
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'
If you like this, follow @HenrikJoreteg on twitter.
run them with
read them here: test.js