forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFilteredObject.ts
More file actions
22 lines (18 loc) · 890 Bytes
/
useFilteredObject.ts
File metadata and controls
22 lines (18 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Similar to "withQueryBuilderParams" except this function filters out any null,
* undefined, or empty string key values. This allows the parameters to be used for
* caching without having to account for all of the different data combinations.
*/
import { isEmptyObject, isObject } from '@/lib/objects';
// eslint-disable-next-line @typescript-eslint/ban-types
export default <T extends {}>(data: T): T => {
const empty = [undefined, null, ''] as unknown[];
const removeEmptyValues = (input: T): T =>
Object.entries(input)
.filter(([_, value]) => !empty.includes(value))
.reduce((obj, [k, v]) => {
const parsed = isObject(v) ? removeEmptyValues(v as any) : v;
return isObject(parsed) && isEmptyObject(parsed) ? obj : { ...obj, [k]: parsed };
}, {} as T);
return removeEmptyValues(data);
};