generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): add omitKeysFromObject utility function (#700)
* feat(utilities): add omitKeysFromObject utility function --------- Co-authored-by: Aura <[email protected]>
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { deepClone } from './deepClone'; | ||
|
||
/** | ||
* Clones the source object using {@link deepClone} then deletes the specified keys with {@link Reflect.deleteProperty} | ||
* | ||
* @template TO - The object type. | ||
* @template KTO - The keys of the object type. | ||
* | ||
* @param source - The input object. | ||
* @param keys - The keys to omit from the object. | ||
* @returns A new object without the specified keys. | ||
*/ | ||
export function omitKeysFromObject<Object extends object, ObjectKeys extends keyof Object>( | ||
source: Object, | ||
...keys: readonly ObjectKeys[] | ||
): Omit<Object, ObjectKeys> { | ||
const clone = deepClone(source); | ||
|
||
for (const key of keys) { | ||
Reflect.deleteProperty(clone, key); | ||
} | ||
|
||
return clone; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { omitKeysFromObject } from '../src/lib/omitKeysFromObject'; | ||
|
||
describe('omitKeysFromObject', () => { | ||
test('GIVEN object and keys THEN returns object without specified keys', () => { | ||
const source = { name: 'John', age: 30, city: 'New York' }; | ||
const result = omitKeysFromObject(source, 'age', 'city'); | ||
expect(result).toEqual({ name: 'John' }); | ||
}); | ||
|
||
test('GIVEN object and no keys THEN returns the same object', () => { | ||
const source = { name: 'John', age: 30, city: 'New York' }; | ||
const result = omitKeysFromObject(source); | ||
expect(result).toEqual(source); | ||
}); | ||
|
||
test('GIVEN empty object and keys THEN returns empty object', () => { | ||
const source = {}; | ||
// @ts-expect-error Testing invalid input | ||
const result = omitKeysFromObject(source, 'age', 'city'); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
test('GIVEN an object with ES objects THEN returns object without specified keys', () => { | ||
const source = { | ||
one: new Map(), | ||
two: new Set(), | ||
three: new Date(), | ||
four: new Uint8Array(), | ||
five: new Error(), | ||
six: new RegExp(''), | ||
seven: [] | ||
}; | ||
const result = omitKeysFromObject(source, 'one', 'two', 'three', 'four', 'five', 'six', 'seven'); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
test('GIVEN keys as array THEN removes keys', () => { | ||
const source = { name: 'John', age: 30, city: 'New York' }; | ||
// @ts-expect-error spreading Arrays gives "string" instead of "keyof T" | ||
const result = omitKeysFromObject(source, ...['age', 'city']); | ||
expect(result).toEqual({ name: 'John' }); | ||
}); | ||
|
||
test("GIVEN any source object THEN validates that the result isn't identical to the source", () => { | ||
const source = { name: 'John', age: 30, city: 'New York' }; | ||
const result = omitKeysFromObject(source); | ||
expect(result).not.toBe(source); | ||
}); | ||
|
||
test("GIVEN any source object THEN validates that the source isn't modified", () => { | ||
const source = { name: 'John', age: 30, city: 'New York' }; | ||
omitKeysFromObject(source); | ||
expect(source).toEqual({ name: 'John', age: 30, city: 'New York' }); | ||
}); | ||
}); |