/** * Shared number-formatting helper used by `` and * ``. Both components format a numeric value for display using * the same algorithm: if an explicit `format` is provided, pass the value * through `Intl.NumberFormat` verbatim; otherwise use a percent formatter * applied to `value / 100`. * * A module-scope cache keyed by `(locale, options)` keeps repeated formats * O(1). Construction of `Intl.NumberFormat` is the expensive step; the * formatters themselves are cheap to re-use. */ const FORMATTER_CACHE = new Map(); function getFormatter( locale: Intl.LocalesArgument | undefined, options: Intl.NumberFormatOptions | undefined, ): Intl.NumberFormat { const key = JSON.stringify({ locale, options }); const cached = FORMATTER_CACHE.get(key); if (cached) return cached; const formatter = new Intl.NumberFormat(locale, options); FORMATTER_CACHE.set(key, formatter); return formatter; } /** * Formats a numeric value for display. Returns `""` if `value` is `null` * (matches progress's indeterminate state). If `format` is undefined, the * default is a percentage applied to `value / 100` — so a raw value of `42` * yields `"42%"` under an `en-US` locale. */ export function formatNumberValue( value: number | null, locale: Intl.LocalesArgument | undefined, format: Intl.NumberFormatOptions | undefined, ): string { if (value == null) return ""; if (!format) { return getFormatter(locale, { style: "percent" }).format(value / 100); } return getFormatter(locale, format).format(value); }