_isIndex.js 707 B

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