Skip to content

Commit

Permalink
feat(Object): added additional values option to object.compact
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Jan 30, 2022
1 parent 96481de commit bf0e03b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/object/compact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* #### Removes `null` and `undefined` values and their keys from an object
*
* Additional values can be removed by passing to an array as the second argument
*
* * * * *
* Example usage:
* ```typescript
Expand Down Expand Up @@ -29,13 +31,18 @@
* ```
* * * *
* @param object Object delete empty keys
* @param additionalValuesToRemove Other values to delete
* @returns Compacted object
*/
export function compact<T extends Object>(object: T): Partial<T> {
export function compact<T extends Record<string|number|symbol, any>>(
object: T,
additionalValuesToRemove: any[] = []
): Partial<T> {
const newObject = {...object};
const valuesToRemove = [undefined, null, ...additionalValuesToRemove];

for (const [key, value] of Object.entries(newObject)) {
if (value === undefined || value === null) {
if (valuesToRemove.includes(value)) {
delete newObject[key];
}
}
Expand Down

0 comments on commit bf0e03b

Please sign in to comment.