docs: Add docs for isLength

This commit is contained in:
Sojin Park 2024-07-20 11:21:19 +09:00
parent caedf69aed
commit 1e20a97134
3 changed files with 44 additions and 2 deletions

View File

@ -147,6 +147,7 @@ function sidebar(): DefaultTheme.Sidebar {
text: '谓词', text: '谓词',
items: [ items: [
{ text: 'isEqual', link: '/zh_hans/reference/predicate/isEqual' }, { text: 'isEqual', link: '/zh_hans/reference/predicate/isEqual' },
{ text: 'isLength', link: '/zh_hans/reference/predicate/isLength' },
{ text: 'isPlainObject', link: '/zh_hans/reference/predicate/isPlainObject' }, { text: 'isPlainObject', link: '/zh_hans/reference/predicate/isPlainObject' },
{ text: 'isNil', link: '/zh_hans/reference/predicate/isNil' }, { text: 'isNil', link: '/zh_hans/reference/predicate/isNil' },
{ text: 'isNotNil', link: '/zh_hans/reference/predicate/isNotNil' }, { text: 'isNotNil', link: '/zh_hans/reference/predicate/isNotNil' },
@ -173,7 +174,7 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'startsWith (兼容性)', link: '/zh_hans/reference/string/startsWith' }, { text: 'startsWith (兼容性)', link: '/zh_hans/reference/string/startsWith' },
{ text: 'endsWith (兼容性)', link: '/zh_hans/reference/string/endsWith' }, { text: 'endsWith (兼容性)', link: '/zh_hans/reference/string/endsWith' },
], ],
} },
], ],
}, },
]; ];

View File

@ -2,7 +2,7 @@
Checks if a given value is a valid length. Checks if a given value is a valid length.
This function tests whether the provided value is of type `number`, is a non-negative integer, and is less than or equal to JavaScript's maximum safe integer (`Number.MAX_SAFE_INTEGER`). It returns `true` if the value is a valid length, and `false` otherwise. A valid length is of type `number`, is a non-negative integer, and is less than or equal to JavaScript's maximum safe integer (`Number.MAX_SAFE_INTEGER`). It returns `true` if the value is a valid length, and `false` otherwise.
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to a valid length (`number`). This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to a valid length (`number`).

View File

@ -0,0 +1,41 @@
# isLength
检查给定的值是否是一个有效的长度。
该函数测试提供的值是否为 `number` 类型,是非负整数,并且小于或等于 JavaScript 的最大安全整数 (`Number.MAX_SAFE_INTEGER`)。如果该值是一个有效的长度,则返回 `true`,否则返回 `false`
此函数还可以作为 TypeScript 中的类型预测器,将参数的类型缩小为有效的长度 (`number`)。
## 签名
```typescript
function isLength(value: unknown): value is number;
```
### 参数
- `value` (`unknown`): 要检查是否为有效长度的值。
### 返回值
(`value is number`): 如果值是有效长度,则返回 `true`,否则返回 `false`
## 示例
```typescript
import { isLength } from 'es-toolkit/predicate';
const value1 = 0;
const value2 = 42;
const value3 = -1;
const value4 = 1.5;
const value5 = Number.MAX_SAFE_INTEGER;
const value6 = Number.MAX_SAFE_INTEGER + 1;
console.log(isLength(value1)); // true
console.log(isLength(value2)); // true
console.log(isLength(value3)); // false
console.log(isLength(value4)); // false
console.log(isLength(value5)); // true
console.log(isLength(value6)); // false
```