forked from sonofmagic/weapp-tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-obj.ts
35 lines (31 loc) · 1012 Bytes
/
filter-obj.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// @ts-nocheck
export function includeKeys(object, predicate) {
const result = {}
if (Array.isArray(predicate)) {
for (const key of predicate) {
const descriptor = Object.getOwnPropertyDescriptor(object, key)
if (descriptor?.enumerable) {
Object.defineProperty(result, key, descriptor)
}
}
} else {
// `Reflect.ownKeys()` is required to retrieve symbol properties
for (const key of Reflect.ownKeys(object)) {
const descriptor = Object.getOwnPropertyDescriptor(object, key)
if (descriptor.enumerable) {
const value = object[key]
if (predicate(key, value, object)) {
Object.defineProperty(result, key, descriptor)
}
}
}
}
return result
}
export function excludeKeys(object, predicate) {
if (Array.isArray(predicate)) {
const set = new Set(predicate)
return includeKeys(object, (key) => !set.has(key))
}
return includeKeys(object, (key, value, object) => !predicate(key, value, object))
}