UNPKG

t-comm

Version:

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

45 lines (42 loc) 1.33 kB
import _typeof from '@babel/runtime/helpers/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(value); return !!length && (type == 'number' || type != 'symbol' && reIsUint.test(value)) && value > -1 && value % 1 == 0 && value < length; } export { isIndex };