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

Function useArrayReduce

packages/shared/useArrayReduce/index.ts:46–61  ·  view source on GitHub ↗
(
  list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,
  reducer: ((...p: any[]) => any),
  ...args: any[]
)

Source from the content-addressed store, hash-verified

44 * @returns the value that results from running the "reducer" callback function to completion over the entire array.
45 */
46export function useArrayReduce<T>(
47 list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,
48 reducer: ((...p: any[]) => any),
49 ...args: any[]
50): ComputedRef<T> {
51 const reduceCallback = (sum: any, value: any, index: number) => reducer(toValue(sum), toValue(value), index)
52
53 return computed(() => {
54 const resolved = toValue(list)
55 // Depending on the behavior of reduce, undefined is also a valid initialization value,
56 // and this code will distinguish the behavior between them.
57 return args.length
58 ? resolved.reduce(reduceCallback, typeof args[0] === 'function' ? toValue(args[0]()) : toValue(args[0]))
59 : resolved.reduce(reduceCallback)
60 })
61}

Callers 1

index.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected