Files
tools/vue/toolkit/src/composables/array/useArrayFindLast/index.ts
T
robonen ab6d8f6ce0
Publish to NPM / Check version changes and publish (push) Failing after 10m34s
build: bump new versions
2026-06-18 02:57:03 +07:00

49 lines
1.5 KiB
TypeScript

import { computed, toValue } from 'vue';
import type { ComputedRef, MaybeRefOrGetter } from 'vue';
export type UseArrayFindLastReturn<T = unknown>
= ComputedRef<T | undefined>;
/**
* `Array.prototype.findLast` polyfill for runtimes older than ES2023.
*/
function findLast<T>(
array: T[],
fn: (element: T, index: number, array: T[]) => unknown,
): T | undefined {
let index = array.length;
while (index-- > 0) {
const element = array[index]!;
if (fn(element, index, array))
return element;
}
return undefined;
}
const hasNativeFindLast = typeof Array.prototype.findLast === 'function';
/**
* @name useArrayFindLast
* @category Array
* @description Reactive `Array.prototype.findLast`.
*
* @param {MaybeRefOrGetter<MaybeRefOrGetter<T>[]>} list The source array (items can be reactive)
* @param {(element: T, index: number, array: T[]) => unknown} fn Predicate testing each element
* @returns {UseArrayFindLastReturn<T>} The last matching element, or `undefined` if none match
*
* @example
* const list = ref([1, 2, 3, 4]);
* const found = useArrayFindLast(list, n => n % 2 === 0); // 4
*
* @since 0.0.14
*/
export function useArrayFindLast<T>(
list: MaybeRefOrGetter<Array<MaybeRefOrGetter<T>>>,
fn: (element: T, index: number, array: T[]) => unknown,
): UseArrayFindLastReturn<T> {
return computed(() => {
const resolved = toValue(list).map(item => toValue(item));
return hasNativeFindLast ? resolved.findLast(fn) : findLast(resolved, fn);
});
}