From 80e441936042cebc01309c54c034a4fccb2869b9 Mon Sep 17 00:00:00 2001 From: haoziqaq <357229046@qq.com> Date: Tue, 5 Nov 2024 00:50:50 +0800 Subject: [PATCH] feat: support chunk --- docs/.vitepress/items/array.ts | 1 + docs/array/chunk.md | 24 ++++++++++++++++++++++++ docs/zh/array/chunk.md | 24 ++++++++++++++++++++++++ src/array.ts | 15 +++++++++++++++ tests/array.spec.ts | 10 ++++++++++ 5 files changed, 74 insertions(+) create mode 100644 docs/array/chunk.md create mode 100644 docs/zh/array/chunk.md diff --git a/docs/.vitepress/items/array.ts b/docs/.vitepress/items/array.ts index f0c9567..c0b0ad2 100644 --- a/docs/.vitepress/items/array.ts +++ b/docs/.vitepress/items/array.ts @@ -1,5 +1,6 @@ export const arrayItems = [ { text: 'at', link: '/array/at' }, + { text: 'chunk', link: '/array/chunk' }, { text: 'uniq', link: '/array/uniq' }, { text: 'uniqBy', link: '/array/uniq-by' }, { text: 'find', link: '/array/find' }, diff --git a/docs/array/chunk.md b/docs/array/chunk.md new file mode 100644 index 0000000..30f589d --- /dev/null +++ b/docs/array/chunk.md @@ -0,0 +1,24 @@ +# chunk + +Chunking an `array`. The passed `size` indicates the length of the chunk. + +### Usage + +```ts +import { chunk } from 'rattail' + +chunk([1, 2, 3], 2) // return [[1, 2], [3]] +``` + +### Arguments + +| Arg | Type | Defaults | +| ------ | -------- | -------- | +| `arr` | `Array` | | +| `size` | `number` | `1` | + +### Return + +| Type | +| ------- | +| `Array` | diff --git a/docs/zh/array/chunk.md b/docs/zh/array/chunk.md new file mode 100644 index 0000000..77c4d6d --- /dev/null +++ b/docs/zh/array/chunk.md @@ -0,0 +1,24 @@ +# chunk + +对 `数组` 进行分块。传递的 `size` 表示块的长度。 + +### 使用 + +```ts +import { chunk } from 'rattail' + +chunk([1, 2, 3], 2) // return [[1, 2], [3]] +``` + +### 参数 + +| 参数 | 类型 | 默认值 | +| ------ | -------- | ------ | +| `arr` | `Array` | | +| `size` | `number` | `1` | + +### 返回值 + +| 类型 | +| ------- | +| `Array` | diff --git a/src/array.ts b/src/array.ts index 59c006a..62522ab 100644 --- a/src/array.ts +++ b/src/array.ts @@ -1,4 +1,5 @@ import { isArray } from './general' +import { clamp } from './number' export function uniq(arr: T[]) { return [...new Set(arr)] @@ -80,3 +81,17 @@ export function shuffle(arr: T[]): T[] { } return arr } + +export function chunk(arr: T[], size = 1): T[][] { + size = clamp(size, 1, arr.length) + + const result: T[][] = [] + let index = 0 + + while (index < arr.length) { + result.push(arr.slice(index, index + size)) + index += size + } + + return result +} diff --git a/tests/array.spec.ts b/tests/array.spec.ts index 9456cf4..fa8386a 100644 --- a/tests/array.spec.ts +++ b/tests/array.spec.ts @@ -10,6 +10,7 @@ import { find, at, shuffle, + chunk, } from '../src' it('uniq', () => { @@ -84,3 +85,12 @@ it('shuffle', () => { const shuffled = shuffle([...arr]) expect(shuffled.sort()).toEqual(arr) }) + +it('chunk', () => { + expect(chunk([])).toEqual([]) + expect(chunk([1, 2, 3, 4, 5])).toEqual([[1], [2], [3], [4], [5]]) + expect(chunk([1, 2, 3, 4, 5], 0)).toEqual([[1], [2], [3], [4], [5]]) + expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]) + expect(chunk([1, 2], 2)).toEqual([[1, 2]]) + expect(chunk([1, 2], 3)).toEqual([[1, 2]]) +})