MCPcopy Create free account
hub / github.com/cn-docs/vueuse / useArrayIncludes

Function useArrayIncludes

packages/shared/useArrayIncludes/index.ts:38–68  ·  view source on GitHub ↗
(
  ...args: any[]
)

Source from the content-addressed store, hash-verified

36 options?: UseArrayIncludesOptions<T, V>,
37): ComputedRef<boolean>
38export function useArrayIncludes<T, V = any>(
39 ...args: any[]
40): ComputedRef<boolean> {
41 const list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]> = args[0]
42 const value: MaybeRefOrGetter<V> = args[1]
43
44 let comparator: UseArrayIncludesComparatorFn<T, V> = args[2]
45 let formIndex = 0
46
47 if (isArrayIncludesOptions(comparator)) {
48 formIndex = comparator.fromIndex ?? 0
49 comparator = comparator.comparator!
50 }
51
52 if (typeof comparator === 'string') {
53 const key = comparator as keyof T
54 comparator = (element: T, value: V) => element[key] === toValue(value)
55 }
56
57 comparator = comparator ?? ((element: T, value: T) => element === toValue(value))
58
59 return computed(() =>
60 toValue(list)
61 .slice(formIndex)
62 .some((element, index, array) => comparator(
63 toValue(element),
64 toValue(value),
65 index,
66 toValue(array),
67 )))
68}

Callers 1

index.test.tsFile · 0.90

Calls 1

isArrayIncludesOptionsFunction · 0.85

Tested by

no test coverage detected