isIndex.js 916 Bytes
Newer Older
徐立's avatar
徐立 committed
1 2
/** Used as references for various `Number` constants. */
/** 用作各种“数字”常量的引用。 */
3
const MAX_SAFE_INTEGER = 9007199254740991;
徐立's avatar
徐立 committed
4 5 6

/** Used to detect unsigned integer values. */
/** 用于检测无符号整数值。 */
7
const reIsUint = /^(?:0|[1-9]\d*)$/;
徐立's avatar
徐立 committed
8 9 10 11 12 13 14 15 16 17

/**
 * Checks if `value` is a valid array-like index.
 * 检查' value '是否是一个有效的类数组索引。
 * @private
 * @param {*} value 要检查的值。
 * @param {number} [length=MAX_SAFE_INTEGER] 有效索引的上界。
 * @returns {boolean} 如果' value '是有效的索引,则返回' true ',否则返回' false '。
 */
function isIndex(value, length) {
18 19
	const type = typeof value;
	length = length == null ? MAX_SAFE_INTEGER : length;
徐立's avatar
徐立 committed
20

21 22 23 24 25
	return (
		!!length &&
		(type === 'number' || (type !== 'symbol' && reIsUint.test(value))) &&
		(value > -1 && value % 1 == 0 && value < length)
	);
徐立's avatar
徐立 committed
26 27
}

28
export default isIndex;