|
| 1 | +import { asNumber, type Component, component, setText } from '../../..' |
| 2 | + |
| 3 | +export type BasicNumberProps = { |
| 4 | + value: number |
| 5 | +} |
| 6 | + |
| 7 | +type Logger = { |
| 8 | + onWarn: (message: string) => void |
| 9 | + onError: (message: string) => void |
| 10 | +} |
| 11 | + |
| 12 | +const FALLBACK_LOCALE = 'en' |
| 13 | + |
| 14 | +function getNumberFormatter( |
| 15 | + locale: string, |
| 16 | + rawOptions: string | null, |
| 17 | + logger: Logger = { |
| 18 | + onWarn: console.warn, |
| 19 | + onError: console.error, |
| 20 | + }, |
| 21 | +) { |
| 22 | + const useFallback = () => new Intl.NumberFormat(locale) |
| 23 | + if (!rawOptions) return useFallback() |
| 24 | + const { onWarn, onError } = logger |
| 25 | + |
| 26 | + let o: Intl.NumberFormatOptions = {} |
| 27 | + try { |
| 28 | + o = JSON.parse(rawOptions) |
| 29 | + } catch (error) { |
| 30 | + onError?.(`Invalid JSON: ${error}`) |
| 31 | + return useFallback() |
| 32 | + } |
| 33 | + |
| 34 | + const style = o.style ?? 'decimal' |
| 35 | + |
| 36 | + const drops: string[] = [] |
| 37 | + if (style === 'currency') { |
| 38 | + if ( |
| 39 | + !o.currency || |
| 40 | + typeof o.currency !== 'string' || |
| 41 | + o.currency.length !== 3 |
| 42 | + ) { |
| 43 | + onError?.( |
| 44 | + `style="currency" requires a 3-letter ISO currency (e.g. "CHF").`, |
| 45 | + ) |
| 46 | + return useFallback() |
| 47 | + } |
| 48 | + } else { |
| 49 | + drops.push('currency', 'currencyDisplay', 'currencySign') |
| 50 | + } |
| 51 | + |
| 52 | + if (style === 'unit') { |
| 53 | + if (!o.unit || typeof o.unit !== 'string') { |
| 54 | + onError?.( |
| 55 | + `style="unit" requires a "unit" (e.g. "liter", "kilometer-per-hour").`, |
| 56 | + ) |
| 57 | + return useFallback() |
| 58 | + } |
| 59 | + } else { |
| 60 | + drops.push('unit', 'unitDisplay') |
| 61 | + } |
| 62 | + |
| 63 | + if (o.notation && o.notation !== 'compact') drops.push('compactDisplay') |
| 64 | + |
| 65 | + const sanitized: Intl.NumberFormatOptions = {} |
| 66 | + for (const [k, v] of Object.entries(o)) { |
| 67 | + if (!drops.includes(k)) sanitized[k] = v |
| 68 | + else onWarn?.(`Option "${k}" is ignored for style="${style}".`) |
| 69 | + } |
| 70 | + |
| 71 | + const { minimumFractionDigits: minFD, maximumFractionDigits: maxFD } = |
| 72 | + sanitized |
| 73 | + if (minFD != null && maxFD != null && minFD > maxFD) { |
| 74 | + onWarn?.( |
| 75 | + `minimumFractionDigits (${minFD}) > maximumFractionDigits (${maxFD}); swapping.`, |
| 76 | + ) |
| 77 | + sanitized.minimumFractionDigits = maxFD |
| 78 | + sanitized.maximumFractionDigits = minFD |
| 79 | + } |
| 80 | + const { minimumSignificantDigits: minSD, maximumSignificantDigits: maxSD } = |
| 81 | + sanitized |
| 82 | + if (minSD != null && maxSD != null && minSD > maxSD) { |
| 83 | + onWarn?.( |
| 84 | + `minimumSignificantDigits (${minSD}) > maximumSignificantDigits (${maxSD}); swapping.`, |
| 85 | + ) |
| 86 | + sanitized.minimumSignificantDigits = maxSD |
| 87 | + sanitized.maximumSignificantDigits = minSD |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + const formatter = new Intl.NumberFormat(locale, sanitized) |
| 92 | + if (formatter.resolvedOptions().locale !== locale) |
| 93 | + onWarn( |
| 94 | + `Fall back to locale ${formatter.resolvedOptions().locale} instead of ${locale}`, |
| 95 | + ) |
| 96 | + return formatter |
| 97 | + } catch (e) { |
| 98 | + onError?.( |
| 99 | + `Options rejected by Intl.NumberFormat: ${e instanceof Error ? e.message : String(e)}`, |
| 100 | + ) |
| 101 | + return useFallback() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export default component('basic-number', { value: asNumber() }, el => { |
| 106 | + const formatter = getNumberFormatter( |
| 107 | + el.closest('[lang]')?.getAttribute('lang') || FALLBACK_LOCALE, |
| 108 | + el.getAttribute('options'), |
| 109 | + ) |
| 110 | + return [setText(() => formatter.format(el.value))] |
| 111 | +}) |
| 112 | + |
| 113 | +declare global { |
| 114 | + interface HTMLElementTagNameMap { |
| 115 | + 'basic-number': Component<BasicNumberProps> |
| 116 | + } |
| 117 | +} |
0 commit comments