forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
20 lines (19 loc) · 788 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'
import type { MaybeComputedRef } from '../utils'
import { resolveUnref } from '../resolveUnref'
/**
* Reactive `Array.map`
*
* @see https://vueuse.org/useArrayMap
* @param {Array} list - the array was called upon.
* @param fn - a function that is called for every element of the given `list`. Each time `fn` executes, the returned value is added to the new array.
*
* @returns {Array} a new array with each element being the result of the callback function.
*/
export function useArrayMap<T, U>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
fn: (element: T, index: number, array: T[]) => U,
): ComputedRef<U[]> {
return computed(() => resolveUnref(list).map(i => resolveUnref(i)).map(fn))
}