array-cycle
Simple wrapper utility to navigate and cycle an array
Install
npm i array-cycle
API
constructor(array)
const Cycle = var cycle = 'a' 'b' 'c' 'd' // also allowedvar cycle =
index
Getter/setter to navigate the array. Loop if out of range
var cycle = // 0cycleindex cycleindex = 1 // 1cycleindex cycleindex -= 2 // 3cycleindex
keys
Getter. Return an object containing the current, next and previous indexes
var cycle = /*{ curr: 0, next: 1, prev: 3}*/cyclekeys
values
Getter. Return an object containing the current, next and previous values
var cycle = /*{ curr: 'a', next: 'b', prev: 'd'}*/cyclevalues
last
Getter. Return an object containing the index and value of the last item
var cycle = /*{ key: 3, value: 'd'}*/cyclevalues
Example
const Cycle = var cycle = // 0cycleindex /*{ curr: 0, next: 1, prev: 3}*/cyclekeys /*{ curr: 'a', next: 'b', prev: 'd'}*/cyclevalues /*{ key: 3, value: 'd'}*/cyclelast // walk one step forwardcycleindex++ // 1cycleindex /*{ curr: 1, next: 2, prev: 0}*/cyclekeys // walk three steps forward (and loop to the beginning)cycleindex += 3 /*{ curr: 0, next: 1, prev: 3}*/cyclekeys
License
MIT