From 1e20a97134471e38d50e8c4b31cc36fc5600836b Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Sat, 20 Jul 2024 11:21:19 +0900 Subject: [PATCH] docs: Add docs for isLength --- docs/.vitepress/zh_hans.mts | 3 +- docs/reference/predicate/isLength.md | 2 +- docs/zh_hans/reference/predicate/isLength.md | 41 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 docs/zh_hans/reference/predicate/isLength.md diff --git a/docs/.vitepress/zh_hans.mts b/docs/.vitepress/zh_hans.mts index d6fcf5e6..624a44d3 100644 --- a/docs/.vitepress/zh_hans.mts +++ b/docs/.vitepress/zh_hans.mts @@ -147,6 +147,7 @@ function sidebar(): DefaultTheme.Sidebar { text: '谓词', items: [ { 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: 'isNil', link: '/zh_hans/reference/predicate/isNil' }, { 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: 'endsWith (兼容性)', link: '/zh_hans/reference/string/endsWith' }, ], - } + }, ], }, ]; diff --git a/docs/reference/predicate/isLength.md b/docs/reference/predicate/isLength.md index e9279600..182aa2f3 100644 --- a/docs/reference/predicate/isLength.md +++ b/docs/reference/predicate/isLength.md @@ -2,7 +2,7 @@ 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`). diff --git a/docs/zh_hans/reference/predicate/isLength.md b/docs/zh_hans/reference/predicate/isLength.md new file mode 100644 index 00000000..feebb22d --- /dev/null +++ b/docs/zh_hans/reference/predicate/isLength.md @@ -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 +```