UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

27 lines (25 loc) 704 B
import { FnError } from "../function/FnError.js"; import { dfdlT } from "@monstermann/dfdl"; //#region src/string/parseIntOrThrow.ts /** * `parseIntOrThrow(target)` * * Parses `target` string and returns an integer, or throws an error if parsing fails. * * ```ts * parseIntOrThrow("42"); // 42 * parseIntOrThrow("abc"); // throws FnError * ``` * * ```ts * pipe("42", parseIntOrThrow()); // 42 * pipe("abc", parseIntOrThrow()); // throws FnError * ``` */ const parseIntOrThrow = dfdlT((target) => { const value = Number.parseInt(target); if (Number.isFinite(value)) return value; throw new FnError("String.parseIntOrThrow: Failed to parse.", [target]); }, 1); //#endregion export { parseIntOrThrow };