UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

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