UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

33 lines (31 loc) 954 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/array/findLastIndexOrThrow.ts /** * `findLastIndexOrThrow(target, predicate)` * * Returns the index of the last element in `target` that satisfies the provided `predicate` function. If no element satisfies the predicate, throws an error. * * ```ts * findLastIndexOrThrow([1, 3, 2, 4], (x) => x > 2); // 3 * findLastIndexOrThrow([1, 2, 3, 4], (x) => x > 5); // throws FnError * ``` * * ```ts * pipe( * [1, 3, 2, 4], * findLastIndexOrThrow((x) => x > 2), * ); // 3 * pipe( * [1, 2, 3, 4], * findLastIndexOrThrow((x) => x > 5), * ); // throws FnError * ``` */ const findLastIndexOrThrow = dfdlT((target, predicate) => { const idx = target.findLastIndex(predicate); if (idx < 0) throw new FnError("Array.findLastIndexOrThrow: No value found.", [target, predicate]); return idx; }, 2); //#endregion export { findLastIndexOrThrow };