@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revolugo/common",
3
- "version": "6.9.3-rc.0",
3
+ "version": "6.9.4-rc.0",
4
4
  "private": false,
5
5
  "description": "Revolugo common",
6
6
  "author": "Revolugo",
@@ -27,3 +27,4 @@ export * from './validators.ts'
27
27
  export * from './value-tools.ts'
28
28
  export * from './create-composite-key.ts'
29
29
  export * from './children-tools.ts'
30
+ export * from './shake.ts'
@@ -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
+ }