-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
export const omitProperties = ( | ||
record: Record<string, unknown>, | ||
keyList: string[], | ||
): Record<string, unknown> => { | ||
if (!record || typeof record !== 'object' || Array.isArray(record)) | ||
return record; | ||
|
||
let reducedRecord = record; | ||
|
||
/* eslint-disable-next-line @typescript-eslint/prefer-for-of -- | ||
* For loop is faster than `for of` and performance is preferred over readability here | ||
**/ | ||
for (let keyIndex = 0; keyIndex < keyList.length; keyIndex++) { | ||
const key = keyList[keyIndex]; | ||
if (typeof key !== 'string') continue; | ||
input: unknown, | ||
properties: string[], | ||
): unknown => { | ||
if (!input || typeof input !== 'object' || Array.isArray(input)) { | ||
return input; | ||
} | ||
|
||
const { [key]: _, ...keepRecord } = reducedRecord; | ||
// We can get away with a shallow clone as we only touch top-level properties. | ||
const output: Record<PropertyKey, unknown> = { | ||
...input, | ||
}; | ||
|
||
reducedRecord = keepRecord; | ||
for (const property of properties) { | ||
// Remove the property from our shallow clone. | ||
delete output[property]; | ||
} | ||
|
||
return reducedRecord; | ||
return output; | ||
}; |