From 29a988445e8cee36a96c09bb72dba1480422f980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B3=E5=BB=B7=E5=AE=89?= <73953029+nrps9909@users.noreply.github.com> Date: Thu, 27 Aug 2026 02:53:20 +0800 Subject: [PATCH] fix: preserve source when removing nested values --- src/utils/set.ts | 5 ++++- tests/utils.test.ts | 12 +++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/utils/set.ts b/src/utils/set.ts index 7d465a56..5d4cb3f5 100644 --- a/src/utils/set.ts +++ b/src/utils/set.ts @@ -25,7 +25,10 @@ function internalSet( // 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); } diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 842c172d..195e1c1f 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -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(