@automattic/number-formatters 1.2.5 → 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.
@@ -1,371 +0,0 @@
1
- import debugFactory from 'debug';
2
- import { FALLBACK_CURRENCY } from "../constants.js";
3
- import { getCachedFormatter } from "../get-cached-formatter.js";
4
- import { defaultCurrencyOverrides } from "./currencies.js";
5
- const debug = debugFactory('number-formatters:number-format-currency');
6
- /**
7
- * Retrieves the currency override for a given currency.
8
- *
9
- * If the currency is USD and the user is not in the US, it will return `US$`.
10
- *
11
- * Per-field merge order is: dynamic overrides (from `currencyOverrides`) → hard-coded defaults.
12
- * This means a caller can supply a partial map (eg: only `decimal`) without losing the
13
- * default `symbol`.
14
- * @param currency - The currency to get the override for.
15
- * @param geoLocation - The geo location of the user.
16
- * @param currencyOverrides - Dynamic per-currency overrides supplied by the host application.
17
- * @return {CurrencyOverride | undefined} The currency override.
18
- */
19
- function getCurrencyOverride(currency, geoLocation, currencyOverrides) {
20
- if (currency === 'USD' && geoLocation && geoLocation !== '' && geoLocation !== 'US') {
21
- return { symbol: 'US$', ...currencyOverrides?.USD };
22
- }
23
- const defaultOverride = defaultCurrencyOverrides[currency];
24
- const dynamicOverride = currencyOverrides?.[currency];
25
- if (!defaultOverride && !dynamicOverride) {
26
- return undefined;
27
- }
28
- return { ...defaultOverride, ...dynamicOverride };
29
- }
30
- /**
31
- * Returns a valid currency code based on a shortlist of currency codes.
32
- * Only currencies from the shortlist are allowed. Everything else will fall back to `FALLBACK_CURRENCY`.
33
- * @param currency - The currency to get the valid currency for.
34
- * @param geoLocation - The geo location of the user.
35
- * @param currencyOverrides - Dynamic per-currency overrides supplied by the host application.
36
- * @return {string} The valid currency.
37
- */
38
- function getValidCurrency(currency, geoLocation, currencyOverrides) {
39
- if (!getCurrencyOverride(currency, geoLocation, currencyOverrides)) {
40
- debug(`getValidCurrency was called with a non-existent currency "${currency}"; falling back to ${FALLBACK_CURRENCY}`);
41
- return FALLBACK_CURRENCY;
42
- }
43
- return currency;
44
- }
45
- /**
46
- * Returns a currency formatter for a given currency.
47
- * @param params - The parameters for the currency formatter.
48
- * @param params.number - The number to format.
49
- * @param params.currency - The currency to format.
50
- * @param params.browserSafeLocale - The browser safe locale.
51
- * @param params.forceLatin - Whether to force the latin locale.
52
- * @param params.stripZeros - Whether to strip zeros.
53
- * @param params.signForPositive - Whether to show the sign for positive numbers.
54
- * @return {Intl.NumberFormat} The currency formatter.
55
- */
56
- function getCurrencyFormatter({ number, currency, browserSafeLocale, forceLatin = true, stripZeros, signForPositive, }) {
57
- /**
58
- * `numberingSystem` is an option to `Intl.NumberFormat` and is available
59
- * in all major browsers according to
60
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
61
- * but is not part of the TypeScript types in `es2020`:
62
- *
63
- * https://github.com/microsoft/TypeScript/blob/cfd472f7aa5a2010a3115263bf457b30c5b489f3/src/lib/es2020.intl.d.ts#L272
64
- *
65
- * However, it is part of the TypeScript types in `es5`:
66
- *
67
- * https://github.com/microsoft/TypeScript/blob/cfd472f7aa5a2010a3115263bf457b30c5b489f3/src/lib/es5.d.ts#L4310
68
- *
69
- * Apparently calypso uses `es2020` so we cannot use that option here right
70
- * now. Instead, we will use the unicode extension to the locale, documented
71
- * here:
72
- *
73
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numberingSystem#adding_a_numbering_system_via_the_locale_string
74
- */
75
- const locale = `${browserSafeLocale}${forceLatin ? '-u-nu-latn' : ''}`;
76
- const numberFormatOptions = {
77
- style: 'currency',
78
- currency,
79
- ...(stripZeros &&
80
- Number.isInteger(number) && {
81
- /**
82
- * There's an option called `trailingZeroDisplay` but it does not yet work
83
- * in FF so we have to strip zeros manually.
84
- */
85
- maximumFractionDigits: 0,
86
- minimumFractionDigits: 0,
87
- }),
88
- ...(signForPositive && { signDisplay: 'exceptZero' }),
89
- };
90
- return getCachedFormatter({
91
- locale,
92
- options: numberFormatOptions,
93
- });
94
- }
95
- /**
96
- * Hard-coded smallest-unit exponent overrides for currencies where browser ICU's
97
- * `maximumFractionDigits` disagrees with the API's smallest-unit encoding.
98
- *
99
- * This list exists as a safety net for callers that have not yet wired up the
100
- * dynamic `currencyOverrides` path (eg: the WPCOM currencies endpoint). Once a
101
- * host application provides overrides via `setCurrencyOverrides`, those take
102
- * precedence on a per-currency basis.
103
- *
104
- * Keep this list minimal — the backend is the source of truth for the API's
105
- * smallest-unit encoding, so adding speculative entries here risks silent
106
- * drift. Only add a currency once we've verified that browsers report a
107
- * value the API does not use.
108
- *
109
- * - IDR: modern Chrome / Node 24+ ICU reports 0; the API encodes with exponent 2.
110
- * - HUF: same browser/API divergence as IDR.
111
- */
112
- const SMALLEST_UNIT_EXPONENT_OVERRIDES = {
113
- IDR: 2,
114
- HUF: 2,
115
- };
116
- /**
117
- * Returns the smallest unit exponent for a currency.
118
- *
119
- * Lookup order:
120
- * 1. The dynamic `currencyOverrides[currency].decimal` if a host application has supplied one (typically via `setCurrencyOverrides`).
121
- * 2. The hard-coded `SMALLEST_UNIT_EXPONENT_OVERRIDES` map.
122
- * 3. The browser-derived display precision (`fallback`).
123
- * @param currency - The currency code (ISO 4217)
124
- * @param fallback - The browser-derived precision to use when no override applies
125
- * @param currencyOverrides - Dynamic per-currency overrides supplied by the host application
126
- * @return number - The smallest unit exponent
127
- */
128
- function getSmallestUnitExponent(currency, fallback, currencyOverrides) {
129
- const dynamicDecimal = currencyOverrides?.[currency]?.decimal;
130
- if (typeof dynamicDecimal === 'number') {
131
- return dynamicDecimal;
132
- }
133
- return SMALLEST_UNIT_EXPONENT_OVERRIDES[currency] ?? fallback;
134
- }
135
- /**
136
- * Returns the precision for a given locale and currency.
137
- * @param browserSafeLocale - The browser safe locale.
138
- * @param currency - The currency to get the precision for.
139
- * @param forceLatin - Whether to force the latin locale.
140
- * @return {number | undefined} The precision.
141
- */
142
- function getPrecisionForLocaleAndCurrency(browserSafeLocale, currency, forceLatin) {
143
- const formatter = getCurrencyFormatter({ number: 0, currency, browserSafeLocale, forceLatin });
144
- /**
145
- * For regular numbers, the default is 3 if neither `minimumFractionDigits` or `maximumFractionDigits` are set,
146
- * otherwise the greatest betweem `minimumFractionDigits` and 3.
147
- *
148
- * For currencies, the default is dependent on the currency.
149
- *
150
- * This may also result in undefined, for several reasons:
151
- * see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#significantdigitsfractiondigits_default_values
152
- */
153
- return formatter.resolvedOptions().maximumFractionDigits;
154
- }
155
- /**
156
- * Scales a number to a specified precision and rounds it to that precision.
157
- * It ensures that all currency values are consistently rounded to the desired precision,
158
- * avoiding issues with floating-point arithmetic.
159
- * @param number - The number to scale.
160
- * @param currencyPrecision - The precision to scale the number to.
161
- * @return {number} The scaled number.
162
- */
163
- function scaleNumberForPrecision(number, currencyPrecision) {
164
- const scale = Math.pow(10, currencyPrecision);
165
- return Math.round(number * scale) / scale;
166
- }
167
- /**
168
- * Prepares a number for formatting.
169
- * @param number - The number to prepare.
170
- * @param currencyPrecision - The display precision (from the browser) to round the result to.
171
- * @param currency - The currency code, used to look up any smallest-unit exponent override.
172
- * @param isSmallestUnit - Whether the number is the smallest unit of a currency.
173
- * @param currencyOverrides - Dynamic per-currency overrides supplied by the host application.
174
- * @return {number} The prepared number.
175
- */
176
- function prepareNumberForFormatting(number, currencyPrecision, currency, isSmallestUnit, currencyOverrides) {
177
- if (isNaN(number)) {
178
- debug('formatCurrency was called with NaN');
179
- return 0;
180
- }
181
- if (isSmallestUnit) {
182
- if (!Number.isInteger(number)) {
183
- debug('formatCurrency was called with isSmallestUnit and a float which will be rounded', number);
184
- }
185
- const smallestUnitDivisor = 10 ** getSmallestUnitExponent(currency, currencyPrecision, currencyOverrides);
186
- return scaleNumberForPrecision(Math.round(number) / smallestUnitDivisor, currencyPrecision);
187
- }
188
- return scaleNumberForPrecision(number, currencyPrecision);
189
- }
190
- /**
191
- * Formats money with a given currency code.
192
- *
193
- * The currency will define the properties to use for this formatting, but
194
- * those properties can be overridden using the options. Be careful when doing
195
- * this.
196
- *
197
- * For currencies that include decimals, this will always return the amount
198
- * with decimals included, even if those decimals are zeros. To exclude the
199
- * zeros, use the `stripZeros` option. For example, the function will normally
200
- * format `10.00` in `USD` as `$10.00` but when this option is true, it will
201
- * return `$10` instead.
202
- *
203
- * Since rounding errors are common in floating point math, sometimes a price
204
- * is provided as an integer in the smallest unit of a currency (eg: cents in
205
- * USD or yen in JPY). Set the `isSmallestUnit` to change the function to
206
- * operate on integer numbers instead. If this option is not set or false, the
207
- * function will format the amount `1025` in `USD` as `$1,025.00`, but when the
208
- * option is true, it will return `$10.25` instead.
209
- *
210
- * If the number is NaN, it will be treated as 0.
211
- *
212
- * If the currency code is not known, this will assume a default currency
213
- * similar to USD.
214
- *
215
- * If `isSmallestUnit` is set and the number is not an integer, it will be
216
- * rounded to an integer.
217
- *
218
- * @param params - The parameters for the currency formatter.
219
- * @param params.number - The number to format.
220
- * @param params.browserSafeLocale - The browser safe locale.
221
- * @param params.currency - The currency to format.
222
- * @param params.stripZeros - Whether to strip zeros.
223
- * @param params.isSmallestUnit - Whether the number is the smallest unit of a currency.
224
- * @param params.signForPositive - Whether to show the sign for positive numbers.
225
- * @param params.geoLocation - The geo location of the user.
226
- * @param params.forceLatin - Whether to force the latin locale.
227
- * @param params.currencyOverrides - Dynamic per-currency overrides supplied by the host application.
228
- * @return {string} A formatted string.
229
- */
230
- const numberFormatCurrency = ({ number, browserSafeLocale, currency, stripZeros, isSmallestUnit, signForPositive, geoLocation, forceLatin, currencyOverrides, }) => {
231
- const validCurrency = getValidCurrency(currency, geoLocation, currencyOverrides);
232
- const currencyOverride = getCurrencyOverride(validCurrency, geoLocation, currencyOverrides);
233
- const currencyPrecision = getPrecisionForLocaleAndCurrency(browserSafeLocale, validCurrency, forceLatin);
234
- if (isSmallestUnit && typeof currencyPrecision === 'undefined') {
235
- throw new Error(`Could not determine currency precision for ${validCurrency} in ${browserSafeLocale}`);
236
- }
237
- const numberAsFloat = prepareNumberForFormatting(number, currencyPrecision ?? 0, validCurrency, isSmallestUnit, currencyOverrides);
238
- const formatter = getCurrencyFormatter({
239
- number: numberAsFloat,
240
- currency: validCurrency,
241
- browserSafeLocale,
242
- forceLatin,
243
- stripZeros,
244
- signForPositive,
245
- });
246
- const parts = formatter.formatToParts(numberAsFloat);
247
- return parts.reduce((formatted, part) => {
248
- switch (part.type) {
249
- case 'currency':
250
- if (currencyOverride?.symbol) {
251
- return formatted + currencyOverride.symbol;
252
- }
253
- return formatted + part.value;
254
- default:
255
- return formatted + part.value;
256
- }
257
- }, '');
258
- };
259
- /**
260
- * Returns a formatted price object which can be used to manually render a
261
- * formatted currency (eg: if you wanted to render the currency symbol in a
262
- * different font size).
263
- *
264
- * The currency will define the properties to use for this formatting, but
265
- * those properties can be overridden using the options. Be careful when doing
266
- * this.
267
- *
268
- * For currencies that include decimals, this will always return the amount
269
- * with decimals included, even if those decimals are zeros. To exclude the
270
- * zeros, use the `stripZeros` option. For example, the function will normally
271
- * format `10.00` in `USD` as `$10.00` but when this option is true, it will
272
- * return `$10` instead.
273
- *
274
- * Since rounding errors are common in floating point math, sometimes a price
275
- * is provided as an integer in the smallest unit of a currency (eg: cents in
276
- * USD or yen in JPY). Set the `isSmallestUnit` to change the function to
277
- * operate on integer numbers instead. If this option is not set or false, the
278
- * function will format the amount `1025` in `USD` as `$1,025.00`, but when the
279
- * option is true, it will return `$10.25` instead.
280
- *
281
- * Note that the `integer` return value of this function is not a number, but a
282
- * locale-formatted string which may include symbols like spaces, commas, or
283
- * periods as group separators. Similarly, the `fraction` property is a string
284
- * that contains the decimal separator.
285
- *
286
- * If the number is NaN, it will be treated as 0.
287
- *
288
- * If the currency code is not known, this will assume a default currency
289
- * similar to USD.
290
- *
291
- * If `isSmallestUnit` is set and the number is not an integer, it will be
292
- * rounded to an integer.
293
- *
294
- * @param params - The parameters for the currency formatter.
295
- * @param params.number - The number to format.
296
- * @param params.browserSafeLocale - The browser safe locale.
297
- * @param params.currency - The currency to format.
298
- * @param params.stripZeros - Whether to strip zeros.
299
- * @param params.isSmallestUnit - Whether the number is the smallest unit of a currency.
300
- * @param params.signForPositive - Whether to show the sign for positive numbers.
301
- * @param params.geoLocation - The geo location of the user.
302
- * @param params.forceLatin - Whether to force the latin locale.
303
- * @param params.currencyOverrides - Dynamic per-currency overrides supplied by the host application.
304
- * @return {CurrencyObject} A formatted string e.g. { symbol:'$', integer: '$99', fraction: '.99', sign: '-' }
305
- */
306
- const getCurrencyObject = ({ number, browserSafeLocale, currency, stripZeros, isSmallestUnit, signForPositive, geoLocation, forceLatin, currencyOverrides, }) => {
307
- const validCurrency = getValidCurrency(currency, geoLocation, currencyOverrides);
308
- const currencyOverride = getCurrencyOverride(validCurrency, geoLocation, currencyOverrides);
309
- const currencyPrecision = getPrecisionForLocaleAndCurrency(browserSafeLocale, validCurrency, forceLatin);
310
- const numberAsFloat = prepareNumberForFormatting(number, currencyPrecision ?? 0, validCurrency, isSmallestUnit, currencyOverrides);
311
- const formatter = getCurrencyFormatter({
312
- number: numberAsFloat,
313
- currency: validCurrency,
314
- browserSafeLocale,
315
- forceLatin,
316
- stripZeros,
317
- signForPositive,
318
- });
319
- const parts = formatter.formatToParts(numberAsFloat);
320
- let sign = '';
321
- let symbol = '$';
322
- let symbolPosition = 'before';
323
- let hasAmountBeenSet = false;
324
- let hasDecimalBeenSet = false;
325
- let integer = '';
326
- let fraction = '';
327
- parts.forEach(part => {
328
- switch (part.type) {
329
- case 'currency':
330
- symbol = currencyOverride?.symbol ?? part.value;
331
- if (hasAmountBeenSet) {
332
- symbolPosition = 'after';
333
- }
334
- return;
335
- case 'group':
336
- integer += part.value;
337
- hasAmountBeenSet = true;
338
- return;
339
- case 'decimal':
340
- fraction += part.value;
341
- hasAmountBeenSet = true;
342
- hasDecimalBeenSet = true;
343
- return;
344
- case 'integer':
345
- integer += part.value;
346
- hasAmountBeenSet = true;
347
- return;
348
- case 'fraction':
349
- fraction += part.value;
350
- hasAmountBeenSet = true;
351
- hasDecimalBeenSet = true;
352
- return;
353
- case 'minusSign':
354
- sign = '-';
355
- return;
356
- case 'plusSign':
357
- sign = '+';
358
- }
359
- });
360
- const hasNonZeroFraction = !Number.isInteger(numberAsFloat) && hasDecimalBeenSet;
361
- return {
362
- sign,
363
- symbol,
364
- symbolPosition,
365
- integer,
366
- fraction,
367
- hasNonZeroFraction,
368
- floatValue: numberAsFloat,
369
- };
370
- };
371
- export { numberFormatCurrency, getCurrencyObject };
@@ -1,55 +0,0 @@
1
- import { getCachedFormatter } from "./get-cached-formatter.js";
2
- /**
3
- * Formats numbers using locale settings and/or passed options.
4
- * @param params - The parameters for the number formatter.
5
- * @param params.browserSafeLocale - The browser safe locale.
6
- * @param params.decimals - The number of decimal places to use.
7
- * @param params.forceLatin - Whether to force the latin locale.
8
- * @param params.numberFormatOptions - The options for the number formatter.
9
- * @return {Intl.NumberFormat} The number formatter.
10
- */
11
- const numberFormat = ({ browserSafeLocale, decimals = 0, forceLatin = true, numberFormatOptions = {}, }) => {
12
- /**
13
- * `numberingSystem` is an option to `Intl.NumberFormat` and is available
14
- * in all major browsers according to
15
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
16
- * but is not part of the TypeScript types in `es2020`:
17
- *
18
- * https://github.com/microsoft/TypeScript/blob/cfd472f7aa5a2010a3115263bf457b30c5b489f3/src/lib/es2020.intl.d.ts#L272
19
- *
20
- * However, it is part of the TypeScript types in `es5`:
21
- *
22
- * https://github.com/microsoft/TypeScript/blob/cfd472f7aa5a2010a3115263bf457b30c5b489f3/src/lib/es5.d.ts#L4310
23
- *
24
- * Apparently calypso uses `es2020` so we cannot use that option here right
25
- * now. Instead, we will use the unicode extension to the locale, documented
26
- * here:
27
- *
28
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numberingSystem#adding_a_numbering_system_via_the_locale_string
29
- */
30
- const locale = `${browserSafeLocale}${forceLatin ? '-u-nu-latn' : ''}`;
31
- const options = {
32
- minimumFractionDigits: decimals, // minimumFractionDigits default is 0
33
- maximumFractionDigits: decimals, // maximumFractionDigits default is the greater between minimumFractionDigits and 3
34
- ...numberFormatOptions,
35
- };
36
- return getCachedFormatter({ locale, options });
37
- };
38
- /**
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 params - The parameters for the number formatter.
44
- * @param params.numberFormatOptions - The options for the number formatter.
45
- * @return {Intl.NumberFormat} The number formatter.
46
- */
47
- const numberFormatCompact = ({ numberFormatOptions = {}, ...params }) => numberFormat({
48
- ...params,
49
- numberFormatOptions: {
50
- notation: 'compact',
51
- maximumFractionDigits: 1,
52
- ...numberFormatOptions,
53
- },
54
- });
55
- export { numberFormat, numberFormatCompact };
package/dist/esm/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- export declare const FALLBACK_LOCALE = "en";
2
- export declare const FALLBACK_CURRENCY = "USD";
3
- export * from '@wordpress/date';
@@ -1,154 +0,0 @@
1
- import type { CurrencyOverride, FormatCurrency, FormatNumber, GetCurrencyObject } from './types.ts';
2
- declare global {
3
- interface Window {
4
- wp?: {
5
- date?: {
6
- getSettings?: () => {
7
- l10n?: {
8
- locale?: string;
9
- };
10
- };
11
- };
12
- };
13
- }
14
- }
15
- export interface NumberFormatters {
16
- /**
17
- * Sets the locale for number formatting
18
- * @param locale - The locale to use for formatting
19
- */
20
- setLocale(locale: string): void;
21
- /**
22
- * Sets the user's geo location for currency formatting if available
23
- * @param geoLocation - The geo location to use for formatting
24
- */
25
- setGeoLocation(geoLocation: string): void;
26
- /**
27
- * Sets a dynamic map of per-currency overrides used by currency formatting.
28
- *
29
- * Typical use: load `{ "IDR": { "decimal": 0 } }` from the WPCOM currencies
30
- * endpoint at app boot and pass the parsed object here. Each entry can carry
31
- * a `symbol` and/or `decimal` (the smallest-unit exponent), and additional
32
- * fields may be added to `CurrencyOverride` in the future.
33
- *
34
- * If this setter is never called, the package falls back to the hard-coded
35
- * defaults shipped with the package, preserving previous behavior.
36
- *
37
- * When called with a partial map, missing currencies or fields fall back to
38
- * the hard-coded defaults — passing `{ IDR: { decimal: 0 } }` does not clear
39
- * the default IDR symbol, for example.
40
- * @param overrides - Map of currency code to override settings
41
- */
42
- setCurrencyOverrides(overrides: Record<string, CurrencyOverride>): void;
43
- /**
44
- * Formats numbers using locale settings and/or passed options.
45
- * @param number - The number to format.
46
- * @param params - The parameters for the formatter.
47
- * @param params.decimals - The number of decimal places to display.
48
- * @param params.forceLatin - Whether to force the Latin script.
49
- * @param params.numberFormatOptions - Additional options to pass to the formatter.
50
- * @return {string} Formatted number as string, or original number as string if formatting fails.
51
- */
52
- formatNumber: FormatNumber;
53
- /**
54
- * Formats numbers using locale settings and/or passed options, with a compact notation.
55
- * Convenience method for formatting numbers in a compact notation e.g. 1K, 1M, etc.
56
- * Basically sets `notation: 'compact'` and `maximumFractionDigits: 1` in the options.
57
- * Everything is overridable by passing the `numberFormatOptions` option.
58
- * If you want more digits, pass `maximumFractionDigits: 2`.
59
- * @param number - The number to format.
60
- * @param params - The parameters for the formatter.
61
- * @param params.decimals - The number of decimal places to display.
62
- * @param params.forceLatin - Whether to force the Latin script.
63
- * @param params.numberFormatOptions - Additional options to pass to the formatter.
64
- * @return {string} Formatted number as string, or original number as string if formatting fails.
65
- */
66
- formatNumberCompact: FormatNumber;
67
- /**
68
- * Formats money with a given currency code.
69
- *
70
- * The currency will define the properties to use for this formatting, but
71
- * those properties can be overridden using the options. Be careful when doing
72
- * this.
73
- *
74
- * For currencies that include decimals, this will always return the amount
75
- * with decimals included, even if those decimals are zeros. To exclude the
76
- * zeros, use the `stripZeros` option. For example, the function will normally
77
- * format `10.00` in `USD` as `$10.00` but when this option is true, it will
78
- * return `$10` instead.
79
- *
80
- * Since rounding errors are common in floating point math, sometimes a price
81
- * is provided as an integer in the smallest unit of a currency (eg: cents in
82
- * USD or yen in JPY). Set the `isSmallestUnit` to change the function to
83
- * operate on integer numbers instead. If this option is not set or false, the
84
- * function will format the amount `1025` in `USD` as `$1,025.00`, but when the
85
- * option is true, it will return `$10.25` instead.
86
- *
87
- * If the number is NaN, it will be treated as 0.
88
- *
89
- * If the currency code is not known, this will assume a default currency
90
- * similar to USD.
91
- *
92
- * If `isSmallestUnit` is set and the number is not an integer, it will be
93
- * rounded to an integer.
94
- * @param number - The number to format.
95
- * @param currency - The currency to format.
96
- * @param options - The options for the formatter.
97
- * @param options.stripZeros - Whether to strip zeros.
98
- * @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
99
- * @param options.signForPositive - Whether to show the sign for positive numbers.
100
- * @param options.forceLatin - Whether to force the latin locale.
101
- * @return {string} A formatted string.
102
- */
103
- formatCurrency: FormatCurrency;
104
- /**
105
- * Returns a formatted price object which can be used to manually render a
106
- * formatted currency (eg: if you wanted to render the currency symbol in a
107
- * different font size).
108
- *
109
- * The currency will define the properties to use for this formatting, but
110
- * those properties can be overridden using the options. Be careful when doing
111
- * this.
112
- *
113
- * For currencies that include decimals, this will always return the amount
114
- * with decimals included, even if those decimals are zeros. To exclude the
115
- * zeros, use the `stripZeros` option. For example, the function will normally
116
- * format `10.00` in `USD` as `$10.00` but when this option is true, it will
117
- * return `$10` instead.
118
- *
119
- * Since rounding errors are common in floating point math, sometimes a price
120
- * is provided as an integer in the smallest unit of a currency (eg: cents in
121
- * USD or yen in JPY). Set the `isSmallestUnit` to change the function to
122
- * operate on integer numbers instead. If this option is not set or false, the
123
- * function will format the amount `1025` in `USD` as `$1,025.00`, but when the
124
- * option is true, it will return `$10.25` instead.
125
- *
126
- * Note that the `integer` return value of this function is not a number, but a
127
- * locale-formatted string which may include symbols like spaces, commas, or
128
- * periods as group separators. Similarly, the `fraction` property is a string
129
- * that contains the decimal separator.
130
- *
131
- * If the number is NaN, it will be treated as 0.
132
- *
133
- * If the currency code is not known, this will assume a default currency
134
- * similar to USD.
135
- *
136
- * If `isSmallestUnit` is set and the number is not an integer, it will be
137
- * rounded to an integer.
138
- * @param number - The number to format.
139
- * @param currency - The currency to format.
140
- * @param options - The options for the formatter.
141
- * @param options.stripZeros - Whether to strip zeros.
142
- * @param options.isSmallestUnit - Whether the number is the smallest unit of a currency.
143
- * @param options.signForPositive - Whether to show the sign for positive numbers.
144
- * @param options.forceLatin - Whether to force the latin locale.
145
- * @return {CurrencyObject} A formatted price object.
146
- */
147
- getCurrencyObject: GetCurrencyObject;
148
- }
149
- /**
150
- * Creates a NumberFormatters instance that provides number and currency formatting functionality with locale awareness
151
- * @return {NumberFormatters} A NumberFormatters instance
152
- */
153
- declare function createNumberFormatters(): NumberFormatters;
154
- export default createNumberFormatters;
@@ -1,17 +0,0 @@
1
- interface Params {
2
- locale: string;
3
- options?: Intl.NumberFormatOptions;
4
- fallbackLocale?: string;
5
- retries?: number;
6
- }
7
- /**
8
- * Get a cached formatter for a given locale and options.
9
- * @param params - The parameters for the formatter.
10
- * @param params.locale - The locale to format the number in.
11
- * @param params.options - Intl.NumberFormatOptions to pass to the formatter.
12
- * @param params.fallbackLocale - The locale to fallback to if the locale is not supported.
13
- * @param params.retries - The number of retries to attempt if the formatter is not created.
14
- * @return {Intl.NumberFormat} A cached formatter for the given locale and options.
15
- */
16
- export declare function getCachedFormatter({ locale, fallbackLocale, options, retries, }: Params): Intl.NumberFormat;
17
- export {};
@@ -1,4 +0,0 @@
1
- import createNumberFormatters from './create-number-formatters.ts';
2
- export declare const setLocale: (locale: string) => void, setGeoLocation: (geoLocation: string) => void, setCurrencyOverrides: (overrides: Record<string, import("./types.ts").CurrencyOverride>) => void, formatNumber: import("./types.ts").FormatNumber, formatNumberCompact: import("./types.ts").FormatNumber, formatCurrency: import("./types.ts").FormatCurrency, getCurrencyObject: import("./types.ts").GetCurrencyObject;
3
- export { createNumberFormatters };
4
- export type * from './types.ts';
@@ -1,2 +0,0 @@
1
- import type { CurrencyOverride } from '../types.ts';
2
- export declare const defaultCurrencyOverrides: Record<string, CurrencyOverride>;