UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

27 lines (25 loc) 778 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/array/lastIndexOfOrThrow.ts /** * `lastIndexOfOrThrow(target, value)` * * Returns the index of the last occurrence of `value` in `target`. If `value` is not found, throws an error. * * ```ts * lastIndexOfOrThrow([1, 2, 3, 2], 2); // 3 * lastIndexOfOrThrow([1, 2, 3], 4); // throws FnError * ``` * * ```ts * pipe([1, 2, 3, 2], lastIndexOfOrThrow(2)); // 3 * pipe([1, 2, 3], lastIndexOfOrThrow(4)); // throws FnError * ``` */ const lastIndexOfOrThrow = dfdlT((target, value) => { const idx = target.lastIndexOf(value); if (idx < 0) throw new FnError("Array.lastIndexOf: No value found.", [target, value]); return idx; }, 2); //#endregion export { lastIndexOfOrThrow };