@automattic/number-formatters 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,260 @@
1
+ import { getSettings } from '@wordpress/date';
2
+ import { FALLBACK_LOCALE } from './constants.js';
3
+ import {
4
+ numberFormatCurrency,
5
+ getCurrencyObject as getCurrencyObjectFromCurrencyFormatter,
6
+ } from './number-format-currency/index.js';
7
+ import { numberFormat, numberFormatCompact } from './number-format.js';
8
+ import type { CurrencyObject, FormatCurrency, FormatNumber, GetCurrencyObject } from './types.js';
9
+
10
+ // Since global is used inside createNumberFormatters, we need to declare it for TS
11
+ declare const global: typeof globalThis;
12
+
13
+ export interface NumberFormatters {
14
+ /**
15
+ * Sets the locale for number formatting
16
+ * @param locale - The locale to use for formatting
17
+ */
18
+ setLocale( locale: string ): void;
19
+
20
+ /**
21
+ * Sets the user's geo location for currency formatting if available
22
+ * @param geoLocation - The geo location to use for formatting
23
+ */
24
+ setGeoLocation( geoLocation: string ): void;
25
+
26
+ /**
27
+ * Formats numbers using locale settings and/or passed options.
28
+ * @param number - The number to format.
29
+ * @param params - The parameters for the formatter.
30
+ * @param params.decimals - The number of decimal places to display.
31
+ * @param params.forceLatin - Whether to force the Latin script.
32
+ * @param params.numberFormatOptions - Additional options to pass to the formatter.
33
+ * @return {string} Formatted number as string, or original number as string if formatting fails.
34
+ */
35
+ formatNumber: FormatNumber;
36
+
37
+ /**
38
+ * Formats numbers using locale settings and/or passed options, with a compact notation.
39
+ * Convenience method for formatting numbers in a compact notation e.g. 1K, 1M, etc.
40
+ * Basically sets `notation: 'compact'` and `maximumFractionDigits: 1` in the options.
41
+ * Everything is overridable by passing the `numberFormatOptions` option.
42
+ * If you want more digits, pass `maximumFractionDigits: 2`.
43
+ * @param number - The number to format.
44
+ * @param params - The parameters for the formatter.
45
+ * @param params.decimals - The number of decimal places to display.
46
+ * @param params.forceLatin - Whether to force the Latin script.
47
+ * @param params.numberFormatOptions - Additional options to pass to the formatter.
48
+ * @return {string} Formatted number as string, or original number as string if formatting fails.
49
+ */
50
+ formatNumberCompact: FormatNumber;
51
+
52
+ /**
53
+ * Formats money with a given currency code.
54
+ *
55
+ * The currency will define the properties to use for this formatting, but
56
+ * those properties can be overridden using the options. Be careful when doing
57
+ * this.
58
+ *
59
+ * For currencies that include decimals, this will always return the amount
60
+ * with decimals included, even if those decimals are zeros. To exclude the
61
+ * zeros, use the `stripZeros` option. For example, the function will normally
62
+ * format `10.00` in `USD` as `$10.00` but when this option is true, it will
63
+ * return `$10` instead.
64
+ *
65
+ * Since rounding errors are common in floating point math, sometimes a price
66
+ * is provided as an integer in the smallest unit of a currency (eg: cents in
67
+ * USD or yen in JPY). Set the `isSmallestUnit` to change the function to
68
+ * operate on integer numbers instead. If this option is not set or false, the
69
+ * function will format the amount `1025` in `USD` as `$1,025.00`, but when the
70
+ * option is true, it will return `$10.25` instead.
71
+ *
72
+ * If the number is NaN, it will be treated as 0.
73
+ *
74
+ * If the currency code is not known, this will assume a default currency
75
+ * similar to USD.
76
+ *
77
+ * If `isSmallestUnit` is set and the number is not an integer, it will be
78
+ * rounded to an integer.
79
+ * @param number - The number to format.
80
+ * @param currency - The currency to format.
81
+ * @param options - The options for the formatter.
82
+ * @param options.stripZeros - Whether to strip zeros.
83
+ * @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
84
+ * @param options.signForPositive - Whether to show the sign for positive numbers.
85
+ * @param options.forceLatin - Whether to force the latin locale.
86
+ * @return {string} A formatted string.
87
+ */
88
+ formatCurrency: FormatCurrency;
89
+
90
+ /**
91
+ * Returns a formatted price object which can be used to manually render a
92
+ * formatted currency (eg: if you wanted to render the currency symbol in a
93
+ * different font size).
94
+ *
95
+ * The currency will define the properties to use for this formatting, but
96
+ * those properties can be overridden using the options. Be careful when doing
97
+ * this.
98
+ *
99
+ * For currencies that include decimals, this will always return the amount
100
+ * with decimals included, even if those decimals are zeros. To exclude the
101
+ * zeros, use the `stripZeros` option. For example, the function will normally
102
+ * format `10.00` in `USD` as `$10.00` but when this option is true, it will
103
+ * return `$10` instead.
104
+ *
105
+ * Since rounding errors are common in floating point math, sometimes a price
106
+ * is provided as an integer in the smallest unit of a currency (eg: cents in
107
+ * USD or yen in JPY). Set the `isSmallestUnit` to change the function to
108
+ * operate on integer numbers instead. If this option is not set or false, the
109
+ * function will format the amount `1025` in `USD` as `$1,025.00`, but when the
110
+ * option is true, it will return `$10.25` instead.
111
+ *
112
+ * Note that the `integer` return value of this function is not a number, but a
113
+ * locale-formatted string which may include symbols like spaces, commas, or
114
+ * periods as group separators. Similarly, the `fraction` property is a string
115
+ * that contains the decimal separator.
116
+ *
117
+ * If the number is NaN, it will be treated as 0.
118
+ *
119
+ * If the currency code is not known, this will assume a default currency
120
+ * similar to USD.
121
+ *
122
+ * If `isSmallestUnit` is set and the number is not an integer, it will be
123
+ * rounded to an integer.
124
+ * @param number - The number to format.
125
+ * @param currency - The currency to format.
126
+ * @param options - The options for the formatter.
127
+ * @param options.stripZeros - Whether to strip zeros.
128
+ * @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
129
+ * @param options.signForPositive - Whether to show the sign for positive numbers.
130
+ * @param options.forceLatin - Whether to force the latin locale.
131
+ * @return {CurrencyObject} A formatted price object.
132
+ */
133
+ getCurrencyObject: GetCurrencyObject;
134
+ }
135
+
136
+ /**
137
+ * Creates a NumberFormatters instance that provides number and currency formatting functionality with locale awareness
138
+ * @return {NumberFormatters} A NumberFormatters instance
139
+ */
140
+ function createNumberFormatters(): NumberFormatters {
141
+ let localeState: string | undefined;
142
+ let geoLocationState: string | undefined;
143
+
144
+ const setLocale = ( locale: string ): void => {
145
+ /**
146
+ * The `Intl.NumberFormat` constructor fails only when there is a variant, divided by `_`.
147
+ * These suffixes should be removed. Values like `de-at` or `es-mx`
148
+ * should all be valid inputs for the constructor.
149
+ */
150
+ localeState = locale;
151
+ };
152
+
153
+ /**
154
+ * Returns the locale defined on the module instance (through `setLocale`)
155
+ * or the "fallback locale" if no locale has been set.
156
+ *
157
+ * The "fallback locale" is defined as:
158
+ * - the current WP user locale, if available through `@wordpress/date` settings (assuming this runs in a WordPress context)
159
+ * - or the browser locale, if available through `window.navigator.language`
160
+ * - or the fallback locale constant (`FALLBACK_LOCALE`)
161
+ *
162
+ * @return {string} The locale to use for formatting.
163
+ */
164
+ const getBrowserSafeLocale = (): string => {
165
+ const {
166
+ l10n: { locale: localeFromUserSettings },
167
+ } = getSettings();
168
+
169
+ return (
170
+ localeState ??
171
+ ( localeFromUserSettings || global?.window?.navigator?.language ) ??
172
+ FALLBACK_LOCALE
173
+ ).split( '_' )[ 0 ];
174
+ };
175
+
176
+ const setGeoLocation = ( geoLocation: string ): void => {
177
+ geoLocationState = geoLocation;
178
+ };
179
+
180
+ const formatNumber: FormatNumber = (
181
+ number,
182
+ { decimals = 0, forceLatin = true, numberFormatOptions = {} } = {}
183
+ ): string => {
184
+ try {
185
+ const formatter = numberFormat( {
186
+ browserSafeLocale: getBrowserSafeLocale(),
187
+ decimals,
188
+ forceLatin,
189
+ numberFormatOptions,
190
+ } );
191
+
192
+ return formatter.format( number );
193
+ } catch {
194
+ return String( number );
195
+ }
196
+ };
197
+
198
+ const formatNumberCompact: FormatNumber = (
199
+ number,
200
+ { decimals = 0, forceLatin = true, numberFormatOptions = {} } = {}
201
+ ): string => {
202
+ try {
203
+ const formatter = numberFormatCompact( {
204
+ browserSafeLocale: getBrowserSafeLocale(),
205
+ decimals,
206
+ forceLatin,
207
+ numberFormatOptions,
208
+ } );
209
+
210
+ return formatter.format( number );
211
+ } catch {
212
+ return String( number );
213
+ }
214
+ };
215
+
216
+ const formatCurrency: FormatCurrency = (
217
+ number,
218
+ currency,
219
+ { stripZeros = false, isSmallestUnit = false, signForPositive = false, forceLatin = true } = {}
220
+ ): string => {
221
+ return numberFormatCurrency( {
222
+ number,
223
+ currency,
224
+ browserSafeLocale: getBrowserSafeLocale(),
225
+ stripZeros,
226
+ isSmallestUnit,
227
+ signForPositive,
228
+ geoLocation: geoLocationState,
229
+ forceLatin,
230
+ } );
231
+ };
232
+
233
+ const getCurrencyObject: GetCurrencyObject = (
234
+ number,
235
+ currency,
236
+ { stripZeros = false, isSmallestUnit = false, signForPositive = false, forceLatin = true } = {}
237
+ ): CurrencyObject => {
238
+ return getCurrencyObjectFromCurrencyFormatter( {
239
+ number,
240
+ currency,
241
+ browserSafeLocale: getBrowserSafeLocale(),
242
+ stripZeros,
243
+ isSmallestUnit,
244
+ signForPositive,
245
+ geoLocation: geoLocationState,
246
+ forceLatin,
247
+ } );
248
+ };
249
+
250
+ return {
251
+ setLocale,
252
+ setGeoLocation,
253
+ formatNumber,
254
+ formatNumberCompact,
255
+ formatCurrency,
256
+ getCurrencyObject,
257
+ };
258
+ }
259
+
260
+ export default createNumberFormatters;
@@ -0,0 +1,53 @@
1
+ import debugFactory from 'debug';
2
+ import { FALLBACK_LOCALE } from './constants.js';
3
+
4
+ const debug = debugFactory( 'number-formatters:get-cached-formatter' );
5
+
6
+ const formatterCache = new Map();
7
+
8
+ interface Params {
9
+ locale: string;
10
+ options?: Intl.NumberFormatOptions;
11
+ fallbackLocale?: string;
12
+ retries?: number;
13
+ }
14
+
15
+ /**
16
+ * Get a cached formatter for a given locale and options.
17
+ * @param params - The parameters for the formatter.
18
+ * @param params.locale - The locale to format the number in.
19
+ * @param params.options - Intl.NumberFormatOptions to pass to the formatter.
20
+ * @param params.fallbackLocale - The locale to fallback to if the locale is not supported.
21
+ * @param params.retries - The number of retries to attempt if the formatter is not created.
22
+ * @return {Intl.NumberFormat} A cached formatter for the given locale and options.
23
+ */
24
+ export function getCachedFormatter( {
25
+ locale,
26
+ fallbackLocale = FALLBACK_LOCALE,
27
+ options,
28
+ retries = 1,
29
+ }: Params ): Intl.NumberFormat {
30
+ const cacheKey = JSON.stringify( [ locale, options ] );
31
+
32
+ try {
33
+ return (
34
+ formatterCache.get( cacheKey ) ??
35
+ formatterCache.set( cacheKey, new Intl.NumberFormat( locale, options ) ).get( cacheKey )
36
+ );
37
+ } catch ( error ) {
38
+ // If the locale is invalid, creating the NumberFormat will throw.
39
+ debug(
40
+ `Intl.NumberFormat was called with a non-existent locale "${ locale }"; falling back to ${ fallbackLocale }`
41
+ );
42
+
43
+ if ( retries ) {
44
+ return getCachedFormatter( {
45
+ locale: fallbackLocale,
46
+ options,
47
+ retries: retries - 1,
48
+ } );
49
+ }
50
+
51
+ throw error;
52
+ }
53
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ import createNumberFormatters from './create-number-formatters.js';
2
+
3
+ const defaultFormatter = createNumberFormatters();
4
+
5
+ export const {
6
+ setLocale,
7
+ setGeoLocation,
8
+ formatNumber,
9
+ formatNumberCompact,
10
+ formatCurrency,
11
+ getCurrencyObject,
12
+ } = defaultFormatter;
13
+
14
+ export { createNumberFormatters };
15
+
16
+ export * from './types.js';
17
+
18
+ // We can optionally export the formatters individually if we want to use them in a more granular way.
19
+ // export { numberFormat, numberFormatCompact, numberFormatCurrency, getCurrencyObject };