jtc-utils
Version:
Utilities for Japanese Traditional Companies
19 lines (18 loc) • 659 B
JavaScript
import { enUS, ja } from "./locale/index.mjs";
import { toHalfwidthAscii } from "./toHalfwidthAscii.mjs";
import { NumberFormat } from "./util/NumberFormat.mjs";
import { getLocale } from "./util/getLocale.mjs";
export function parseNumber(str, format, options) {
if (str == null) {
return undefined;
}
let num;
if (format) {
const locale = options?.locale ?? (/^ja(-|$)/i.test(getLocale()) ? ja : enUS);
num = NumberFormat.get(format, locale.code).parse(str);
}
else {
num = Number.parseFloat(toHalfwidthAscii(str.replace(/[^0-9.-]+/g, "")));
}
return Number.isFinite(num) ? num : undefined;
}