UNPKG

circular-at

Version:

Access array items at any positive or negative index. If the index is out of bounds, it will be wrapped around the length of the array.

6 lines (5 loc) 189 B
module.exports = function circularAt (array, index) { var length = array && array.length var idx = Math.abs(length + index % length) % length return array[isNaN(idx) ? index : idx] }