From a2c5ee82cb2ba3507e70788a7d9553ab3a923e69 Mon Sep 17 00:00:00 2001 From: ssi02014 Date: Sun, 12 May 2024 15:49:31 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix(utils):=20deleteFalsyProperties=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0=20=EB=B0=8F=20isArray=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/sour-hats-smash.md | 5 ++++ .../utils/object/deleteEmptyProperties.md | 6 +++- .../deleteFalsyProperties.spec.ts | 28 +++++++++++++++++++ .../src/object/deleteFalsyProperties/index.ts | 22 +++++++++------ packages/utils/src/validator/index.ts | 1 + packages/utils/src/validator/isArray/index.ts | 3 ++ .../src/validator/isArray/isArray.spec.ts | 20 +++++++++++++ 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 .changeset/sour-hats-smash.md create mode 100644 packages/utils/src/validator/isArray/index.ts create mode 100644 packages/utils/src/validator/isArray/isArray.spec.ts diff --git a/.changeset/sour-hats-smash.md b/.changeset/sour-hats-smash.md new file mode 100644 index 000000000..f652423b4 --- /dev/null +++ b/.changeset/sour-hats-smash.md @@ -0,0 +1,5 @@ +--- +'@modern-kit/utils': patch +--- + +fix: deleteFalsyProperties 개선 diff --git a/docs/docs/utils/object/deleteEmptyProperties.md b/docs/docs/utils/object/deleteEmptyProperties.md index fa1f6a7cc..235046cdd 100644 --- a/docs/docs/utils/object/deleteEmptyProperties.md +++ b/docs/docs/utils/object/deleteEmptyProperties.md @@ -11,7 +11,11 @@ ## Interface ```ts title="typescript" -const deleteEmptyProperties: >(obj: T) => T +const deleteFalsyProperties: < + T extends Record = Record +>( + source: Record +) => T; ``` ## Usage diff --git a/packages/utils/src/object/deleteFalsyProperties/deleteFalsyProperties.spec.ts b/packages/utils/src/object/deleteFalsyProperties/deleteFalsyProperties.spec.ts index b580274af..dd2269b69 100644 --- a/packages/utils/src/object/deleteFalsyProperties/deleteFalsyProperties.spec.ts +++ b/packages/utils/src/object/deleteFalsyProperties/deleteFalsyProperties.spec.ts @@ -53,6 +53,7 @@ describe('deleteFalsyProperties', () => { ], }, }, + prop3: [1, 2, null], }; const expectedObj = { prop1: [ @@ -73,6 +74,7 @@ describe('deleteFalsyProperties', () => { prop2_1_6: [{ prop2_1_6_1: 1 }], }, }, + prop3: [1, 2], }; expect(deleteFalsyProperties(originObj)).toEqual(expectedObj); @@ -118,4 +120,30 @@ describe('deleteFalsyProperties', () => { prop10: {}, }); }); + + it('should allow specifying desired types through generics', () => { + const originObj = { + prop1: 1, + prop2: 0, + prop3: '', + prop4: '1', + prop5: true, + prop6: false, + prop7: null, + prop8: undefined, + prop9: [], + prop10: {}, + }; + const expectedObj = { + prop1: 1, + prop2: 0, + prop4: '1', + prop5: true, + prop6: false, + }; + + const result = deleteFalsyProperties(originObj); + + expectTypeOf(result).toEqualTypeOf(); + }); }); diff --git a/packages/utils/src/object/deleteFalsyProperties/index.ts b/packages/utils/src/object/deleteFalsyProperties/index.ts index 2a2e11a8c..f8c302d59 100644 --- a/packages/utils/src/object/deleteFalsyProperties/index.ts +++ b/packages/utils/src/object/deleteFalsyProperties/index.ts @@ -1,7 +1,9 @@ -import { hasProperty } from '../../validator'; +import { hasProperty, isArray } from '../../validator'; -export const deleteFalsyProperties = >( - source: T +export const deleteFalsyProperties = < + T extends Record = Record +>( + source: Record ): T => { const copiedObj: Record = {}; @@ -9,16 +11,18 @@ export const deleteFalsyProperties = >( if (hasProperty(source, key)) { const value = source[key]; - if (value !== null && typeof value === 'object') { + if (value != null && typeof value === 'object') { // object - if (!Array.isArray(value)) { + if (!isArray(value)) { const newObj = deleteFalsyProperties(value); + const isNonEmptyObj = !!Object.keys(newObj).length; - if (Object.keys(newObj).length) { + if (isNonEmptyObj) { copiedObj[key] = newObj; } continue; } + // array const newArray = value.reduce((acc: any[], cur: any) => { if (typeof cur !== 'object') { @@ -36,14 +40,14 @@ export const deleteFalsyProperties = >( copiedObj[key] = newArray; } } else if ( + value || typeof value === 'number' || - typeof value === 'boolean' || - value + typeof value === 'boolean' ) { copiedObj[key] = value; } } } - return copiedObj; + return copiedObj as T; }; diff --git a/packages/utils/src/validator/index.ts b/packages/utils/src/validator/index.ts index 142b51590..cbc596091 100644 --- a/packages/utils/src/validator/index.ts +++ b/packages/utils/src/validator/index.ts @@ -1,4 +1,5 @@ export * from './hasProperty'; +export * from './isArray'; export * from './isFunction'; export * from './isNotNullish'; export * from './isNullish'; diff --git a/packages/utils/src/validator/isArray/index.ts b/packages/utils/src/validator/isArray/index.ts new file mode 100644 index 000000000..7992c341e --- /dev/null +++ b/packages/utils/src/validator/isArray/index.ts @@ -0,0 +1,3 @@ +export const isArray = (value: unknown): value is Array => { + return Array.isArray(value); +}; diff --git a/packages/utils/src/validator/isArray/isArray.spec.ts b/packages/utils/src/validator/isArray/isArray.spec.ts new file mode 100644 index 000000000..e3e60871a --- /dev/null +++ b/packages/utils/src/validator/isArray/isArray.spec.ts @@ -0,0 +1,20 @@ +import { isArray } from '.'; + +describe('isArray', () => { + it('should return true if the argument is an array, and false if it is not', () => { + expect(isArray([])).toBeTruthy(); + expect(isArray(1)).toBeFalsy(); + expect(isArray('')).toBeFalsy(); + expect(isArray({})).toBeFalsy(); + }); + + it('should narrow the type through if statements', () => { + const testValue = ['foo'] as string | string[]; + + if (isArray(testValue)) { + expectTypeOf(testValue).toEqualTypeOf(); + } else { + expectTypeOf(testValue).toEqualTypeOf(); + } + }); +}); From e6bcbf097cc4240b35ecf1a05325e4c617e8462d Mon Sep 17 00:00:00 2001 From: ssi02014 Date: Sun, 12 May 2024 15:52:00 +0900 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20isArray=20=EB=AC=B8=EC=84=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/utils/validator/isArray.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 docs/docs/utils/validator/isArray.md diff --git a/docs/docs/utils/validator/isArray.md b/docs/docs/utils/validator/isArray.md new file mode 100644 index 000000000..74eb2d435 --- /dev/null +++ b/docs/docs/utils/validator/isArray.md @@ -0,0 +1,25 @@ +# isArray + +주어진 인자가 `배열`인지 검사하고, 맞다면 인자의 타입을 `Array`로 좁혀주는 함수입니다. + +
+ +## Code +[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/utils/src/validator/isArray/index.ts) + +## Interface +```ts title="typescript" +const isArray: (value: unknown) => value is T[] +``` + +## Usage +```ts title="typescript" +import { isArray } from '@modern-kit/utils'; + +isArray([]); // true + +isArray(() => {}); // false +isArray('123'); // false +isArray(123); // false +isArray({}); // false +```