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]); +``` 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]); + }); +}); 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[][]; +} 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'; -