import { EMPTY } from "./placeholder"; /** * Format a numeric (or numeric-string) amount as a localized currency string. * * Returns the canonical {@link EMPTY} placeholder for null/undefined/NaN * inputs so callers can pass raw schema values (which often arrive as decimal * strings) without pre-coercing. */ export function formatCurrency( value: string | number | null | undefined, currency = "USD", maxDecimals?: number, ): string { if (value == null) return EMPTY; const n = typeof value === "number" ? value : Number(value); if (!Number.isFinite(n)) return EMPTY; const opts: Intl.NumberFormatOptions = { style: "currency", currency }; if (maxDecimals !== undefined) { opts.maximumFractionDigits = maxDecimals; } try { return new Intl.NumberFormat("en-US", opts).format(n); } catch { return n.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: maxDecimals ?? 2, }); } }