@monstermann/fn
Version:
A utility library for TypeScript.
30 lines (28 loc) • 775 B
JavaScript
import { FnError } from "../function/FnError.js";
import { dfdlT } from "@monstermann/dfdl";
//#region src/number/orThrow.ts
/**
* `orThrow(target)`
*
* Returns the numeric value of `target` if it's a finite number, otherwise throws an error.
*
* ```ts
* orThrow(42); // 42
* orThrow(NaN); // throws FnError
* orThrow(Infinity); // throws FnError
* orThrow("hello"); // throws FnError
* ```
*
* ```ts
* pipe(42, orThrow()); // 42
* pipe(NaN, orThrow()); // throws FnError
* pipe(Infinity, orThrow()); // throws FnError
* pipe("hello", orThrow()); // throws FnError
* ```
*/
const orThrow = dfdlT((target) => {
if (Number.isFinite(target)) return target;
throw new FnError("Number.orThrow: Value is not a finite number.", [target]);
}, 1);
//#endregion
export { orThrow };