From 198abc121efd50a0b68e2a6328b35fcf11469872 Mon Sep 17 00:00:00 2001 From: vyron Date: Sat, 2 Apr 2022 12:23:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(array):=20=E6=96=B0=E5=A2=9Echunk,compact,?= =?UTF-8?q?head,first,last,flattenDepth,flattenDeep,flatten?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/array/package.json | 3 +++ packages/array/src/index.ts | 44 ++++++++++++++++++++++++++++++++++++- pnpm-lock.yaml | 6 +++++ pnpm-workspace.yaml | 2 +- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/packages/array/package.json b/packages/array/package.json index f89e2cf..c1afc85 100644 --- a/packages/array/package.json +++ b/packages/array/package.json @@ -21,5 +21,8 @@ "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" + }, + "dependencies": { + "@v-utils/type": "workspace:^0.0.1" } } \ No newline at end of file diff --git a/packages/array/src/index.ts b/packages/array/src/index.ts index 1f06e85..7e0c36c 100644 --- a/packages/array/src/index.ts +++ b/packages/array/src/index.ts @@ -1,10 +1,52 @@ /* * @Author: vyron * @Date: 2022-03-24 14:48:36 - * @LastEditTime: 2022-03-24 14:48:37 + * @LastEditTime: 2022-04-01 15:44:39 * @LastEditors: vyron * @Description: 数组常用方法 * @FilePath: /v-utils/packages/array/src/index.ts */ +// 判断是否为数组 export const isArray = Array.isArray + +// 将数组拆分成多个长度为 size 的块,并将多个块组成一个新数组并返回; +// 如果 array 无法被分割成全部等长的块,剩余的元素将组成一个块 +export const chunk = (array: any[], size: number = 1) => { + const result: any[] = [] + let index = 0 + while (index < array.length) { + result.push(array.slice(index, index + size)) + index += size + } + return result +} + +// 过滤并返回数组中所有的 truthy 值元素 +export const compact = (array: any[]) => array.filter(Boolean) + +// 返回数组的第一个元素,如果没有则返回 undefined +export const head = (array: any[]) => (isArray(array) ? array[0] : undefined) + +export const first = head + +// 返回数组的最后一个元素,如果没有则返回 undefined +export const last = (array: any[]) => + isArray(array) ? array[array.length - 1] : undefined + +// 根据 depth 递归减少 array 的嵌套层级 +export const flattenDepth = (array: any[], depth = 1): any[] => { + if (!isArray(array) || depth < 1) return array + return array.reduce((acc, val) => { + if (isArray(val)) { + return acc.concat(flattenDepth(val, depth - 1)) + } + return acc.concat(val) + }, []) +} + +// 将数组递归为一维数组 +export const flattenDeep = (array: any[]) => flattenDepth(array, array.length) + +// 减少一级数组的嵌套 +export const flatten = (array: any[]) => flattenDepth(array) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e531595..50743b9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,12 @@ importers: yorkie: 2.0.0 zlib: 1.0.5 + packages/array: + specifiers: + '@v-utils/type': workspace:^0.0.1 + dependencies: + '@v-utils/type': link:../type + packages: /@babel/code-frame/7.16.7: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 30af677..600b4bb 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,2 +1,2 @@ packages: - - 'packages:/**' + - 'packages/**'