UNPKG

jtc-utils

Version:
40 lines (35 loc) 1 kB
import { type Locale, enUS, ja } from "./locale/index.cjs"; import { toHalfwidthAscii } from "./toHalfwidthAscii.cjs"; import { NumberFormat } from "./util/NumberFormat.cjs"; import { getLocale } from "./util/getLocale.cjs"; declare type ParseNumberOptions = { locale?: Locale; }; export function parseNumber( str: string, format?: string, options?: ParseNumberOptions, ): number; export function parseNumber( str: null | undefined, format?: string, options?: ParseNumberOptions, ): undefined; export function parseNumber( str: string | null | undefined, format?: string, options?: ParseNumberOptions, ) { if (str == null) { return undefined; } let num: number; 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; }