isIndex.js 797 B

1234567891011121314151617181920212223
  1. // @ts-nocheck
  2. /** Used as references for various `Number` constants. */
  3. const MAX_SAFE_INTEGER = 9007199254740991;
  4. /** Used to detect unsigned integer values. */
  5. const reIsUint = /^(?:0|[1-9]\d*)$/;
  6. /**
  7. * Checks if `value` is a valid array-like index.
  8. *
  9. * @private
  10. * @param {*} value The value to check.
  11. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  12. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  13. */
  14. function isIndex(value, length) {
  15. const type = typeof value;
  16. length = length == null ? MAX_SAFE_INTEGER : length;
  17. return (!!length &&
  18. (type === "number" || (type !== "symbol" && reIsUint.test(value))) &&
  19. value > -1 &&
  20. value % 1 === 0 &&
  21. value < length);
  22. }
  23. export default isIndex;