Source

array/getNextIndex.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNextIndex = void 0;
/**
 * Get the next item index of the array
 * It handles with the length of the array so that the index won't overflow.
 * @category array
 * @module getNextIndex
 */
function getNextIndex(prevIndex, totalLength, direction) {
    if (direction === void 0) { direction = 'forward'; }
    var isForward = direction === 'forward';
    var lastIndex = totalLength - 1;
    var nextIndex = (prevIndex === null
        ? isForward ? 0 : lastIndex
        : (
        // Determin if the move is within the bound
        isForward
            ? (prevIndex + 1 <= lastIndex)
            : (prevIndex > 0))
            ? prevIndex + (isForward ? 1 : -1)
            : isForward ? 0 : lastIndex);
    return nextIndex;
}
exports.getNextIndex = getNextIndex;
exports.default = getNextIndex;