From f674f706f6913e99688c549fe1e6c925cc68e5aa Mon Sep 17 00:00:00 2001 From: Ryan-Zayne Date: Sun, 1 Dec 2024 19:28:25 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(core):=20add=20omitKeysWithDel?= =?UTF-8?q?ete=20utility=20for=20extra=20choice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/toolkit/src/core/omitKeys.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/toolkit/src/core/omitKeys.ts b/packages/toolkit/src/core/omitKeys.ts index 60d2b49..e031ed0 100644 --- a/packages/toolkit/src/core/omitKeys.ts +++ b/packages/toolkit/src/core/omitKeys.ts @@ -56,3 +56,23 @@ export const omitKeysWithFilter = < return updatedObject as OmitKeys; }; + +export const omitKeysWithDelete = < + TObject extends AnyObject, + const TOmitArray extends Array, +>( + initialObject: TObject, + keysToOmit: TOmitArray +) => { + const keysToOmitSet = new Set(keysToOmit); + + const updatedObject = { ...initialObject }; + + for (const key of Object.keys(updatedObject)) { + if (keysToOmitSet.has(key)) { + Reflect.deleteProperty(updatedObject, key); + } + } + + return updatedObject as OmitKeys; +};