|
| 1 | +import { getLocale, getDefaultLocale, setDefaultLocale } from './FormattingLocale.js'; |
| 2 | + |
| 3 | +export default class NumberFormatter { |
| 4 | + #number; |
| 5 | + #rangeEnd; |
| 6 | + #options; |
| 7 | + #presets = { |
| 8 | + decimal: { |
| 9 | + style: 'decimal', |
| 10 | + }, |
| 11 | + percent: { |
| 12 | + style: 'percent', |
| 13 | + }, |
| 14 | + }; |
| 15 | + |
| 16 | + constructor(number, options) { |
| 17 | + if (Array.isArray(number)) { |
| 18 | + this.#number = this.#normalizeNumber(number[0]); |
| 19 | + this.#rangeEnd = this.#normalizeNumber(number[1]); |
| 20 | + } else { |
| 21 | + this.#number = this.#normalizeNumber(number); |
| 22 | + } |
| 23 | + |
| 24 | + this.#options = this.#normalizeOptions(options); |
| 25 | + } |
| 26 | + |
| 27 | + number(value) { |
| 28 | + return new NumberFormatter(value, this.#options); |
| 29 | + } |
| 30 | + |
| 31 | + options(options) { |
| 32 | + const value = this.#rangeEnd !== undefined ? [this.#number, this.#rangeEnd] : this.#number; |
| 33 | + |
| 34 | + return new NumberFormatter(value, options); |
| 35 | + } |
| 36 | + |
| 37 | + toString() { |
| 38 | + try { |
| 39 | + if (this.#rangeEnd !== undefined) { |
| 40 | + return Intl.NumberFormat(this.locale, this.#options).formatRange(this.#number, this.#rangeEnd); |
| 41 | + } |
| 42 | + |
| 43 | + return Intl.NumberFormat(this.locale, this.#options).format(this.#number); |
| 44 | + } catch (e) { |
| 45 | + return 'Invalid Number'; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static format(number, options) { |
| 50 | + return new NumberFormatter(number, options).toString(); |
| 51 | + } |
| 52 | + |
| 53 | + format(number, options) { |
| 54 | + return this.number(number).options(options).toString(); |
| 55 | + } |
| 56 | + |
| 57 | + formatRange(start, end, options) { |
| 58 | + return this.number([start, end]).options(options ?? this.#options).toString(); |
| 59 | + } |
| 60 | + |
| 61 | + static formatRange(start, end, options) { |
| 62 | + return new NumberFormatter([start, end], options).toString(); |
| 63 | + } |
| 64 | + |
| 65 | + static get defaultLocale() { |
| 66 | + return getDefaultLocale(); |
| 67 | + } |
| 68 | + |
| 69 | + static set defaultLocale(locale) { |
| 70 | + setDefaultLocale(locale); |
| 71 | + } |
| 72 | + |
| 73 | + withLocale(locale, callback) { |
| 74 | + const previousLocale = getDefaultLocale(); |
| 75 | + setDefaultLocale(locale); |
| 76 | + |
| 77 | + try { |
| 78 | + return callback(this); |
| 79 | + } finally { |
| 80 | + setDefaultLocale(previousLocale); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + setDefaultLocale(locale) { |
| 85 | + setDefaultLocale(locale); |
| 86 | + } |
| 87 | + |
| 88 | + get locale() { |
| 89 | + return getLocale(); |
| 90 | + } |
| 91 | + |
| 92 | + #normalizeNumber(number) { |
| 93 | + if (number === null || number === undefined) return 0; |
| 94 | + |
| 95 | + const n = Number(number); |
| 96 | + |
| 97 | + if (Number.isNaN(n)) throw new Error('Invalid Number'); |
| 98 | + |
| 99 | + return n; |
| 100 | + } |
| 101 | + |
| 102 | + #normalizeOptions(options) { |
| 103 | + if (!options) options = 'decimal'; |
| 104 | + |
| 105 | + if (typeof options === 'string') { |
| 106 | + if (!this.#presets[options]) throw new Error(`Invalid number format: ${options}`); |
| 107 | + |
| 108 | + return this.#presets[options]; |
| 109 | + } |
| 110 | + |
| 111 | + return options; |
| 112 | + } |
| 113 | +} |
0 commit comments