diff --git a/src/object/compact.ts b/src/object/compact.ts index 70aa5cf..1abd51b 100644 --- a/src/object/compact.ts +++ b/src/object/compact.ts @@ -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 @@ -29,13 +31,18 @@ * ``` * * * * * @param object Object delete empty keys + * @param additionalValuesToRemove Other values to delete * @returns Compacted object */ -export function compact(object: T): Partial { +export function compact>( + object: T, + additionalValuesToRemove: any[] = [] +): Partial { 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]; } }