UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

26 lines (24 loc) 650 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/array/maxOrThrow.ts /** * `maxOrThrow(array)` * * Returns the maximum value from `array`, or throws an error if the array is empty. * * ```ts * maxOrThrow([1, 5, 3]); // 5 * maxOrThrow([]); // throws FnError * ``` * * ```ts * pipe([1, 5, 3], maxOrThrow()); // 5 * pipe([], maxOrThrow()); // throws FnError * ``` */ const maxOrThrow = dfdlT((target) => { if (target.length === 0) throw new FnError("Array.maxOrThrow: Target is empty.", [target]); return target.reduce((a, b) => Math.max(a, b), 0); }, 1); //#endregion export { maxOrThrow };