From 04f79ce68cd92d2ae902e96bc532de8cb857cc42 Mon Sep 17 00:00:00 2001 From: Ethan Zhou <149743008+Aybrea@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:09:11 +0800 Subject: [PATCH] docs: add array docs (#13) * docs: add array docs * chore: ignore vitepress cache files --- .gitignore | 3 ++- docs/.vitepress/config.ts | 10 +++++++++- docs/.vitepress/items/array.ts | 14 ++++++++++++++ docs/.vitepress/items/index.ts | 1 + docs/array/at.md | 24 ++++++++++++++++++++++++ docs/array/find.md | 25 +++++++++++++++++++++++++ docs/array/normalizeToArray.md | 24 ++++++++++++++++++++++++ docs/array/removeArrayBlank.md | 23 +++++++++++++++++++++++ docs/array/removeArrayEmpty.md | 23 +++++++++++++++++++++++ docs/array/removeItem.md | 25 +++++++++++++++++++++++++ docs/array/shuffle.md | 23 +++++++++++++++++++++++ docs/array/sum.md | 23 +++++++++++++++++++++++ docs/array/sumBy.md | 24 ++++++++++++++++++++++++ docs/array/toggleItem.md | 26 ++++++++++++++++++++++++++ docs/array/uniq.md | 24 ++++++++++++++++++++++++ docs/array/uniqBy.md | 24 ++++++++++++++++++++++++ docs/zh/array/at.md | 24 ++++++++++++++++++++++++ docs/zh/array/find.md | 25 +++++++++++++++++++++++++ docs/zh/array/normalizeToArray.md | 24 ++++++++++++++++++++++++ docs/zh/array/removeArrayBlank.md | 23 +++++++++++++++++++++++ docs/zh/array/removeArrayEmpty.md | 23 +++++++++++++++++++++++ docs/zh/array/removeItem.md | 25 +++++++++++++++++++++++++ docs/zh/array/shuffle.md | 23 +++++++++++++++++++++++ docs/zh/array/sum.md | 23 +++++++++++++++++++++++ docs/zh/array/sumBy.md | 24 ++++++++++++++++++++++++ docs/zh/array/toggleItem.md | 26 ++++++++++++++++++++++++++ docs/zh/array/uniq copy.md | 24 ++++++++++++++++++++++++ docs/zh/array/uniq.md | 24 ++++++++++++++++++++++++ docs/zh/array/uniqBy copy.md | 24 ++++++++++++++++++++++++ docs/zh/array/uniqBy.md | 24 ++++++++++++++++++++++++ 30 files changed, 650 insertions(+), 2 deletions(-) create mode 100644 docs/.vitepress/items/array.ts create mode 100644 docs/array/at.md create mode 100644 docs/array/find.md create mode 100644 docs/array/normalizeToArray.md create mode 100644 docs/array/removeArrayBlank.md create mode 100644 docs/array/removeArrayEmpty.md create mode 100644 docs/array/removeItem.md create mode 100644 docs/array/shuffle.md create mode 100644 docs/array/sum.md create mode 100644 docs/array/sumBy.md create mode 100644 docs/array/toggleItem.md create mode 100644 docs/array/uniq.md create mode 100644 docs/array/uniqBy.md create mode 100644 docs/zh/array/at.md create mode 100644 docs/zh/array/find.md create mode 100644 docs/zh/array/normalizeToArray.md create mode 100644 docs/zh/array/removeArrayBlank.md create mode 100644 docs/zh/array/removeArrayEmpty.md create mode 100644 docs/zh/array/removeItem.md create mode 100644 docs/zh/array/shuffle.md create mode 100644 docs/zh/array/sum.md create mode 100644 docs/zh/array/sumBy.md create mode 100644 docs/zh/array/toggleItem.md create mode 100644 docs/zh/array/uniq copy.md create mode 100644 docs/zh/array/uniq.md create mode 100644 docs/zh/array/uniqBy copy.md create mode 100644 docs/zh/array/uniqBy.md diff --git a/.gitignore b/.gitignore index 68f1b47..ac725b8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ lib coverage .DS_Store docs/.vitepress/cache -docs/.vitepress/dist \ No newline at end of file +docs/.vitepress/dist +.vitepress/cache diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 7ea3957..81da82f 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -1,5 +1,5 @@ import { defineConfig } from 'vitepress' -import { generalItems, numberItems, stringItems } from './items' +import { generalItems, numberItems, stringItems, arrayItems } from './items' function withI18n(items: { link: string; text: string }[], locale: 'zh') { return items.map((item) => { @@ -43,6 +43,10 @@ export default defineConfig({ text: '字符串', items: withI18n(stringItems, 'zh'), }, + { + text: '数组', + items: withI18n(arrayItems, 'zh'), + }, ], docFooter: { @@ -81,6 +85,10 @@ export default defineConfig({ text: 'String', items: stringItems, }, + { + text: 'Array', + items: arrayItems, + }, ], socialLinks: [{ icon: 'github', link: 'https://github.com/varletjs/rattail' }], diff --git a/docs/.vitepress/items/array.ts b/docs/.vitepress/items/array.ts new file mode 100644 index 0000000..7555905 --- /dev/null +++ b/docs/.vitepress/items/array.ts @@ -0,0 +1,14 @@ +export const arrayItems = [ + { text: 'uniq', link: '/array/uniq' }, + { text: 'uniqBy', link: '/array/uniqBy' }, + { text: 'normalizeToArray', link: '/array/normalizeToArray' }, + { text: 'removeItem', link: '/array/removeItem' }, + { text: 'toggleItem', link: '/array/toggleItem' }, + { text: 'removeArrayBlank', link: '/array/removeArrayBlank' }, + { text: 'removeArrayEmpty', link: '/array/removeArrayEmpty' }, + { text: 'find', link: '/array/find' }, + { text: 'at', link: '/array/at' }, + { text: 'shuffle', link: '/array/shuffle' }, + { text: 'sum', link: '/array/sum' }, + { text: 'sumBy', link: '/array/sumBy' }, +] diff --git a/docs/.vitepress/items/index.ts b/docs/.vitepress/items/index.ts index 513a636..cfe1d48 100644 --- a/docs/.vitepress/items/index.ts +++ b/docs/.vitepress/items/index.ts @@ -1,3 +1,4 @@ export * from './general' export * from './string' export * from './number' +export * from './array' diff --git a/docs/array/at.md b/docs/array/at.md new file mode 100644 index 0000000..d10ef8c --- /dev/null +++ b/docs/array/at.md @@ -0,0 +1,24 @@ +# at + +Retrieves the element at a specified index in an array, supporting negative indices. + +### Usage + +```ts +import { at } from 'rattail' + +at([1, 2, 3], -1) // returns 3 +``` + +### Arguments + +| Arg | Type | Defaults | +| ------- | -------- | -------- | +| `arr` | `Array` | | +| `index` | `number` | | + +### Return + +| Type | +| ----- | +| `any` | diff --git a/docs/array/find.md b/docs/array/find.md new file mode 100644 index 0000000..f15fdb2 --- /dev/null +++ b/docs/array/find.md @@ -0,0 +1,25 @@ +# find + +Finds the first or last element in an array that meets a specified condition, returning the element and its index. + +### Usage + +```ts +import { find } from 'rattail' + +find([1, 2, 3], (item) => item > 1) // returns [2, 1] +``` + +### Arguments + +| Arg | Type | Defaults | +| ------ | ------------------------------------------------- | --------- | +| `arr` | `Array` | | +| `fn` | `(item: any, index: number, array: Array) => any` | | +| `from` | `'start' \| 'end'` | `'start'` | + +### Return + +| Type | +| ----------------------------- | +| `[any, number] \| [null, -1]` | diff --git a/docs/array/normalizeToArray.md b/docs/array/normalizeToArray.md new file mode 100644 index 0000000..318c8cf --- /dev/null +++ b/docs/array/normalizeToArray.md @@ -0,0 +1,24 @@ +# normalizeToArray + +Converts a value to an array if it is not already an array. If the input is an array, it returns the input as-is. + +### Usage + +```ts +import { normalizeToArray } from 'rattail' + +normalizeToArray(5) // returns [5] +normalizeToArray([1, 2, 3]) // returns [1, 2, 3] +``` + +### Arguments + +| Arg | Type | Defaults | +| ------- | ----- | -------- | +| `value` | `any` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/array/removeArrayBlank.md b/docs/array/removeArrayBlank.md new file mode 100644 index 0000000..2941ca0 --- /dev/null +++ b/docs/array/removeArrayBlank.md @@ -0,0 +1,23 @@ +# removeArrayBlank + +Removes `null` or `undefined` values from an array. + +### Usage + +```ts +import { removeArrayBlank } from 'rattail' + +removeArrayBlank([1, null, 2, undefined, 3]) // returns [1, 2, 3] +``` + +### Arguments + +| Arg | Type | Defaults | +| --- | ----- | -------- | --- | +| | `arr` | `Array` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/array/removeArrayEmpty.md b/docs/array/removeArrayEmpty.md new file mode 100644 index 0000000..142512e --- /dev/null +++ b/docs/array/removeArrayEmpty.md @@ -0,0 +1,23 @@ +# removeArrayEmpty + +Removes `null`, `undefined`, or empty string (`''`) values from an array. + +### Usage + +```ts +import { removeArrayEmpty } from 'rattail' + +removeArrayEmpty([1, null, '', 3]) // returns [1, 3] +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ------- | -------- | +| `arr` | `Array` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/array/removeItem.md b/docs/array/removeItem.md new file mode 100644 index 0000000..fd5fb91 --- /dev/null +++ b/docs/array/removeItem.md @@ -0,0 +1,25 @@ +# removeItem + +Removes the first occurrence of a specific item from an array and returns the removed item. + +### Usage + +```ts +import { removeItem } from 'rattail' + +const arr = [1, 2, 3] +removeItem(arr, 2) // arr becomes [1, 3] +``` + +### Arguments + +| Arg | Type | Defaults | +| ------ | ------- | -------- | +| `arr` | `Array` | | +| `item` | `any` | | + +### Return + +| Type | +| ----- | +| `any` | diff --git a/docs/array/shuffle.md b/docs/array/shuffle.md new file mode 100644 index 0000000..927990c --- /dev/null +++ b/docs/array/shuffle.md @@ -0,0 +1,23 @@ +# shuffle + +Randomly shuffles elements within an array. + +### Usage + +```ts +import { shuffle } from 'rattail' + +shuffle([1, 2, 3]) // returns [2, 1, 3] (output may vary) +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ------- | -------- | +| `arr` | `Array` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/array/sum.md b/docs/array/sum.md new file mode 100644 index 0000000..bafc148 --- /dev/null +++ b/docs/array/sum.md @@ -0,0 +1,23 @@ +# sum + +Calculates the sum of values in an array of numbers. + +### Usage + +```ts +import { sum } from 'rattail' + +sum([1, 2, 3]) // returns 6 +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ---------- | -------- | +| `arr` | `number[]` | | + +### Return + +| Type | +| -------- | +| `number` | diff --git a/docs/array/sumBy.md b/docs/array/sumBy.md new file mode 100644 index 0000000..e3f03d1 --- /dev/null +++ b/docs/array/sumBy.md @@ -0,0 +1,24 @@ +# sumBy + +Calculates the sum of values in an array based on a provided function. + +### Usage + +```ts +import { sumBy } from 'rattail' + +sumBy([{ n: 1 }, { n: 2 }, { n: 3 }], (item) => item.n) // returns 6 +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ---------------------- | -------- | +| `arr` | `Array` | | +| `fn` | `(val: any) => number` | | + +### Return + +| Type | +| -------- | +| `number` | diff --git a/docs/array/toggleItem.md b/docs/array/toggleItem.md new file mode 100644 index 0000000..a04e557 --- /dev/null +++ b/docs/array/toggleItem.md @@ -0,0 +1,26 @@ +# toggleItem + +Adds or removes an item from an array, based on its existence in the array. + +### Usage + +```ts +import { toggleItem } from 'rattail' + +const arr = [1, 2] +toggleItem(arr, 2) // arr becomes [1] +toggleItem(arr, 3) // arr becomes [1, 3] +``` + +### Arguments + +| Arg | Type | Defaults | +| ------ | ------- | -------- | +| `arr` | `Array` | | +| `item` | `any` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/array/uniq.md b/docs/array/uniq.md new file mode 100644 index 0000000..543f050 --- /dev/null +++ b/docs/array/uniq.md @@ -0,0 +1,24 @@ +# uniq + +Creates a duplicate-free version of an array, using the values' equality. + +### Usage + +```ts +import { uniq } from 'rattail' + +uniq([1, 2, 2, 3]) // returns [1, 2, 3] +uniq(['a', 'a', 'b', 'c']) // returns ['a', 'b', 'c'] +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ------- | -------- | +| `arr` | `Array` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/array/uniqBy.md b/docs/array/uniqBy.md new file mode 100644 index 0000000..24c452b --- /dev/null +++ b/docs/array/uniqBy.md @@ -0,0 +1,24 @@ +# uniqBy + +Creates a duplicate-free version of an array, using a custom comparison function to determine uniqueness. + +### Usage + +```ts +import { uniqBy } from 'rattail' + +uniqBy([{ id: 1 }, { id: 2 }, { id: 1 }], (a, b) => a.id === b.id) // returns [{ id: 1 }, { id: 2 }] +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ----------------------------- | -------- | +| `arr` | `Array` | | +| `fn` | `(a: any, b: any) => boolean` | | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/zh/array/at.md b/docs/zh/array/at.md new file mode 100644 index 0000000..3f7fe73 --- /dev/null +++ b/docs/zh/array/at.md @@ -0,0 +1,24 @@ +# at + +获取数组中指定索引的元素,支持负索引。 + +### 使用 + +```ts +import { at } from 'rattail' + +at([1, 2, 3], -1) // return 3 +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ------- | -------- | ------ | +| `arr` | `Array` | | +| `index` | `number` | | + +### 返回值 + +| 类型 | +| ----- | +| `any` | diff --git a/docs/zh/array/find.md b/docs/zh/array/find.md new file mode 100644 index 0000000..7a9c18c --- /dev/null +++ b/docs/zh/array/find.md @@ -0,0 +1,25 @@ +# find + +在数组中查找第一个或最后一个满足指定条件的元素,返回该元素及其索引。 + +### 使用 + +```ts +import { find } from 'rattail' + +find([1, 2, 3], (item) => item > 1) // return [2, 1] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ------ | ------------------------------------------------- | --------- | +| `arr` | `Array` | | +| `fn` | `(item: any, index: number, array: Array) => any` | | +| `from` | `'start' \| 'end'` | `'start'` | + +### 返回值 + +| 类型 | +| ----------------------------- | +| `[any, number] \| [null, -1]` | diff --git a/docs/zh/array/normalizeToArray.md b/docs/zh/array/normalizeToArray.md new file mode 100644 index 0000000..74cec61 --- /dev/null +++ b/docs/zh/array/normalizeToArray.md @@ -0,0 +1,24 @@ +# normalizeToArray + +将值转换为数组,如果输入已经是数组,则直接返回输入值。 + +### 使用 + +```ts +import { normalizeToArray } from 'rattail' + +normalizeToArray(5) // return [5] +normalizeToArray([1, 2, 3]) // return [1, 2, 3] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ------- | ----- | ------ | +| `value` | `any` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/docs/zh/array/removeArrayBlank.md b/docs/zh/array/removeArrayBlank.md new file mode 100644 index 0000000..4767846 --- /dev/null +++ b/docs/zh/array/removeArrayBlank.md @@ -0,0 +1,23 @@ +# removeArrayBlank + +移除数组中值为 `null` 或 `undefined` 的元素。 + +### 使用 + +```ts +import { removeArrayBlank } from 'rattail' + +removeArrayBlank([1, null, 2, undefined, 3]) // return [1, 2, 3] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ------- | ------ | +| `arr` | `Array` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/docs/zh/array/removeArrayEmpty.md b/docs/zh/array/removeArrayEmpty.md new file mode 100644 index 0000000..169ac89 --- /dev/null +++ b/docs/zh/array/removeArrayEmpty.md @@ -0,0 +1,23 @@ +# removeArrayEmpty + +移除数组中值为 `null`、`undefined` 或空字符串(`''`)的元素。 + +### 使用 + +```ts +import { removeArrayEmpty } from 'rattail' + +removeArrayEmpty([1, null, undefined, '', 3]) // return [1, 3] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ------- | ------ | +| `arr` | `Array` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/docs/zh/array/removeItem.md b/docs/zh/array/removeItem.md new file mode 100644 index 0000000..3d32ae1 --- /dev/null +++ b/docs/zh/array/removeItem.md @@ -0,0 +1,25 @@ +# removeItem + +从数组中移除第一次出现的指定元素,返回移除的元素。 + +### 使用 + +```ts +import { removeItem } from 'rattail' + +const arr = [1, 2, 3] +removeItem(arr, 2) // arr 变为 [1, 3] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ------ | ------- | ------ | +| `arr` | `Array` | | +| `item` | `any` | | + +### 返回值 + +| 类型 | +| ----- | +| `any` | diff --git a/docs/zh/array/shuffle.md b/docs/zh/array/shuffle.md new file mode 100644 index 0000000..4730b1c --- /dev/null +++ b/docs/zh/array/shuffle.md @@ -0,0 +1,23 @@ +# shuffle + +随机打乱数组中的元素。 + +### 使用 + +```ts +import { shuffle } from 'rattail' + +shuffle([1, 2, 3]) // return [2, 1, 3] (输出可能不同) +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ------- | ------ | +| `arr` | `Array` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/docs/zh/array/sum.md b/docs/zh/array/sum.md new file mode 100644 index 0000000..bd0d7e0 --- /dev/null +++ b/docs/zh/array/sum.md @@ -0,0 +1,23 @@ +# sum + +计算数字数组中各项值的和。 + +### 使用 + +```ts +import { sum } from 'rattail' + +sum([1, 2, 3]) // return 6 +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ---------- | ------ | +| `arr` | `number[]` | | + +### 返回值 + +| 类型 | +| -------- | +| `number` | diff --git a/docs/zh/array/sumBy.md b/docs/zh/array/sumBy.md new file mode 100644 index 0000000..346d6b2 --- /dev/null +++ b/docs/zh/array/sumBy.md @@ -0,0 +1,24 @@ +# sumBy + +根据提供的函数计算数组中各项值的和。 + +### 使用 + +```ts +import { sumBy } from 'rattail' + +sumBy([{ n: 1 }, { n: 2 }, { n: 3 }], (item) => item.n) // return 6 +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ---------------------- | ------ | +| `arr` | `Array` | | +| `fn` | `(val: any) => number` | | + +### 返回值 + +| 类型 | +| -------- | +| `number` | diff --git a/docs/zh/array/toggleItem.md b/docs/zh/array/toggleItem.md new file mode 100644 index 0000000..2cc5077 --- /dev/null +++ b/docs/zh/array/toggleItem.md @@ -0,0 +1,26 @@ +# toggleItem + +根据元素是否在数组中,将其添加到数组末尾或从数组中移除。 + +### 使用 + +```ts +import { toggleItem } from 'rattail' + +const arr = [1, 2] +toggleItem(arr, 2) // arr 变为 [1] +toggleItem(arr, 3) // arr 变为 [1, 3] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ------ | ------- | ------ | +| `arr` | `Array` | | +| `item` | `any` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/docs/zh/array/uniq copy.md b/docs/zh/array/uniq copy.md new file mode 100644 index 0000000..b56b247 --- /dev/null +++ b/docs/zh/array/uniq copy.md @@ -0,0 +1,24 @@ +# uniq + +Creates a duplicate-free version of an array, using the values' equality. + +### Usage + +```ts +import { uniq } from 'rattail' + +uniq([1, 2, 2, 3]) // returns [1, 2, 3] +uniq(['a', 'a', 'b', 'c']) // returns ['a', 'b', 'c'] +``` + +### Arguments + +| Arg | Type | Defaults | +| --- | ---- | -------- | +| arr | T[] | | + +### Return + +| Type | +| ---- | +| T[] | diff --git a/docs/zh/array/uniq.md b/docs/zh/array/uniq.md new file mode 100644 index 0000000..633c77a --- /dev/null +++ b/docs/zh/array/uniq.md @@ -0,0 +1,24 @@ +# uniq + +返回去重后的数组。 + +### 使用 + +```ts +import { uniq } from 'rattail' + +uniq([1, 2, 2, 3]) // return [1, 2, 3] +uniq(['a', 'a', 'b', 'c']) // return ['a', 'b', 'c'] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ------- | ------ | +| `arr` | `Array` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/docs/zh/array/uniqBy copy.md b/docs/zh/array/uniqBy copy.md new file mode 100644 index 0000000..a9b0059 --- /dev/null +++ b/docs/zh/array/uniqBy copy.md @@ -0,0 +1,24 @@ +# uniqBy + +Creates a duplicate-free version of an array, using a custom comparison function to determine uniqueness. + +### Usage + +```ts +import { uniqBy } from 'rattail' + +uniqBy([{ id: 1 }, { id: 2 }, { id: 1 }], (a, b) => a.id === b.id) // returns [{ id: 1 }, { id: 2 }] +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | ------------------------- | -------- | +| `arr` | `T[]` | | +| `fn` | `(a: T, b: T) => boolean` | | + +### Return + +| Type | +| ----- | +| `T[]` | diff --git a/docs/zh/array/uniqBy.md b/docs/zh/array/uniqBy.md new file mode 100644 index 0000000..d6c2375 --- /dev/null +++ b/docs/zh/array/uniqBy.md @@ -0,0 +1,24 @@ +# uniqBy + +使用自定义比较函数返回去重后的数组。 + +### 使用 + +```ts +import { uniqBy } from 'rattail' + +uniqBy([{ id: 1 }, { id: 2 }, { id: 1 }], (a, b) => a.id === b.id) // return [{ id: 1 }, { id: 2 }] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ----- | ----------------------------- | ------ | +| `arr` | `Array` | | +| `fn` | `(a: any, b: any) => boolean` | | + +### 返回值 + +| 类型 | +| ------- | +| `Array` |