# isIndex

> "This is your life. Do what you love, and do it often." —@Holstee

本文为 《lodash 源码阅读》 系列文章，后续内容会在 [github](https://github.com/gu-xionghong/lodash-analysis) 中发布，欢迎 star，[gitbook](https://gu-xionghong.gitbook.io/lodash-analysis/) 同步更新。

## 源码

```javascript
const MAX_SAFE_INTEGER = 9007199254740991;

/** 用于检测无符号整数值 */
const reIsUint = /^(?:0|[1-9]\d*)$/;

/**
 * 检查 `value` 是否是一个有效的类数组索引。
 *
 * @private
 * @param {*} value 要检查的值。
 * @param {number} [length=MAX_SAFE_INTEGER] 有效索引的上限。
 * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
 */
function isIndex(value, length) {
  const type = typeof value;
  length = length == null ? MAX_SAFE_INTEGER : length;

  return !!length && (type == 'number' || (type != 'symbol' && reIsUint.test(value))) && (value > -1 && value % 1 == 0 && value < length);
}
```

## 知识点

在 [ES spec - ToLength](http://ecma-international.org/ecma-262/9.0/#sec-tolength) 中我们了解到，有效的 类数组 长度值需要具备以下条件：

1. 数值类型且为整数
2. 大于或等于 `0`
3. 小于或等于 `Number.MAX_SAFE_INTEGER`

## 相关链接

* [ES spec - ToLength](http://ecma-international.org/ecma-262/9.0/#sec-tolength)
