import { computed, toValue } from 'vue'; import type { ComputedRef, MaybeRefOrGetter } from 'vue'; /** * @name useArrayFind * @category Array * @description Reactive `Array.prototype.find`. * * @param {MaybeRefOrGetter[]>} list The source array (items can be reactive) * @param {(element: T, index: number, array: T[]) => boolean} fn Predicate * @returns {ComputedRef} The first matching element * * @example * const list = ref([1, 2, 3]); * const found = useArrayFind(list, n => n > 1); // 2 * * @since 0.0.15 */ export function useArrayFind( list: MaybeRefOrGetter>>, fn: (element: T, index: number, array: T[]) => boolean, ): ComputedRef { return computed(() => toValue(list).map(i => toValue(i)).find(fn)); }