@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.
- package/CHANGELOG.md +5 -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 -11
- 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
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { CurrencyObject, NumberFormatCurrencyParams } from '../types.ts';
|
|
2
|
-
/**
|
|
3
|
-
* Formats money with a given currency code.
|
|
4
|
-
*
|
|
5
|
-
* The currency will define the properties to use for this formatting, but
|
|
6
|
-
* those properties can be overridden using the options. Be careful when doing
|
|
7
|
-
* this.
|
|
8
|
-
*
|
|
9
|
-
* For currencies that include decimals, this will always return the amount
|
|
10
|
-
* with decimals included, even if those decimals are zeros. To exclude the
|
|
11
|
-
* zeros, use the `stripZeros` option. For example, the function will normally
|
|
12
|
-
* format `10.00` in `USD` as `$10.00` but when this option is true, it will
|
|
13
|
-
* return `$10` instead.
|
|
14
|
-
*
|
|
15
|
-
* Since rounding errors are common in floating point math, sometimes a price
|
|
16
|
-
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
17
|
-
* USD or yen in JPY). Set the `isSmallestUnit` to change the function to
|
|
18
|
-
* operate on integer numbers instead. If this option is not set or false, the
|
|
19
|
-
* function will format the amount `1025` in `USD` as `$1,025.00`, but when the
|
|
20
|
-
* option is true, it will return `$10.25` instead.
|
|
21
|
-
*
|
|
22
|
-
* If the number is NaN, it will be treated as 0.
|
|
23
|
-
*
|
|
24
|
-
* If the currency code is not known, this will assume a default currency
|
|
25
|
-
* similar to USD.
|
|
26
|
-
*
|
|
27
|
-
* If `isSmallestUnit` is set and the number is not an integer, it will be
|
|
28
|
-
* rounded to an integer.
|
|
29
|
-
*
|
|
30
|
-
* @param params - The parameters for the currency formatter.
|
|
31
|
-
* @param params.number - The number to format.
|
|
32
|
-
* @param params.browserSafeLocale - The browser safe locale.
|
|
33
|
-
* @param params.currency - The currency to format.
|
|
34
|
-
* @param params.stripZeros - Whether to strip zeros.
|
|
35
|
-
* @param params.isSmallestUnit - Whether the number is the smallest unit of a currency.
|
|
36
|
-
* @param params.signForPositive - Whether to show the sign for positive numbers.
|
|
37
|
-
* @param params.geoLocation - The geo location of the user.
|
|
38
|
-
* @param params.forceLatin - Whether to force the latin locale.
|
|
39
|
-
* @param params.currencyOverrides - Dynamic per-currency overrides supplied by the host application.
|
|
40
|
-
* @return {string} A formatted string.
|
|
41
|
-
*/
|
|
42
|
-
declare const numberFormatCurrency: ({ number, browserSafeLocale, currency, stripZeros, isSmallestUnit, signForPositive, geoLocation, forceLatin, currencyOverrides, }: NumberFormatCurrencyParams) => string;
|
|
43
|
-
/**
|
|
44
|
-
* Returns a formatted price object which can be used to manually render a
|
|
45
|
-
* formatted currency (eg: if you wanted to render the currency symbol in a
|
|
46
|
-
* different font size).
|
|
47
|
-
*
|
|
48
|
-
* The currency will define the properties to use for this formatting, but
|
|
49
|
-
* those properties can be overridden using the options. Be careful when doing
|
|
50
|
-
* this.
|
|
51
|
-
*
|
|
52
|
-
* For currencies that include decimals, this will always return the amount
|
|
53
|
-
* with decimals included, even if those decimals are zeros. To exclude the
|
|
54
|
-
* zeros, use the `stripZeros` option. For example, the function will normally
|
|
55
|
-
* format `10.00` in `USD` as `$10.00` but when this option is true, it will
|
|
56
|
-
* return `$10` instead.
|
|
57
|
-
*
|
|
58
|
-
* Since rounding errors are common in floating point math, sometimes a price
|
|
59
|
-
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
60
|
-
* USD or yen in JPY). Set the `isSmallestUnit` to change the function to
|
|
61
|
-
* operate on integer numbers instead. If this option is not set or false, the
|
|
62
|
-
* function will format the amount `1025` in `USD` as `$1,025.00`, but when the
|
|
63
|
-
* option is true, it will return `$10.25` instead.
|
|
64
|
-
*
|
|
65
|
-
* Note that the `integer` return value of this function is not a number, but a
|
|
66
|
-
* locale-formatted string which may include symbols like spaces, commas, or
|
|
67
|
-
* periods as group separators. Similarly, the `fraction` property is a string
|
|
68
|
-
* that contains the decimal separator.
|
|
69
|
-
*
|
|
70
|
-
* If the number is NaN, it will be treated as 0.
|
|
71
|
-
*
|
|
72
|
-
* If the currency code is not known, this will assume a default currency
|
|
73
|
-
* similar to USD.
|
|
74
|
-
*
|
|
75
|
-
* If `isSmallestUnit` is set and the number is not an integer, it will be
|
|
76
|
-
* rounded to an integer.
|
|
77
|
-
*
|
|
78
|
-
* @param params - The parameters for the currency formatter.
|
|
79
|
-
* @param params.number - The number to format.
|
|
80
|
-
* @param params.browserSafeLocale - The browser safe locale.
|
|
81
|
-
* @param params.currency - The currency to format.
|
|
82
|
-
* @param params.stripZeros - Whether to strip zeros.
|
|
83
|
-
* @param params.isSmallestUnit - Whether the number is the smallest unit of a currency.
|
|
84
|
-
* @param params.signForPositive - Whether to show the sign for positive numbers.
|
|
85
|
-
* @param params.geoLocation - The geo location of the user.
|
|
86
|
-
* @param params.forceLatin - Whether to force the latin locale.
|
|
87
|
-
* @param params.currencyOverrides - Dynamic per-currency overrides supplied by the host application.
|
|
88
|
-
* @return {CurrencyObject} A formatted string e.g. { symbol:'$', integer: '$99', fraction: '.99', sign: '-' }
|
|
89
|
-
*/
|
|
90
|
-
declare const getCurrencyObject: ({ number, browserSafeLocale, currency, stripZeros, isSmallestUnit, signForPositive, geoLocation, forceLatin, currencyOverrides, }: NumberFormatCurrencyParams) => CurrencyObject;
|
|
91
|
-
export { numberFormatCurrency, getCurrencyObject };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { NumberFormatParams } from './types.ts';
|
|
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
|
-
declare const numberFormat: ({ browserSafeLocale, decimals, forceLatin, numberFormatOptions, }: NumberFormatParams) => Intl.NumberFormat;
|
|
12
|
-
/**
|
|
13
|
-
* Convenience method for formatting numbers in a compact notation e.g. 1K, 1M, etc.
|
|
14
|
-
* Basically sets `notation: 'compact'` and `maximumFractionDigits: 1` in the options.
|
|
15
|
-
* Everything is overridable by passing the `numberFormatOptions` option.
|
|
16
|
-
* If you want more digits, pass `maximumFractionDigits: 2`.
|
|
17
|
-
* @param params - The parameters for the number formatter.
|
|
18
|
-
* @param params.numberFormatOptions - The options for the number formatter.
|
|
19
|
-
* @return {Intl.NumberFormat} The number formatter.
|
|
20
|
-
*/
|
|
21
|
-
declare const numberFormatCompact: typeof numberFormat;
|
|
22
|
-
export { numberFormat, numberFormatCompact };
|
package/dist/types/types.d.ts
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
export interface NumberFormatParams {
|
|
2
|
-
/**
|
|
3
|
-
* Browser-safe locale string that works with Intl.NumberFormat e.g. 'en-US' (not 'en_US').
|
|
4
|
-
*/
|
|
5
|
-
browserSafeLocale: string;
|
|
6
|
-
/**
|
|
7
|
-
* Number of decimal places to use.
|
|
8
|
-
* This is just convenience over setting `minimumFractionDigits`, `maximumFractionDigits` to the same value.
|
|
9
|
-
* ( default = 0 )
|
|
10
|
-
*/
|
|
11
|
-
decimals?: number;
|
|
12
|
-
/**
|
|
13
|
-
* Whether to use latin numbers by default ( default = true ).
|
|
14
|
-
*/
|
|
15
|
-
forceLatin?: boolean;
|
|
16
|
-
/**
|
|
17
|
-
* `Intl.NumberFormat` options to pass through.
|
|
18
|
-
* `minimumFractionDigits` & `maximumFractionDigits` will override `decimals` if set.
|
|
19
|
-
*/
|
|
20
|
-
numberFormatOptions?: Intl.NumberFormatOptions;
|
|
21
|
-
}
|
|
22
|
-
export interface CurrencyOverride {
|
|
23
|
-
symbol?: string;
|
|
24
|
-
/**
|
|
25
|
-
* Smallest-unit exponent for this currency, used when the browser's ICU
|
|
26
|
-
* `maximumFractionDigits` disagrees with the API's smallest-unit encoding,
|
|
27
|
-
* or when this package's hard-coded fallback exponent (see
|
|
28
|
-
* `SMALLEST_UNIT_EXPONENT_OVERRIDES` in `number-format-currency/index.ts`)
|
|
29
|
-
* disagrees with the host application's source of truth.
|
|
30
|
-
*
|
|
31
|
-
* For example, modern browser ICU (Chrome / Node 24+) reports IDR as
|
|
32
|
-
* 0-decimal, but this package's hard-coded fallback applies an exponent of
|
|
33
|
-
* 2 for legacy compatibility. The WPCOM currencies endpoint can send
|
|
34
|
-
* `{ "IDR": { "decimal": 0 } }` to override that hard-coded 2 back to 0.
|
|
35
|
-
*/
|
|
36
|
-
decimal?: number;
|
|
37
|
-
}
|
|
38
|
-
export interface NumberFormatCurrencyParams {
|
|
39
|
-
/**
|
|
40
|
-
* Number to format.
|
|
41
|
-
*/
|
|
42
|
-
number: number;
|
|
43
|
-
/**
|
|
44
|
-
* Browser-safe locale string that works with Intl.NumberFormat e.g. 'en-US' (not 'en_US').
|
|
45
|
-
*/
|
|
46
|
-
browserSafeLocale: string;
|
|
47
|
-
/**
|
|
48
|
-
* Currency code.
|
|
49
|
-
*/
|
|
50
|
-
currency: string;
|
|
51
|
-
/**
|
|
52
|
-
* Whether to use latin numbers by default ( default = true ).
|
|
53
|
-
*/
|
|
54
|
-
forceLatin?: boolean;
|
|
55
|
-
/**
|
|
56
|
-
* The user's geo location if available.
|
|
57
|
-
*/
|
|
58
|
-
geoLocation?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Forces any decimal zeros to be hidden if set.
|
|
61
|
-
*
|
|
62
|
-
* For example, the function will normally format `10.00` in `USD` as
|
|
63
|
-
* `$10.00` but when this option is true, it will return `$10` instead.
|
|
64
|
-
*
|
|
65
|
-
* For currencies without decimals (eg: JPY), this has no effect.
|
|
66
|
-
*/
|
|
67
|
-
stripZeros?: boolean;
|
|
68
|
-
/**
|
|
69
|
-
* Changes function to treat number as an integer in the currency's smallest unit.
|
|
70
|
-
*
|
|
71
|
-
* Since rounding errors are common in floating point math, sometimes a price
|
|
72
|
-
* is provided as an integer in the smallest unit of a currency (eg: cents in
|
|
73
|
-
* USD or yen in JPY). If this option is false, the function will format the
|
|
74
|
-
* amount `1025` in `USD` as `$1,025.00`, but when the option is true, it
|
|
75
|
-
* will return `$10.25` instead.
|
|
76
|
-
*/
|
|
77
|
-
isSmallestUnit?: boolean;
|
|
78
|
-
/**
|
|
79
|
-
* If the number is greater than 0, setting this to true will include its
|
|
80
|
-
* sign (eg: `+$35.00`). Has no effect on negative numbers or 0.
|
|
81
|
-
*/
|
|
82
|
-
signForPositive?: boolean;
|
|
83
|
-
/**
|
|
84
|
-
* Dynamic currency overrides, typically supplied by the host application
|
|
85
|
-
* (eg: from a remote endpoint) via `setCurrencyOverrides`.
|
|
86
|
-
*
|
|
87
|
-
* When provided, entries here take precedence over the hard-coded defaults
|
|
88
|
-
* baked into the package on a per-field basis. Anything not specified in
|
|
89
|
-
* this map falls back to the hard-coded defaults, so passing a partial map
|
|
90
|
-
* is safe.
|
|
91
|
-
*/
|
|
92
|
-
currencyOverrides?: Record<string, CurrencyOverride>;
|
|
93
|
-
}
|
|
94
|
-
export interface CurrencyObject {
|
|
95
|
-
/**
|
|
96
|
-
* The negative sign for the price, if it is negative, or the positive sign
|
|
97
|
-
* if `signForPositive` is set.
|
|
98
|
-
*/
|
|
99
|
-
sign: '-' | '+' | '';
|
|
100
|
-
/**
|
|
101
|
-
* The currency symbol for the formatted price.
|
|
102
|
-
*
|
|
103
|
-
* Note that the symbol's position depends on the `symbolPosition` property,
|
|
104
|
-
* and keep RTL locales in mind.
|
|
105
|
-
*/
|
|
106
|
-
symbol: string;
|
|
107
|
-
/**
|
|
108
|
-
* The position of the currency symbol relative to the formatted price.
|
|
109
|
-
*/
|
|
110
|
-
symbolPosition: 'before' | 'after';
|
|
111
|
-
/**
|
|
112
|
-
* The section of the formatted price before the decimal.
|
|
113
|
-
*
|
|
114
|
-
* Note that this is not a number, but a locale-formatted string which may
|
|
115
|
-
* include symbols like spaces, commas, or periods as group separators.
|
|
116
|
-
*/
|
|
117
|
-
integer: string;
|
|
118
|
-
/**
|
|
119
|
-
* The section of the formatted price after and including the decimal.
|
|
120
|
-
*
|
|
121
|
-
* Note that this is not a number, but a locale-formatted string which may
|
|
122
|
-
* include symbols like spaces, commas, or periods as the decimal separator.
|
|
123
|
-
*/
|
|
124
|
-
fraction: string;
|
|
125
|
-
/**
|
|
126
|
-
* True if the formatted number has a non-0 decimal part.
|
|
127
|
-
*/
|
|
128
|
-
hasNonZeroFraction: boolean;
|
|
129
|
-
/**
|
|
130
|
-
* The raw floating-point version of the number prepared for formatting,
|
|
131
|
-
* after unit conversion and precision scaling.
|
|
132
|
-
*
|
|
133
|
-
* For non-decimal currencies (eg: JPY) this will actually be an integer.
|
|
134
|
-
* Otherwise it will likely be a floating-point number. Be careful with this!
|
|
135
|
-
* It should not be used for math if possible because of floating-point
|
|
136
|
-
* rounding issues! Use the smallest unit instead.
|
|
137
|
-
*/
|
|
138
|
-
floatValue: number;
|
|
139
|
-
}
|
|
140
|
-
export type FormatNumber = (number: number, options?: Omit<NumberFormatParams, 'browserSafeLocale'>) => string;
|
|
141
|
-
export type FormatCurrency = (number: number, currency: string, options?: Omit<NumberFormatCurrencyParams, 'number' | 'currency' | 'browserSafeLocale' | 'geoLocation' | 'currencyOverrides'>) => string;
|
|
142
|
-
export type GetCurrencyObject = (number: number, currency: string, options?: Omit<NumberFormatCurrencyParams, 'number' | 'currency' | 'browserSafeLocale' | 'geoLocation' | 'currencyOverrides'>) => CurrencyObject;
|