@revolugo/common 7.13.1-alpha.2 → 7.13.1-alpha.4

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": "7.13.1-alpha.2",
3
+ "version": "7.13.1-alpha.4",
4
4
  "private": false,
5
5
  "description": "Revolugo common",
6
6
  "author": "Revolugo",
@@ -43,6 +43,7 @@ export * from './merge.ts'
43
43
  export * from './omit-by.ts'
44
44
  export * from './omit.ts'
45
45
  export * from './parse-children.ts'
46
+ export * from './pick-by.ts'
46
47
  export * from './pick.ts'
47
48
  export * from './poller.ts'
48
49
  export * from './prepare-ts-query.ts'
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Creates an object composed of the own enumerable properties of object that predicate returns truthy for.
3
+ * The predicate is invoked with two arguments: (value, key).
4
+ * If predicate is a string, it picks the property with that key name.
5
+ *
6
+ * @param object - The source object
7
+ * @param predicate - The function invoked per property or a string key to pick
8
+ * @returns Returns the new object
9
+ */
10
+ export function pickBy<T extends object>(
11
+ object: T,
12
+ predicate: string | ((value: T[keyof T], key: keyof T) => boolean),
13
+ ): Partial<T> {
14
+ const result = {} as Partial<T>
15
+
16
+ for (const key in object) {
17
+ if (Object.hasOwn(object, key)) {
18
+ const value = object[key]
19
+
20
+ // If predicate is a string, check if key matches
21
+ if (typeof predicate === 'string') {
22
+ // eslint-disable-next-line max-depth
23
+ if (key === predicate) {
24
+ result[key] = value
25
+ }
26
+ } else if (typeof predicate === 'function') {
27
+ // If predicate is a function, use it
28
+ // eslint-disable-next-line max-depth
29
+ if (predicate(value, key)) {
30
+ result[key] = value
31
+ }
32
+ }
33
+ }
34
+ }
35
+
36
+ return result
37
+ }