UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

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