File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed
Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change 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
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 }
You can’t perform that action at this time.
0 commit comments