-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpickBy.ts
50 lines (44 loc) · 1.6 KB
/
pickBy.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import flatten from './flatten'
import {baseIteratee} from './internal/baseIteratee'
import keys from './keys'
import type {ValueKeyIteratee, ValueKeyIterateeTypeGuard} from './internal/baseIteratee.type'
import type {Dictionary, NumericDictionary} from './internal/types'
export function pickBy<T, S extends T>(
object: Dictionary<T> | null | undefined,
predicate: ValueKeyIterateeTypeGuard<T, S>,
): Dictionary<S>
export function pickBy<T, S extends T>(
object: NumericDictionary<T> | null | undefined,
predicate: ValueKeyIterateeTypeGuard<T, S>,
): NumericDictionary<S>
export function pickBy<T>(object: Dictionary<T> | null | undefined, predicate?: ValueKeyIteratee<T>): Dictionary<T>
export function pickBy<T>(
object: NumericDictionary<T> | null | undefined,
predicate?: ValueKeyIteratee<T>,
): NumericDictionary<T>
export function pickBy<T extends object>(
object: T | null | undefined,
predicate?: ValueKeyIteratee<T[keyof T]>,
): Partial<T>
export function pickBy<T, S extends T = T>(
object: T | null | undefined,
predicate: ValueKeyIteratee<T> | ValueKeyIterateeTypeGuard<T, S> = (value) => value,
) {
if (object == null) {
return {} as Partial<T>
}
const props = flatten(keys(object))
const iteratee = baseIteratee(predicate)
var index = -1
var length = props.length
var result = {} as Partial<T>
while (++index < length) {
var key = props[index]
var value = object[key as keyof T]
if (iteratee(value, key, object)) {
result[key as keyof T] = value
}
}
return result
}
export default pickBy