-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
29 lines (26 loc) · 1.11 KB
/
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
import type { UnwrapRef } from 'vue-demi'
import { toRef, toRefs } from 'vue-demi'
import { reactiveComputed } from '../reactiveComputed'
import { resolveUnref } from '../resolveUnref'
export type ReactivePickPredicate<T> = (value: T[keyof T], key: keyof T) => boolean
export function reactivePick<T extends object, K extends keyof T>(
obj: T,
...keys: (K | K[])[]
): { [S in K]: UnwrapRef<T[S]> }
export function reactivePick<T extends object>(
obj: T,
predicate: ReactivePickPredicate<T>
): { [S in keyof T]?: UnwrapRef<T[S]> }
/**
* Reactively pick fields from a reactive object
*
* @see https://vueuse.org/reactivePick
*/
export function reactivePick<T extends object, K extends keyof T>(
obj: T,
...keys: (K | K[])[]
): { [S in K]: UnwrapRef<T[S]> } {
const flatKeys = keys.flat() as K[]
const predicate = flatKeys[0] as unknown as ReactivePickPredicate<T>
return reactiveComputed(() => typeof predicate === 'function' ? Object.fromEntries(Object.entries(toRefs(obj)).filter(([k, v]) => predicate(resolveUnref(v) as T[K], k as K))) : Object.fromEntries(flatKeys.map(k => [k, toRef(obj, k)]))) as any
}