UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

25 lines (23 loc) 570 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/array/firstOrThrow.ts /** * `firstOrThrow(array)` * * Returns the first element of `array`, or throws an error if the array is empty. * * ```ts * firstOrThrow([1, 2, 3, 4]); // 1 * ``` * * ```ts * pipe([1, 2, 3, 4], firstOrThrow()); // 1 * ``` */ const firstOrThrow = dfdlT((target) => { const value = target[0]; if (value != null) return value; throw new FnError("Array.firstOrThrow: No value found.", [target]); }, 1); //#endregion export { firstOrThrow };