@revolugo/common 6.9.3-rc.0 → 6.9.4-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/index.ts +1 -0
- package/src/utils/shake.ts +22 -0
package/package.json
CHANGED
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes (shakes out) undefined entries from an
|
|
3
|
+
* object. Optional second argument shakes out values
|
|
4
|
+
* by custom evaluation.
|
|
5
|
+
*/
|
|
6
|
+
export function shake<RemovedKeys extends string, T>(
|
|
7
|
+
obj: T,
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
filter: (value: any) => boolean = x => x === undefined,
|
|
10
|
+
): Omit<T, RemovedKeys> {
|
|
11
|
+
if (!obj) {
|
|
12
|
+
return {} as T
|
|
13
|
+
}
|
|
14
|
+
const keys = Object.keys(obj) as (keyof T)[]
|
|
15
|
+
return keys.reduce((acc, key) => {
|
|
16
|
+
if (filter(obj[key])) {
|
|
17
|
+
return acc
|
|
18
|
+
}
|
|
19
|
+
acc[key] = obj[key]
|
|
20
|
+
return acc
|
|
21
|
+
}, {} as T)
|
|
22
|
+
}
|