array-jumper
Jump forward or backward through an array by an index based on an item already in the array.
Last updated 7 years ago by henrikjoreteg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install array-jumper 
SYNC missed versions from official npm registry.

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 = require('array-jumper');

// our demo array
var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];


// If non are specified jumper should return first
jumper(arr); // returns 'a'

// If no jump amount specified jumper should return
// next item in array
jumper(arr, 'a') // returns 'b'

// If jump amount specified is in range, jump to item
jumper(arr, 'a', 2) // returns 'c'

// Should work for negative
jumper(arr, 'c', -2) // returns 'a'

// Should work for zero
jumper(arr, 'a', 0) // returns 'a'

// Should loop around if given positive jump number that
// is out of range.
jumper(arr, 'a', 10) // returns 'd'

// Should loop around if given negative jump number that
// is out of range
jumper(arr, 'a', -10) // returns 'e'

// should work for item not found with no jump value
jumper(arr, 'z') // returns 'a'

// should work for item not found with jump value 1
jumper(arr, 'z', 1) // returns 'a'

// should work for item not found with jump value 2
jumper(arr, 'z', 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

Current Tags

  • 1.0.0                                ...           latest (7 years ago)

2 Versions

  • 1.0.0                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |