UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

53 lines (46 loc) 1.63 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _typeof = require('@babel/runtime/helpers/typeof'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof); /** * Used as references for various `Number` constants. * @private */ var MAX_SAFE_INTEGER = 9007199254740991; /** * Used to detect unsigned integer values. * @private */ var reIsUint = /^(?:0|[1-9]\d*)$/; /** * Checks if `value` is a valid array-like index. * * @param {*} value The value to check. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`. * @example * ```ts * isIndex(0); // true * isIndex(1); // true * isIndex(100); // true * isIndex('0'); // true * isIndex('999'); // true * isIndex(-1); // false * isIndex(1.5); // false * isIndex('abc'); // false * isIndex('01'); // false // 前导 0 * isIndex(5, 5); // false // value 必须 < length * isIndex(4, 5); // true * isIndex(0, 0); // false // length 为 0 * isIndex(Symbol('test')); // false * isIndex(null); // false * isIndex(undefined); // false * ``` */ function isIndex(value) { var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : MAX_SAFE_INTEGER; var type = _typeof__default["default"](value); return !!length && (type == 'number' || type != 'symbol' && reIsUint.test(value)) && value > -1 && value % 1 == 0 && value < length; } exports.isIndex = isIndex;