UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

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