import getLogger from "../logger";
const L = getLogger("env-to-number").debug;
export function envToInt(name: string, fallback: number) {
const value = process.env[name];
if (value == null) {
L(`envToInt: using fallback value ${fallback} for ${name}`);
return fallback;
}
const parsed = parseInt(value);
if (isNaN(parsed)) {
L(
`envToInt: could not parse ${name}=${value}, using fallback value ${fallback}`
);
return fallback;
}
return parsed;
}
export function envToFloat(name: string, fallback: number) {
const value = process.env[name];
if (value == null) {
L(`envToFloat: using fallback value ${fallback} for ${name}`);
return fallback;
}
const parsed = parseFloat(value);
if (isNaN(parsed)) {
L(
`envToFloat: could not parse ${name}=${value}, using fallback value ${fallback}`
);
return fallback;
}
return parsed;
}