@revolugo/common 6.9.6-beta.1 → 6.9.6-beta.3
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/object-tools.ts +38 -0
package/package.json
CHANGED
|
@@ -80,3 +80,41 @@ export function keyBy<T>(
|
|
|
80
80
|
return acc
|
|
81
81
|
}, {})
|
|
82
82
|
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Creates an object composed of the own enumerable properties of object that predicate doesn't return truthy for.
|
|
86
|
+
* The predicate is invoked with two arguments: (value, key).
|
|
87
|
+
* If predicate is a string, it omits the property with that key name.
|
|
88
|
+
*
|
|
89
|
+
* @param object - The source object
|
|
90
|
+
* @param predicate - The function invoked per property or a string key to omit
|
|
91
|
+
* @returns Returns the new object
|
|
92
|
+
*/
|
|
93
|
+
export function omitBy<T extends object>(
|
|
94
|
+
object: T,
|
|
95
|
+
predicate: ((value: T[keyof T], key: keyof T) => boolean) | string,
|
|
96
|
+
): Partial<T> {
|
|
97
|
+
const result = {} as Partial<T>
|
|
98
|
+
|
|
99
|
+
for (const key in object) {
|
|
100
|
+
if (Object.hasOwn(object, key)) {
|
|
101
|
+
const value = object[key]
|
|
102
|
+
|
|
103
|
+
// If predicate is a string, check if key matches
|
|
104
|
+
if (typeof predicate === 'string') {
|
|
105
|
+
// eslint-disable-next-line max-depth
|
|
106
|
+
if (key !== predicate) {
|
|
107
|
+
result[key] = value
|
|
108
|
+
}
|
|
109
|
+
} else if (typeof predicate === 'function') {
|
|
110
|
+
// If predicate is a function, use it
|
|
111
|
+
// eslint-disable-next-line max-depth
|
|
112
|
+
if (!predicate(value, key)) {
|
|
113
|
+
result[key] = value
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return result
|
|
120
|
+
}
|