isIndex.js 778 B

123456789101112131415161718192021222324
  1. /** Used to detect unsigned integer values. */
  2. var reIsUint = /^\d+$/;
  3. /**
  4. * Used as the [maximum length](http://ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer)
  5. * of an array-like value.
  6. */
  7. var MAX_SAFE_INTEGER = 9007199254740991;
  8. /**
  9. * Checks if `value` is a valid array-like index.
  10. *
  11. * @private
  12. * @param {*} value The value to check.
  13. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  14. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  15. */
  16. function isIndex(value, length) {
  17. value = (typeof value == 'number' || reIsUint.test(value)) ? +value : -1;
  18. length = length == null ? MAX_SAFE_INTEGER : length;
  19. return value > -1 && value % 1 == 0 && value < length;
  20. }
  21. module.exports = isIndex;