|
| 1 | +import { formatDate, formatTime, formatDateTime, formatRelative } from './date'; |
| 2 | +import { formatNumber, formatCompact, formatCurrency, formatPercent } from './number'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Returns the locale-aware `format*` functions pre-bound to `locale`, so call sites |
| 6 | + * don't need to repeat `{ locale }` on every call. Each returned function still accepts |
| 7 | + * its normal options and can override the locale per-call if needed. Stateless: safe to |
| 8 | + * call once per request/component, no shared state across concurrent calls. |
| 9 | + */ |
| 10 | +export function createFormatters(locale: string) { |
| 11 | + return { |
| 12 | + formatDate: (input: Parameters<typeof formatDate>[0], options: Parameters<typeof formatDate>[1] = {}) => |
| 13 | + formatDate(input, { locale, ...options }), |
| 14 | + |
| 15 | + formatTime: (input: Parameters<typeof formatTime>[0], options: Parameters<typeof formatTime>[1] = {}) => |
| 16 | + formatTime(input, { locale, ...options }), |
| 17 | + |
| 18 | + formatDateTime: (input: Parameters<typeof formatDateTime>[0], options: Parameters<typeof formatDateTime>[1] = {}) => |
| 19 | + formatDateTime(input, { locale, ...options }), |
| 20 | + |
| 21 | + formatRelative: (input: Parameters<typeof formatRelative>[0], options: Parameters<typeof formatRelative>[1] = {}) => |
| 22 | + formatRelative(input, { locale, ...options }), |
| 23 | + |
| 24 | + formatNumber: (value: Parameters<typeof formatNumber>[0], options: Parameters<typeof formatNumber>[1] = {}) => |
| 25 | + formatNumber(value, { locale, ...options }), |
| 26 | + |
| 27 | + formatCompact: (value: Parameters<typeof formatCompact>[0], options: Parameters<typeof formatCompact>[1] = {}) => |
| 28 | + formatCompact(value, { locale, ...options }), |
| 29 | + |
| 30 | + formatCurrency: (value: Parameters<typeof formatCurrency>[0], options: Parameters<typeof formatCurrency>[1] = {}) => |
| 31 | + formatCurrency(value, { locale, ...options }), |
| 32 | + |
| 33 | + formatPercent: (value: Parameters<typeof formatPercent>[0], options: Parameters<typeof formatPercent>[1] = {}) => |
| 34 | + formatPercent(value, { locale, ...options }), |
| 35 | + }; |
| 36 | +} |
0 commit comments