UNPKG

diginext-utils

Version:
26 lines 578 B
import { isNull } from "./isNull.js"; /** * Converts a value to an integer. * Returns 0 if the value cannot be converted. * * @param value - The value to convert * @returns The integer representation * * @example * ```ts * toInt('42'); // 42 * toInt('3.14'); // 3 * toInt(5.7); // 5 * toInt('hello'); // 0 * toInt(null); // 0 * toInt(''); // 0 * ``` */ export function toInt(value) { if (isNull(value)) { return 0; } const result = parseInt(String(value), 10); return Number.isNaN(result) ? 0 : result; } //# sourceMappingURL=toInt.js.map