Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/utils/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ function internalSet<Entity = any, Output = Entity, Value = any>(

// Delete prop if `removeIfUndefined` and value is undefined
if (removeIfUndefined && value === undefined && restPath.length === 1) {
delete clone[path][restPath[0]];
const origin = clone[path];
const child = Array.isArray(origin) ? [...origin] : { ...origin };
delete child[restPath[0]];
clone[path] = child;
} else {
clone[path] = internalSet(clone[path], restPath, value, removeIfUndefined);
}
Expand Down
12 changes: 5 additions & 7 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ describe('utils', () => {
expect(set({}, ['notExist'], undefined, true)).toEqual({});

// Delete value
const target = set(
{ keep: { light: 2333 } },
['keep', 'light'],
undefined,
true,
);
expect(target).toEqual({ keep: {} });
const source = { keep: { light: 2333, bamboo: 1 } };
const target = set(source, ['keep', 'light'], undefined, true);
expect(target).toEqual({ keep: { bamboo: 1 } });
expect('light' in target.keep).toBeFalsy();
expect(source).toEqual({ keep: { light: 2333, bamboo: 1 } });
expect(target.keep).not.toBe(source.keep);

// Mid path not exist
const midTgt = set(
Expand Down