unset
객체의 특정 경로에 해당하는 속성을 삭제한 객체를 반환합니다.
get, set과 동일한 점 표기법(dot notation) 경로를 사용하며, 주어진 객체의 타입에 옵셔널 프로퍼티가 있는 경우 옵셔널(?) 경로로 접근해야 합니다.
경로 중간의 값이 없거나 객체가 아니라면 아무것도 삭제하지 않고 반환합니다.
기본적으로 원본 객체를 변경하지 않으며(immutable: true), immutable: false 옵션을 주면 원본 객체에서 직접 삭제하고 원본 객체를 그대로 반환합니다.
Code
Interface
typescript
function unset<T extends Record<string, unknown>, P extends PropertyPath<T>>(
obj: T,
path: P,
options?: { immutable?: boolean }
): T;
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
obj | T extends Record<string, unknown> | - | 속성을 삭제할 객체입니다. |
path | P extends PropertyPath<T> | - | 삭제할 속성의 경로입니다. |
options.immutable | boolean | true | false이면 복사본을 만들지 않고 원본 객체에서 직접 삭제합니다. |
Usage
기본 사용법
typescript
import { unset } from '@modern-kit/utils';
const obj = { a: { b: 1, c: 2 } };
unset(obj, 'a.b');
// { a: { c: 2 } }
// obj: { a: { b: 1, c: 2 } } (원본 유지)
옵셔널 경로
typescript
import { unset } from '@modern-kit/utils';
// 경로 중간의 값이 없으면 그대로 반환합니다.
const obj: { a?: { b?: number } } = {};
unset(obj, 'a?.b');
// {}
원본 객체 직접 변경
typescript
import { unset } from '@modern-kit/utils';
const obj = { a: { b: 1, c: 2 } };
unset(obj, 'a.b', { immutable: false });
// obj: { a: { c: 2 } }