Source

array/getNextIndex.js

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getNextIndex = void 0;
  4. /**
  5. * Get the next item index of the array
  6. * It handles with the length of the array so that the index won't overflow.
  7. * @category array
  8. * @module getNextIndex
  9. */
  10. function getNextIndex(prevIndex, totalLength, direction) {
  11. if (direction === void 0) { direction = 'forward'; }
  12. var isForward = direction === 'forward';
  13. var lastIndex = totalLength - 1;
  14. var nextIndex = (prevIndex === null
  15. ? isForward ? 0 : lastIndex
  16. : (
  17. // Determin if the move is within the bound
  18. isForward
  19. ? (prevIndex + 1 <= lastIndex)
  20. : (prevIndex > 0))
  21. ? prevIndex + (isForward ? 1 : -1)
  22. : isForward ? 0 : lastIndex);
  23. return nextIndex;
  24. }
  25. exports.getNextIndex = getNextIndex;
  26. exports.default = getNextIndex;