UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

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