21 lines
895 B
TypeScript
21 lines
895 B
TypeScript
// Locale-aware currency formatter. Currency code is required (ISO-4217) so
|
|
// callers cannot accidentally render an amount without the symbol — matches
|
|
// billing/invoice surfaces that always know their currency.
|
|
|
|
import { getCachedFormatter } from "./cache.js";
|
|
|
|
export type FormatCurrencyOptions = {
|
|
locale: string;
|
|
currency: string;
|
|
minimumFractionDigits?: number;
|
|
maximumFractionDigits?: number;
|
|
currencyDisplay?: Intl.NumberFormatOptions["currencyDisplay"];
|
|
};
|
|
|
|
const currencyFormatters = new Map<string, Intl.NumberFormat>();
|
|
|
|
export function formatCurrency(value: number, options: FormatCurrencyOptions): string {
|
|
const { locale, currency, ...rest } = options;
|
|
return getCachedFormatter(currencyFormatters, locale, { style: "currency", currency, ...rest }, (formatterLocale, formatterOptions) => new Intl.NumberFormat(formatterLocale, formatterOptions)).format(value);
|
|
}
|