UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

28 lines (26 loc) 734 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/array/findLastOrThrow.ts /** * `findLastOrThrow(array, predicate)` * * Returns the last element in `array` that satisfies the provided `predicate` function, or throws an error if no element is found. * * ```ts * findLastOrThrow([1, 2, 3, 4], (x) => x > 2); // 4 * ``` * * ```ts * pipe( * [1, 2, 3, 4], * findLastOrThrow((x) => x > 2), * ); // 4 * ``` */ const findLastOrThrow = dfdlT((target, predicate) => { const value = target.findLast(predicate); if (value != null) return value; throw new FnError("Array.findLastOrThrow: Value not found.", [target, predicate]); }, 2); //#endregion export { findLastOrThrow };