-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
31 lines (29 loc) · 835 Bytes
/
index.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
import { isObject } from '../../filters/isObject';
import { Entry } from '../../types/Entry';
export interface FilterObjectOptions {
/**
* Enable same filtering over sub-objects (if any).
*/
recursive?: boolean;
}
/**
* Filters object entries by predicate like standard array's filter.
*/
export const filterObject = (
source: object,
filter: (entry: Entry, index?: number, array?: Entry[]) => any,
{ recursive = false }: FilterObjectOptions = {}
): object => {
return recursive
? Object.fromEntries(
Object.entries(source)
.filter(filter)
.map(([key, value]) => [
key,
isObject(value)
? filterObject(value, filter, { recursive })
: value,
])
)
: Object.fromEntries(Object.entries(source).filter(filter));
};