Skip to content

Commit bf0e03b

Browse files
feat(Object): added additional values option to object.compact
1 parent 96481de commit bf0e03b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/object/compact.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* #### Removes `null` and `undefined` values and their keys from an object
33
*
4+
* Additional values can be removed by passing to an array as the second argument
5+
*
46
* * * * *
57
* Example usage:
68
* ```typescript
@@ -29,13 +31,18 @@
2931
* ```
3032
* * * *
3133
* @param object Object delete empty keys
34+
* @param additionalValuesToRemove Other values to delete
3235
* @returns Compacted object
3336
*/
34-
export function compact<T extends Object>(object: T): Partial<T> {
37+
export function compact<T extends Record<string|number|symbol, any>>(
38+
object: T,
39+
additionalValuesToRemove: any[] = []
40+
): Partial<T> {
3541
const newObject = {...object};
42+
const valuesToRemove = [undefined, null, ...additionalValuesToRemove];
3643

3744
for (const [key, value] of Object.entries(newObject)) {
38-
if (value === undefined || value === null) {
45+
if (valuesToRemove.includes(value)) {
3946
delete newObject[key];
4047
}
4148
}

0 commit comments

Comments
 (0)