diff --git a/README.md b/README.md index a8c06f6..be28112 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ const unescaped = stringUtil.unescapeHtml("<div>Hello</div>"); // Object utilities const cleaned = objectUtil.clearNullProperties({ a: 1, b: null, c: 3 }); const frozen = objectUtil.deepFreeze({ a: { b: 1 } }); +const withoutKey = objectUtil.removeKey("b", { a: 1, b: 2, c: 3 }); // { a: 1, c: 3 } // Number utilities const total = numberUtil.sum(1, 2, 3, 4, 5); // 15 @@ -81,6 +82,7 @@ const theme = cookieUtil.getCookie("theme"); - `clearNullProperties(obj: object): object` - Removes null/undefined properties - `deepFreeze(obj: object): object` - Deep freezes an object recursively +- `removeKey(key: string, obj: Record): object` - Returns a new object with the specified key removed ### NumberUtil diff --git a/package/objectUtil/index.ts b/package/objectUtil/index.ts index 0f3fe58..36b3c9e 100644 --- a/package/objectUtil/index.ts +++ b/package/objectUtil/index.ts @@ -1,2 +1,3 @@ -export { default as clearNullProperties } from './clearNullProperties'; -export { default as deepFreeze } from './deepFreeze'; \ No newline at end of file +export { default as clearNullProperties } from "./clearNullProperties"; +export { default as deepFreeze } from "./deepFreeze"; +export { default as removeKey } from "./removeKey"; diff --git a/package/objectUtil/removeKey/index.test.ts b/package/objectUtil/removeKey/index.test.ts new file mode 100644 index 0000000..dbdbca4 --- /dev/null +++ b/package/objectUtil/removeKey/index.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, test } from "vitest"; +import removeKey from "."; + +describe("removeKey 유틸 함수 테스트", () => { + test("객체와 키를 입력했을 때, 정상적으로 객체에서 속성이 제거되어야한다.", () => { + expect(removeKey("b", { a: 1, b: 2, c: 3 })).toEqual({ a: 1, c: 3 }); + }); + test("객체와 키를 입력했을 때, 제거할 키가 객체에 없으면 원본 객체가 반환되어야한다.", () => { + expect(removeKey("d", { a: 1, b: 2, c: 3 })).toEqual({ a: 1, b: 2, c: 3 }); + }); + test("빈 객체와 키를 입력했을 때, 빈 객체가 반환되어야한다.", () => { + expect(removeKey("d", {})).toEqual({}); + }); +}); diff --git a/package/objectUtil/removeKey/index.ts b/package/objectUtil/removeKey/index.ts new file mode 100644 index 0000000..033cd71 --- /dev/null +++ b/package/objectUtil/removeKey/index.ts @@ -0,0 +1,4 @@ +export default function removeKey(key: string, obj: Record) { + const { [key]: _, ...rest } = obj; + return rest; +}