@automattic/number-formatters 1.2.4 → 1.2.6
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.
- package/CHANGELOG.md +10 -0
- package/dist/index.cjs +701 -0
- package/dist/index.d.cts +302 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.js +670 -0
- package/package.json +9 -12
- package/src/constants.ts +7 -4
- package/dist/cjs/constants.cjs +0 -9
- package/dist/cjs/create-number-formatters.cjs +0 -113
- package/dist/cjs/get-cached-formatter.cjs +0 -36
- package/dist/cjs/index.cjs +0 -10
- package/dist/cjs/number-format-currency/currencies.cjs +0 -488
- package/dist/cjs/number-format-currency/index.cjs +0 -376
- package/dist/cjs/number-format.cjs +0 -59
- package/dist/cjs/types.cjs +0 -2
- package/dist/esm/constants.js +0 -5
- package/dist/esm/create-number-formatters.js +0 -111
- package/dist/esm/get-cached-formatter.js +0 -32
- package/dist/esm/index.js +0 -6
- package/dist/esm/number-format-currency/currencies.js +0 -485
- package/dist/esm/number-format-currency/index.js +0 -371
- package/dist/esm/number-format.js +0 -55
- package/dist/esm/types.js +0 -1
- package/dist/types/constants.d.ts +0 -3
- package/dist/types/create-number-formatters.d.ts +0 -154
- package/dist/types/get-cached-formatter.d.ts +0 -17
- package/dist/types/index.d.ts +0 -4
- package/dist/types/number-format-currency/currencies.d.ts +0 -2
- package/dist/types/number-format-currency/index.d.ts +0 -91
- package/dist/types/number-format.d.ts +0 -22
- package/dist/types/types.d.ts +0 -142
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface NumberFormatParams {
|
|
3
|
+
/**
|
|
4
|
+
* Browser-safe locale string that works with Intl.NumberFormat e.g. 'en-US' (not 'en_US').
|
|
5
|
+
*/
|
|
6
|
+
browserSafeLocale: string;
|
|
7
|
+
/**
|
|
8
|
+
* Number of decimal places to use.
|
|
9
|
+
* This is just convenience over setting `minimumFractionDigits`, `maximumFractionDigits` to the same value.
|
|
10
|
+
* ( default = 0 )
|
|
11
|
+
*/
|
|
12
|
+
decimals?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether to use latin numbers by default ( default = true ).
|
|
15
|
+
*/
|
|
16
|
+
forceLatin?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* `Intl.NumberFormat` options to pass through.
|
|
19
|
+
* `minimumFractionDigits` & `maximumFractionDigits` will override `decimals` if set.
|
|
20
|
+
*/
|
|
21
|
+
numberFormatOptions?: Intl.NumberFormatOptions;
|
|
22
|
+
}
|
|
23
|
+
interface CurrencyOverride {
|
|
24
|
+
symbol?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Smallest-unit exponent for this currency, used when the browser's ICU
|
|
27
|
+
* `maximumFractionDigits` disagrees with the API's smallest-unit encoding,
|
|
28
|
+
* or when this package's hard-coded fallback exponent (see
|
|
29
|
+
* `SMALLEST_UNIT_EXPONENT_OVERRIDES` in `number-format-currency/index.ts`)
|
|
30
|
+
* disagrees with the host application's source of truth.
|
|
31
|
+
*
|
|
32
|
+
* For example, modern browser ICU (Chrome / Node 24+) reports IDR as
|
|
33
|
+
* 0-decimal, but this package's hard-coded fallback applies an exponent of
|
|
34
|
+
* 2 for legacy compatibility. The WPCOM currencies endpoint can send
|
|
35
|
+
* `{ "IDR": { "decimal": 0 } }` to override that hard-coded 2 back to 0.
|
|
36
|
+
*/
|
|
37
|
+
decimal?: number;
|
|
38
|
+
}
|
|
39
|
+
interface NumberFormatCurrencyParams {
|
|
40
|
+
/**
|
|
41
|
+
* Number to format.
|
|
42
|
+
*/
|
|
43
|
+
number: number;
|
|
44
|
+
/**
|
|
45
|
+
* Browser-safe locale string that works with Intl.NumberFormat e.g. 'en-US' (not 'en_US').
|
|
46
|
+
*/
|
|
47
|
+
browserSafeLocale: string;
|
|
48
|
+
/**
|
|
49
|
+
* Currency code.
|
|
50
|
+
*/
|
|
51
|
+
currency: string;
|
|
52
|
+
/**
|
|
53
|
+
* Whether to use latin numbers by default ( default = true ).
|
|
54
|
+
*/
|
|
55
|
+
forceLatin?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The user's geo location if available.
|
|
58
|
+
*/
|
|
59
|
+
geoLocation?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Forces any decimal zeros to be hidden if set.
|
|
62
|
+
*
|
|
63
|
+
* For example, the function will normally format `10.00` in `USD` as
|
|
64
|
+
* `$10.00` but when this option is true, it will return `$10` instead.
|
|
65
|
+
*
|
|
66
|
+
* For currencies without decimals (eg: JPY), this has no effect.
|
|
67
|
+
*/
|
|
68
|
+
stripZeros?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Changes function to treat number as an integer in the currency's smallest unit.
|
|
71
|
+
*
|
|
72
|
+
* Since rounding errors are common in floating point math, sometimes a price
|
|
73
|
+
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
74
|
+
* USD or yen in JPY). If this option is false, the function will format the
|
|
75
|
+
* amount `1025` in `USD` as `$1,025.00`, but when the option is true, it
|
|
76
|
+
* will return `$10.25` instead.
|
|
77
|
+
*/
|
|
78
|
+
isSmallestUnit?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* If the number is greater than 0, setting this to true will include its
|
|
81
|
+
* sign (eg: `+$35.00`). Has no effect on negative numbers or 0.
|
|
82
|
+
*/
|
|
83
|
+
signForPositive?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Dynamic currency overrides, typically supplied by the host application
|
|
86
|
+
* (eg: from a remote endpoint) via `setCurrencyOverrides`.
|
|
87
|
+
*
|
|
88
|
+
* When provided, entries here take precedence over the hard-coded defaults
|
|
89
|
+
* baked into the package on a per-field basis. Anything not specified in
|
|
90
|
+
* this map falls back to the hard-coded defaults, so passing a partial map
|
|
91
|
+
* is safe.
|
|
92
|
+
*/
|
|
93
|
+
currencyOverrides?: Record<string, CurrencyOverride>;
|
|
94
|
+
}
|
|
95
|
+
interface CurrencyObject {
|
|
96
|
+
/**
|
|
97
|
+
* The negative sign for the price, if it is negative, or the positive sign
|
|
98
|
+
* if `signForPositive` is set.
|
|
99
|
+
*/
|
|
100
|
+
sign: '-' | '+' | '';
|
|
101
|
+
/**
|
|
102
|
+
* The currency symbol for the formatted price.
|
|
103
|
+
*
|
|
104
|
+
* Note that the symbol's position depends on the `symbolPosition` property,
|
|
105
|
+
* and keep RTL locales in mind.
|
|
106
|
+
*/
|
|
107
|
+
symbol: string;
|
|
108
|
+
/**
|
|
109
|
+
* The position of the currency symbol relative to the formatted price.
|
|
110
|
+
*/
|
|
111
|
+
symbolPosition: 'before' | 'after';
|
|
112
|
+
/**
|
|
113
|
+
* The section of the formatted price before the decimal.
|
|
114
|
+
*
|
|
115
|
+
* Note that this is not a number, but a locale-formatted string which may
|
|
116
|
+
* include symbols like spaces, commas, or periods as group separators.
|
|
117
|
+
*/
|
|
118
|
+
integer: string;
|
|
119
|
+
/**
|
|
120
|
+
* The section of the formatted price after and including the decimal.
|
|
121
|
+
*
|
|
122
|
+
* Note that this is not a number, but a locale-formatted string which may
|
|
123
|
+
* include symbols like spaces, commas, or periods as the decimal separator.
|
|
124
|
+
*/
|
|
125
|
+
fraction: string;
|
|
126
|
+
/**
|
|
127
|
+
* True if the formatted number has a non-0 decimal part.
|
|
128
|
+
*/
|
|
129
|
+
hasNonZeroFraction: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* The raw floating-point version of the number prepared for formatting,
|
|
132
|
+
* after unit conversion and precision scaling.
|
|
133
|
+
*
|
|
134
|
+
* For non-decimal currencies (eg: JPY) this will actually be an integer.
|
|
135
|
+
* Otherwise it will likely be a floating-point number. Be careful with this!
|
|
136
|
+
* It should not be used for math if possible because of floating-point
|
|
137
|
+
* rounding issues! Use the smallest unit instead.
|
|
138
|
+
*/
|
|
139
|
+
floatValue: number;
|
|
140
|
+
}
|
|
141
|
+
type FormatNumber = (number: number, options?: Omit<NumberFormatParams, 'browserSafeLocale'>) => string;
|
|
142
|
+
type FormatCurrency = (number: number, currency: string, options?: Omit<NumberFormatCurrencyParams, 'number' | 'currency' | 'browserSafeLocale' | 'geoLocation' | 'currencyOverrides'>) => string;
|
|
143
|
+
type GetCurrencyObject = (number: number, currency: string, options?: Omit<NumberFormatCurrencyParams, 'number' | 'currency' | 'browserSafeLocale' | 'geoLocation' | 'currencyOverrides'>) => CurrencyObject;
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/create-number-formatters.d.ts
|
|
146
|
+
declare global {
|
|
147
|
+
interface Window {
|
|
148
|
+
wp?: {
|
|
149
|
+
date?: {
|
|
150
|
+
getSettings?: () => {
|
|
151
|
+
l10n?: {
|
|
152
|
+
locale?: string;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
interface NumberFormatters {
|
|
160
|
+
/**
|
|
161
|
+
* Sets the locale for number formatting
|
|
162
|
+
* @param locale - The locale to use for formatting
|
|
163
|
+
*/
|
|
164
|
+
setLocale(locale: string): void;
|
|
165
|
+
/**
|
|
166
|
+
* Sets the user's geo location for currency formatting if available
|
|
167
|
+
* @param geoLocation - The geo location to use for formatting
|
|
168
|
+
*/
|
|
169
|
+
setGeoLocation(geoLocation: string): void;
|
|
170
|
+
/**
|
|
171
|
+
* Sets a dynamic map of per-currency overrides used by currency formatting.
|
|
172
|
+
*
|
|
173
|
+
* Typical use: load `{ "IDR": { "decimal": 0 } }` from the WPCOM currencies
|
|
174
|
+
* endpoint at app boot and pass the parsed object here. Each entry can carry
|
|
175
|
+
* a `symbol` and/or `decimal` (the smallest-unit exponent), and additional
|
|
176
|
+
* fields may be added to `CurrencyOverride` in the future.
|
|
177
|
+
*
|
|
178
|
+
* If this setter is never called, the package falls back to the hard-coded
|
|
179
|
+
* defaults shipped with the package, preserving previous behavior.
|
|
180
|
+
*
|
|
181
|
+
* When called with a partial map, missing currencies or fields fall back to
|
|
182
|
+
* the hard-coded defaults — passing `{ IDR: { decimal: 0 } }` does not clear
|
|
183
|
+
* the default IDR symbol, for example.
|
|
184
|
+
* @param overrides - Map of currency code to override settings
|
|
185
|
+
*/
|
|
186
|
+
setCurrencyOverrides(overrides: Record<string, CurrencyOverride>): void;
|
|
187
|
+
/**
|
|
188
|
+
* Formats numbers using locale settings and/or passed options.
|
|
189
|
+
* @param number - The number to format.
|
|
190
|
+
* @param params - The parameters for the formatter.
|
|
191
|
+
* @param params.decimals - The number of decimal places to display.
|
|
192
|
+
* @param params.forceLatin - Whether to force the Latin script.
|
|
193
|
+
* @param params.numberFormatOptions - Additional options to pass to the formatter.
|
|
194
|
+
* @return {string} Formatted number as string, or original number as string if formatting fails.
|
|
195
|
+
*/
|
|
196
|
+
formatNumber: FormatNumber;
|
|
197
|
+
/**
|
|
198
|
+
* Formats numbers using locale settings and/or passed options, with a compact notation.
|
|
199
|
+
* Convenience method for formatting numbers in a compact notation e.g. 1K, 1M, etc.
|
|
200
|
+
* Basically sets `notation: 'compact'` and `maximumFractionDigits: 1` in the options.
|
|
201
|
+
* Everything is overridable by passing the `numberFormatOptions` option.
|
|
202
|
+
* If you want more digits, pass `maximumFractionDigits: 2`.
|
|
203
|
+
* @param number - The number to format.
|
|
204
|
+
* @param params - The parameters for the formatter.
|
|
205
|
+
* @param params.decimals - The number of decimal places to display.
|
|
206
|
+
* @param params.forceLatin - Whether to force the Latin script.
|
|
207
|
+
* @param params.numberFormatOptions - Additional options to pass to the formatter.
|
|
208
|
+
* @return {string} Formatted number as string, or original number as string if formatting fails.
|
|
209
|
+
*/
|
|
210
|
+
formatNumberCompact: FormatNumber;
|
|
211
|
+
/**
|
|
212
|
+
* Formats money with a given currency code.
|
|
213
|
+
*
|
|
214
|
+
* The currency will define the properties to use for this formatting, but
|
|
215
|
+
* those properties can be overridden using the options. Be careful when doing
|
|
216
|
+
* this.
|
|
217
|
+
*
|
|
218
|
+
* For currencies that include decimals, this will always return the amount
|
|
219
|
+
* with decimals included, even if those decimals are zeros. To exclude the
|
|
220
|
+
* zeros, use the `stripZeros` option. For example, the function will normally
|
|
221
|
+
* format `10.00` in `USD` as `$10.00` but when this option is true, it will
|
|
222
|
+
* return `$10` instead.
|
|
223
|
+
*
|
|
224
|
+
* Since rounding errors are common in floating point math, sometimes a price
|
|
225
|
+
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
226
|
+
* USD or yen in JPY). Set the `isSmallestUnit` to change the function to
|
|
227
|
+
* operate on integer numbers instead. If this option is not set or false, the
|
|
228
|
+
* function will format the amount `1025` in `USD` as `$1,025.00`, but when the
|
|
229
|
+
* option is true, it will return `$10.25` instead.
|
|
230
|
+
*
|
|
231
|
+
* If the number is NaN, it will be treated as 0.
|
|
232
|
+
*
|
|
233
|
+
* If the currency code is not known, this will assume a default currency
|
|
234
|
+
* similar to USD.
|
|
235
|
+
*
|
|
236
|
+
* If `isSmallestUnit` is set and the number is not an integer, it will be
|
|
237
|
+
* rounded to an integer.
|
|
238
|
+
* @param number - The number to format.
|
|
239
|
+
* @param currency - The currency to format.
|
|
240
|
+
* @param options - The options for the formatter.
|
|
241
|
+
* @param options.stripZeros - Whether to strip zeros.
|
|
242
|
+
* @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
|
|
243
|
+
* @param options.signForPositive - Whether to show the sign for positive numbers.
|
|
244
|
+
* @param options.forceLatin - Whether to force the latin locale.
|
|
245
|
+
* @return {string} A formatted string.
|
|
246
|
+
*/
|
|
247
|
+
formatCurrency: FormatCurrency;
|
|
248
|
+
/**
|
|
249
|
+
* Returns a formatted price object which can be used to manually render a
|
|
250
|
+
* formatted currency (eg: if you wanted to render the currency symbol in a
|
|
251
|
+
* different font size).
|
|
252
|
+
*
|
|
253
|
+
* The currency will define the properties to use for this formatting, but
|
|
254
|
+
* those properties can be overridden using the options. Be careful when doing
|
|
255
|
+
* this.
|
|
256
|
+
*
|
|
257
|
+
* For currencies that include decimals, this will always return the amount
|
|
258
|
+
* with decimals included, even if those decimals are zeros. To exclude the
|
|
259
|
+
* zeros, use the `stripZeros` option. For example, the function will normally
|
|
260
|
+
* format `10.00` in `USD` as `$10.00` but when this option is true, it will
|
|
261
|
+
* return `$10` instead.
|
|
262
|
+
*
|
|
263
|
+
* Since rounding errors are common in floating point math, sometimes a price
|
|
264
|
+
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
265
|
+
* USD or yen in JPY). Set the `isSmallestUnit` to change the function to
|
|
266
|
+
* operate on integer numbers instead. If this option is not set or false, the
|
|
267
|
+
* function will format the amount `1025` in `USD` as `$1,025.00`, but when the
|
|
268
|
+
* option is true, it will return `$10.25` instead.
|
|
269
|
+
*
|
|
270
|
+
* Note that the `integer` return value of this function is not a number, but a
|
|
271
|
+
* locale-formatted string which may include symbols like spaces, commas, or
|
|
272
|
+
* periods as group separators. Similarly, the `fraction` property is a string
|
|
273
|
+
* that contains the decimal separator.
|
|
274
|
+
*
|
|
275
|
+
* If the number is NaN, it will be treated as 0.
|
|
276
|
+
*
|
|
277
|
+
* If the currency code is not known, this will assume a default currency
|
|
278
|
+
* similar to USD.
|
|
279
|
+
*
|
|
280
|
+
* If `isSmallestUnit` is set and the number is not an integer, it will be
|
|
281
|
+
* rounded to an integer.
|
|
282
|
+
* @param number - The number to format.
|
|
283
|
+
* @param currency - The currency to format.
|
|
284
|
+
* @param options - The options for the formatter.
|
|
285
|
+
* @param options.stripZeros - Whether to strip zeros.
|
|
286
|
+
* @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
|
|
287
|
+
* @param options.signForPositive - Whether to show the sign for positive numbers.
|
|
288
|
+
* @param options.forceLatin - Whether to force the latin locale.
|
|
289
|
+
* @return {CurrencyObject} A formatted price object.
|
|
290
|
+
*/
|
|
291
|
+
getCurrencyObject: GetCurrencyObject;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates a NumberFormatters instance that provides number and currency formatting functionality with locale awareness
|
|
295
|
+
* @return {NumberFormatters} A NumberFormatters instance
|
|
296
|
+
*/
|
|
297
|
+
declare function createNumberFormatters(): NumberFormatters;
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/index.d.ts
|
|
300
|
+
declare const setLocale: (locale: string) => void, setGeoLocation: (geoLocation: string) => void, setCurrencyOverrides: (overrides: Record<string, CurrencyOverride>) => void, formatNumber: FormatNumber, formatNumberCompact: FormatNumber, formatCurrency: FormatCurrency, getCurrencyObject: GetCurrencyObject;
|
|
301
|
+
//#endregion
|
|
302
|
+
export { type CurrencyObject, type CurrencyOverride, type FormatCurrency, type FormatNumber, type GetCurrencyObject, type NumberFormatCurrencyParams, type NumberFormatParams, createNumberFormatters, formatCurrency, formatNumber, formatNumberCompact, getCurrencyObject, setCurrencyOverrides, setGeoLocation, setLocale };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface NumberFormatParams {
|
|
3
|
+
/**
|
|
4
|
+
* Browser-safe locale string that works with Intl.NumberFormat e.g. 'en-US' (not 'en_US').
|
|
5
|
+
*/
|
|
6
|
+
browserSafeLocale: string;
|
|
7
|
+
/**
|
|
8
|
+
* Number of decimal places to use.
|
|
9
|
+
* This is just convenience over setting `minimumFractionDigits`, `maximumFractionDigits` to the same value.
|
|
10
|
+
* ( default = 0 )
|
|
11
|
+
*/
|
|
12
|
+
decimals?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether to use latin numbers by default ( default = true ).
|
|
15
|
+
*/
|
|
16
|
+
forceLatin?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* `Intl.NumberFormat` options to pass through.
|
|
19
|
+
* `minimumFractionDigits` & `maximumFractionDigits` will override `decimals` if set.
|
|
20
|
+
*/
|
|
21
|
+
numberFormatOptions?: Intl.NumberFormatOptions;
|
|
22
|
+
}
|
|
23
|
+
interface CurrencyOverride {
|
|
24
|
+
symbol?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Smallest-unit exponent for this currency, used when the browser's ICU
|
|
27
|
+
* `maximumFractionDigits` disagrees with the API's smallest-unit encoding,
|
|
28
|
+
* or when this package's hard-coded fallback exponent (see
|
|
29
|
+
* `SMALLEST_UNIT_EXPONENT_OVERRIDES` in `number-format-currency/index.ts`)
|
|
30
|
+
* disagrees with the host application's source of truth.
|
|
31
|
+
*
|
|
32
|
+
* For example, modern browser ICU (Chrome / Node 24+) reports IDR as
|
|
33
|
+
* 0-decimal, but this package's hard-coded fallback applies an exponent of
|
|
34
|
+
* 2 for legacy compatibility. The WPCOM currencies endpoint can send
|
|
35
|
+
* `{ "IDR": { "decimal": 0 } }` to override that hard-coded 2 back to 0.
|
|
36
|
+
*/
|
|
37
|
+
decimal?: number;
|
|
38
|
+
}
|
|
39
|
+
interface NumberFormatCurrencyParams {
|
|
40
|
+
/**
|
|
41
|
+
* Number to format.
|
|
42
|
+
*/
|
|
43
|
+
number: number;
|
|
44
|
+
/**
|
|
45
|
+
* Browser-safe locale string that works with Intl.NumberFormat e.g. 'en-US' (not 'en_US').
|
|
46
|
+
*/
|
|
47
|
+
browserSafeLocale: string;
|
|
48
|
+
/**
|
|
49
|
+
* Currency code.
|
|
50
|
+
*/
|
|
51
|
+
currency: string;
|
|
52
|
+
/**
|
|
53
|
+
* Whether to use latin numbers by default ( default = true ).
|
|
54
|
+
*/
|
|
55
|
+
forceLatin?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The user's geo location if available.
|
|
58
|
+
*/
|
|
59
|
+
geoLocation?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Forces any decimal zeros to be hidden if set.
|
|
62
|
+
*
|
|
63
|
+
* For example, the function will normally format `10.00` in `USD` as
|
|
64
|
+
* `$10.00` but when this option is true, it will return `$10` instead.
|
|
65
|
+
*
|
|
66
|
+
* For currencies without decimals (eg: JPY), this has no effect.
|
|
67
|
+
*/
|
|
68
|
+
stripZeros?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Changes function to treat number as an integer in the currency's smallest unit.
|
|
71
|
+
*
|
|
72
|
+
* Since rounding errors are common in floating point math, sometimes a price
|
|
73
|
+
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
74
|
+
* USD or yen in JPY). If this option is false, the function will format the
|
|
75
|
+
* amount `1025` in `USD` as `$1,025.00`, but when the option is true, it
|
|
76
|
+
* will return `$10.25` instead.
|
|
77
|
+
*/
|
|
78
|
+
isSmallestUnit?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* If the number is greater than 0, setting this to true will include its
|
|
81
|
+
* sign (eg: `+$35.00`). Has no effect on negative numbers or 0.
|
|
82
|
+
*/
|
|
83
|
+
signForPositive?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Dynamic currency overrides, typically supplied by the host application
|
|
86
|
+
* (eg: from a remote endpoint) via `setCurrencyOverrides`.
|
|
87
|
+
*
|
|
88
|
+
* When provided, entries here take precedence over the hard-coded defaults
|
|
89
|
+
* baked into the package on a per-field basis. Anything not specified in
|
|
90
|
+
* this map falls back to the hard-coded defaults, so passing a partial map
|
|
91
|
+
* is safe.
|
|
92
|
+
*/
|
|
93
|
+
currencyOverrides?: Record<string, CurrencyOverride>;
|
|
94
|
+
}
|
|
95
|
+
interface CurrencyObject {
|
|
96
|
+
/**
|
|
97
|
+
* The negative sign for the price, if it is negative, or the positive sign
|
|
98
|
+
* if `signForPositive` is set.
|
|
99
|
+
*/
|
|
100
|
+
sign: '-' | '+' | '';
|
|
101
|
+
/**
|
|
102
|
+
* The currency symbol for the formatted price.
|
|
103
|
+
*
|
|
104
|
+
* Note that the symbol's position depends on the `symbolPosition` property,
|
|
105
|
+
* and keep RTL locales in mind.
|
|
106
|
+
*/
|
|
107
|
+
symbol: string;
|
|
108
|
+
/**
|
|
109
|
+
* The position of the currency symbol relative to the formatted price.
|
|
110
|
+
*/
|
|
111
|
+
symbolPosition: 'before' | 'after';
|
|
112
|
+
/**
|
|
113
|
+
* The section of the formatted price before the decimal.
|
|
114
|
+
*
|
|
115
|
+
* Note that this is not a number, but a locale-formatted string which may
|
|
116
|
+
* include symbols like spaces, commas, or periods as group separators.
|
|
117
|
+
*/
|
|
118
|
+
integer: string;
|
|
119
|
+
/**
|
|
120
|
+
* The section of the formatted price after and including the decimal.
|
|
121
|
+
*
|
|
122
|
+
* Note that this is not a number, but a locale-formatted string which may
|
|
123
|
+
* include symbols like spaces, commas, or periods as the decimal separator.
|
|
124
|
+
*/
|
|
125
|
+
fraction: string;
|
|
126
|
+
/**
|
|
127
|
+
* True if the formatted number has a non-0 decimal part.
|
|
128
|
+
*/
|
|
129
|
+
hasNonZeroFraction: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* The raw floating-point version of the number prepared for formatting,
|
|
132
|
+
* after unit conversion and precision scaling.
|
|
133
|
+
*
|
|
134
|
+
* For non-decimal currencies (eg: JPY) this will actually be an integer.
|
|
135
|
+
* Otherwise it will likely be a floating-point number. Be careful with this!
|
|
136
|
+
* It should not be used for math if possible because of floating-point
|
|
137
|
+
* rounding issues! Use the smallest unit instead.
|
|
138
|
+
*/
|
|
139
|
+
floatValue: number;
|
|
140
|
+
}
|
|
141
|
+
type FormatNumber = (number: number, options?: Omit<NumberFormatParams, 'browserSafeLocale'>) => string;
|
|
142
|
+
type FormatCurrency = (number: number, currency: string, options?: Omit<NumberFormatCurrencyParams, 'number' | 'currency' | 'browserSafeLocale' | 'geoLocation' | 'currencyOverrides'>) => string;
|
|
143
|
+
type GetCurrencyObject = (number: number, currency: string, options?: Omit<NumberFormatCurrencyParams, 'number' | 'currency' | 'browserSafeLocale' | 'geoLocation' | 'currencyOverrides'>) => CurrencyObject;
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/create-number-formatters.d.ts
|
|
146
|
+
declare global {
|
|
147
|
+
interface Window {
|
|
148
|
+
wp?: {
|
|
149
|
+
date?: {
|
|
150
|
+
getSettings?: () => {
|
|
151
|
+
l10n?: {
|
|
152
|
+
locale?: string;
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
interface NumberFormatters {
|
|
160
|
+
/**
|
|
161
|
+
* Sets the locale for number formatting
|
|
162
|
+
* @param locale - The locale to use for formatting
|
|
163
|
+
*/
|
|
164
|
+
setLocale(locale: string): void;
|
|
165
|
+
/**
|
|
166
|
+
* Sets the user's geo location for currency formatting if available
|
|
167
|
+
* @param geoLocation - The geo location to use for formatting
|
|
168
|
+
*/
|
|
169
|
+
setGeoLocation(geoLocation: string): void;
|
|
170
|
+
/**
|
|
171
|
+
* Sets a dynamic map of per-currency overrides used by currency formatting.
|
|
172
|
+
*
|
|
173
|
+
* Typical use: load `{ "IDR": { "decimal": 0 } }` from the WPCOM currencies
|
|
174
|
+
* endpoint at app boot and pass the parsed object here. Each entry can carry
|
|
175
|
+
* a `symbol` and/or `decimal` (the smallest-unit exponent), and additional
|
|
176
|
+
* fields may be added to `CurrencyOverride` in the future.
|
|
177
|
+
*
|
|
178
|
+
* If this setter is never called, the package falls back to the hard-coded
|
|
179
|
+
* defaults shipped with the package, preserving previous behavior.
|
|
180
|
+
*
|
|
181
|
+
* When called with a partial map, missing currencies or fields fall back to
|
|
182
|
+
* the hard-coded defaults — passing `{ IDR: { decimal: 0 } }` does not clear
|
|
183
|
+
* the default IDR symbol, for example.
|
|
184
|
+
* @param overrides - Map of currency code to override settings
|
|
185
|
+
*/
|
|
186
|
+
setCurrencyOverrides(overrides: Record<string, CurrencyOverride>): void;
|
|
187
|
+
/**
|
|
188
|
+
* Formats numbers using locale settings and/or passed options.
|
|
189
|
+
* @param number - The number to format.
|
|
190
|
+
* @param params - The parameters for the formatter.
|
|
191
|
+
* @param params.decimals - The number of decimal places to display.
|
|
192
|
+
* @param params.forceLatin - Whether to force the Latin script.
|
|
193
|
+
* @param params.numberFormatOptions - Additional options to pass to the formatter.
|
|
194
|
+
* @return {string} Formatted number as string, or original number as string if formatting fails.
|
|
195
|
+
*/
|
|
196
|
+
formatNumber: FormatNumber;
|
|
197
|
+
/**
|
|
198
|
+
* Formats numbers using locale settings and/or passed options, with a compact notation.
|
|
199
|
+
* Convenience method for formatting numbers in a compact notation e.g. 1K, 1M, etc.
|
|
200
|
+
* Basically sets `notation: 'compact'` and `maximumFractionDigits: 1` in the options.
|
|
201
|
+
* Everything is overridable by passing the `numberFormatOptions` option.
|
|
202
|
+
* If you want more digits, pass `maximumFractionDigits: 2`.
|
|
203
|
+
* @param number - The number to format.
|
|
204
|
+
* @param params - The parameters for the formatter.
|
|
205
|
+
* @param params.decimals - The number of decimal places to display.
|
|
206
|
+
* @param params.forceLatin - Whether to force the Latin script.
|
|
207
|
+
* @param params.numberFormatOptions - Additional options to pass to the formatter.
|
|
208
|
+
* @return {string} Formatted number as string, or original number as string if formatting fails.
|
|
209
|
+
*/
|
|
210
|
+
formatNumberCompact: FormatNumber;
|
|
211
|
+
/**
|
|
212
|
+
* Formats money with a given currency code.
|
|
213
|
+
*
|
|
214
|
+
* The currency will define the properties to use for this formatting, but
|
|
215
|
+
* those properties can be overridden using the options. Be careful when doing
|
|
216
|
+
* this.
|
|
217
|
+
*
|
|
218
|
+
* For currencies that include decimals, this will always return the amount
|
|
219
|
+
* with decimals included, even if those decimals are zeros. To exclude the
|
|
220
|
+
* zeros, use the `stripZeros` option. For example, the function will normally
|
|
221
|
+
* format `10.00` in `USD` as `$10.00` but when this option is true, it will
|
|
222
|
+
* return `$10` instead.
|
|
223
|
+
*
|
|
224
|
+
* Since rounding errors are common in floating point math, sometimes a price
|
|
225
|
+
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
226
|
+
* USD or yen in JPY). Set the `isSmallestUnit` to change the function to
|
|
227
|
+
* operate on integer numbers instead. If this option is not set or false, the
|
|
228
|
+
* function will format the amount `1025` in `USD` as `$1,025.00`, but when the
|
|
229
|
+
* option is true, it will return `$10.25` instead.
|
|
230
|
+
*
|
|
231
|
+
* If the number is NaN, it will be treated as 0.
|
|
232
|
+
*
|
|
233
|
+
* If the currency code is not known, this will assume a default currency
|
|
234
|
+
* similar to USD.
|
|
235
|
+
*
|
|
236
|
+
* If `isSmallestUnit` is set and the number is not an integer, it will be
|
|
237
|
+
* rounded to an integer.
|
|
238
|
+
* @param number - The number to format.
|
|
239
|
+
* @param currency - The currency to format.
|
|
240
|
+
* @param options - The options for the formatter.
|
|
241
|
+
* @param options.stripZeros - Whether to strip zeros.
|
|
242
|
+
* @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
|
|
243
|
+
* @param options.signForPositive - Whether to show the sign for positive numbers.
|
|
244
|
+
* @param options.forceLatin - Whether to force the latin locale.
|
|
245
|
+
* @return {string} A formatted string.
|
|
246
|
+
*/
|
|
247
|
+
formatCurrency: FormatCurrency;
|
|
248
|
+
/**
|
|
249
|
+
* Returns a formatted price object which can be used to manually render a
|
|
250
|
+
* formatted currency (eg: if you wanted to render the currency symbol in a
|
|
251
|
+
* different font size).
|
|
252
|
+
*
|
|
253
|
+
* The currency will define the properties to use for this formatting, but
|
|
254
|
+
* those properties can be overridden using the options. Be careful when doing
|
|
255
|
+
* this.
|
|
256
|
+
*
|
|
257
|
+
* For currencies that include decimals, this will always return the amount
|
|
258
|
+
* with decimals included, even if those decimals are zeros. To exclude the
|
|
259
|
+
* zeros, use the `stripZeros` option. For example, the function will normally
|
|
260
|
+
* format `10.00` in `USD` as `$10.00` but when this option is true, it will
|
|
261
|
+
* return `$10` instead.
|
|
262
|
+
*
|
|
263
|
+
* Since rounding errors are common in floating point math, sometimes a price
|
|
264
|
+
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
265
|
+
* USD or yen in JPY). Set the `isSmallestUnit` to change the function to
|
|
266
|
+
* operate on integer numbers instead. If this option is not set or false, the
|
|
267
|
+
* function will format the amount `1025` in `USD` as `$1,025.00`, but when the
|
|
268
|
+
* option is true, it will return `$10.25` instead.
|
|
269
|
+
*
|
|
270
|
+
* Note that the `integer` return value of this function is not a number, but a
|
|
271
|
+
* locale-formatted string which may include symbols like spaces, commas, or
|
|
272
|
+
* periods as group separators. Similarly, the `fraction` property is a string
|
|
273
|
+
* that contains the decimal separator.
|
|
274
|
+
*
|
|
275
|
+
* If the number is NaN, it will be treated as 0.
|
|
276
|
+
*
|
|
277
|
+
* If the currency code is not known, this will assume a default currency
|
|
278
|
+
* similar to USD.
|
|
279
|
+
*
|
|
280
|
+
* If `isSmallestUnit` is set and the number is not an integer, it will be
|
|
281
|
+
* rounded to an integer.
|
|
282
|
+
* @param number - The number to format.
|
|
283
|
+
* @param currency - The currency to format.
|
|
284
|
+
* @param options - The options for the formatter.
|
|
285
|
+
* @param options.stripZeros - Whether to strip zeros.
|
|
286
|
+
* @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
|
|
287
|
+
* @param options.signForPositive - Whether to show the sign for positive numbers.
|
|
288
|
+
* @param options.forceLatin - Whether to force the latin locale.
|
|
289
|
+
* @return {CurrencyObject} A formatted price object.
|
|
290
|
+
*/
|
|
291
|
+
getCurrencyObject: GetCurrencyObject;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates a NumberFormatters instance that provides number and currency formatting functionality with locale awareness
|
|
295
|
+
* @return {NumberFormatters} A NumberFormatters instance
|
|
296
|
+
*/
|
|
297
|
+
declare function createNumberFormatters(): NumberFormatters;
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/index.d.ts
|
|
300
|
+
declare const setLocale: (locale: string) => void, setGeoLocation: (geoLocation: string) => void, setCurrencyOverrides: (overrides: Record<string, CurrencyOverride>) => void, formatNumber: FormatNumber, formatNumberCompact: FormatNumber, formatCurrency: FormatCurrency, getCurrencyObject: GetCurrencyObject;
|
|
301
|
+
//#endregion
|
|
302
|
+
export { type CurrencyObject, type CurrencyOverride, type FormatCurrency, type FormatNumber, type GetCurrencyObject, type NumberFormatCurrencyParams, type NumberFormatParams, createNumberFormatters, formatCurrency, formatNumber, formatNumberCompact, getCurrencyObject, setCurrencyOverrides, setGeoLocation, setLocale };
|