From ee8ff9c14ae24c3b5446e824a7fb4fc9e3a1ae09 Mon Sep 17 00:00:00 2001 From: ubinquitous Date: Mon, 5 Aug 2024 17:13:55 +0900 Subject: [PATCH 1/4] chore: set export index --- packages/common/utils/src/array/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/utils/src/array/index.ts b/packages/common/utils/src/array/index.ts index c4553d557..3235c8b17 100644 --- a/packages/common/utils/src/array/index.ts +++ b/packages/common/utils/src/array/index.ts @@ -1,8 +1,8 @@ /** @tossdocs-ignore */ export * from './arrayIncludes'; +export * from './flattenArray'; export * from './isDifferentArray'; export * from './isNonEmptyArray'; export * from './last'; export * from './NonEmptyArray'; export * from './sample'; - From d7bee8c42621d8b6a3b76a6a9a42b6f058edc190 Mon Sep 17 00:00:00 2001 From: ubinquitous Date: Mon, 5 Aug 2024 17:14:26 +0900 Subject: [PATCH 2/4] feat: define flattenArray --- packages/common/utils/src/array/flattenArray.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 packages/common/utils/src/array/flattenArray.ts diff --git a/packages/common/utils/src/array/flattenArray.ts b/packages/common/utils/src/array/flattenArray.ts new file mode 100644 index 000000000..efa2fd049 --- /dev/null +++ b/packages/common/utils/src/array/flattenArray.ts @@ -0,0 +1,13 @@ +/** @tossdocs-ignore */ +export function flattenArray(array: Type[]): Type[]; +export function flattenArray(array: Type[][]): Type[] { + if (array.length === 0) { + return array as Type[] & Type[][]; + } + + if (Array.isArray(array[0])) { + return (array as Type[][]).reduce((acc: Type[], curr: Type[]) => acc.concat(curr), []); + } + + return array as Type[] & Type[][]; +} From 5cb5dc68e953fe04f541a487e98495adc0a93ec6 Mon Sep 17 00:00:00 2001 From: ubinquitous Date: Mon, 5 Aug 2024 17:14:35 +0900 Subject: [PATCH 3/4] test: flattenArray test code --- .../utils/src/array/flattenArray.spec.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 packages/common/utils/src/array/flattenArray.spec.ts diff --git a/packages/common/utils/src/array/flattenArray.spec.ts b/packages/common/utils/src/array/flattenArray.spec.ts new file mode 100644 index 000000000..dd98d5089 --- /dev/null +++ b/packages/common/utils/src/array/flattenArray.spec.ts @@ -0,0 +1,28 @@ +import { flattenArray } from '.'; + +describe('flattenArray', () => { + it('should work such that if a two-dimensional array is input, it returns a one-dimensional array', () => { + const twoDimensionsArray1 = [ + [1, 2], + [3, 4], + ]; + expect(flattenArray(twoDimensionsArray1)).toEqual([1, 2, 3, 4]); + + const twoDimensionsArray2 = [ + [{ a: 1 }, { b: 2 }], + [{ c: 3 }, { d: 4 }], + ]; + expect(flattenArray(twoDimensionsArray2)).toEqual([{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }]); + + const twoDimensionsArray3 = [ + ['', false, 0, undefined, null], + [null, undefined, 0, false, ''], + ]; + expect(flattenArray(twoDimensionsArray3)).toEqual(['', false, 0, undefined, null, null, undefined, 0, false, '']); + }); + + it('should work by returning the array as is if a one-dimensional array is input', () => { + const oneDimensionsArray = [1, 2, 3, 4]; + expect(flattenArray(oneDimensionsArray)).toEqual([1, 2, 3, 4]); + }); +}); From a98ea3c0ee82cac743782bb0bde9948bced55e3a Mon Sep 17 00:00:00 2001 From: ubinquitous Date: Mon, 5 Aug 2024 17:14:52 +0900 Subject: [PATCH 4/4] docs: flattenArray docs publish --- .../common/utils/src/array/flattenArray.en.md | 23 +++++++++++++++++++ .../common/utils/src/array/flattenArray.ko.md | 23 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 packages/common/utils/src/array/flattenArray.en.md create mode 100644 packages/common/utils/src/array/flattenArray.ko.md diff --git a/packages/common/utils/src/array/flattenArray.en.md b/packages/common/utils/src/array/flattenArray.en.md new file mode 100644 index 000000000..7d2211898 --- /dev/null +++ b/packages/common/utils/src/array/flattenArray.en.md @@ -0,0 +1,23 @@ +# flattenArray + +이차원 배열을 일차원 배열로 반환합니다. + +## Example + +```typescript +// [1, 2, 3, 4] +flattenArray([ + [1, 2], + [3, 4], +]); + +// [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }] +flattenArray([ + [{ a: 1 }, { b: 2 }], + [{ c: 3 }, { d: 4 }], +]); + +// 일차원 배열을 입력했을 경우 입력받은 배열을 다시 그대로 반환합니다. +// 1, 2, 3, 4 +flattenArray([1, 2, 3, 4]); +``` diff --git a/packages/common/utils/src/array/flattenArray.ko.md b/packages/common/utils/src/array/flattenArray.ko.md new file mode 100644 index 000000000..7d2211898 --- /dev/null +++ b/packages/common/utils/src/array/flattenArray.ko.md @@ -0,0 +1,23 @@ +# flattenArray + +이차원 배열을 일차원 배열로 반환합니다. + +## Example + +```typescript +// [1, 2, 3, 4] +flattenArray([ + [1, 2], + [3, 4], +]); + +// [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }] +flattenArray([ + [{ a: 1 }, { b: 2 }], + [{ c: 3 }, { d: 4 }], +]); + +// 일차원 배열을 입력했을 경우 입력받은 배열을 다시 그대로 반환합니다. +// 1, 2, 3, 4 +flattenArray([1, 2, 3, 4]); +```