@ohos-ports/cryptoformat 0.0.0-stage → 0.8.3-beta.0
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/LICENSE +7 -0
- package/README.md +80 -2
- package/lib/cryptoformat.cjs.js +525 -0
- package/lib/cryptoformat.esm.js +512 -0
- package/lib/cryptoformat.umd.js +522 -0
- package/lib/index.d.ts +40 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2018 CoinGecko
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,81 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cryptoformat
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://badge.fury.io/js/%40coingecko%2Fcryptoformat)
|
|
4
|
+
[](https://travis-ci.org/coingecko/cryptoformat)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
`cryptoformat` is used by CoinGecko (<https://www.coingecko.com>) to format crypto and fiat values.
|
|
8
|
+
|
|
9
|
+
Often an altcoin can be worth much less than \$0.01 USD, and thus we need to format this value by providing more decimal places in the formatting to prevent losing precious information.
|
|
10
|
+
|
|
11
|
+
`cryptoformat` also tries to handle different locales and currency formatting by deferring the work to the browser's [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat). If `Intl.NumberFormat` is not supported by the browser, `cryptoformat` provides a primitive fallback for currency display.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm i @coingecko/cryptoformat
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { formatCurrency } from "@coingecko/cryptoformat";
|
|
23
|
+
|
|
24
|
+
formatCurrency(123, "USD", "en");
|
|
25
|
+
// "$123.00"
|
|
26
|
+
|
|
27
|
+
formatCurrency(0.00123, "USD", "en");
|
|
28
|
+
// "$0.00123000"
|
|
29
|
+
|
|
30
|
+
// Provide raw = true to remove formatting and symbol
|
|
31
|
+
formatCurrency(0.00123, "USD", "en", true);
|
|
32
|
+
// "0.00123000"
|
|
33
|
+
|
|
34
|
+
formatCurrency(123400, "IDR", "id");
|
|
35
|
+
// "Rp123.400"
|
|
36
|
+
|
|
37
|
+
formatCurrency(123400, "EUR", "de");
|
|
38
|
+
// "123.400 €"
|
|
39
|
+
|
|
40
|
+
// Provide noDecimal = true to explicitly remove decimal for numbers above > 1.0
|
|
41
|
+
formatCurrency(4000.23, "USD", "en", false, true);
|
|
42
|
+
// "$4,000"
|
|
43
|
+
|
|
44
|
+
// Provide number of decimal places or significant figures
|
|
45
|
+
formatCurrency(1.234, "USD", "en", false, { decimalPlaces: 2 });
|
|
46
|
+
// "$1.23"
|
|
47
|
+
|
|
48
|
+
formatCurrency(1234, "USD", "en", false, { significantFigures: 2 });
|
|
49
|
+
// "$1,200"
|
|
50
|
+
|
|
51
|
+
// Provide number of significant figures only up to specified number of decimal places
|
|
52
|
+
formatCurrency(0.1234, "USD", "en", false, { decimalPlaces: 2, significantFigures: 3 });
|
|
53
|
+
// "$0.12"
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`cryptoformat` tries to cache formatters for reuse internally. If same locale and currency is used, the cached formatter will be used.
|
|
58
|
+
|
|
59
|
+
## Known Issues
|
|
60
|
+
|
|
61
|
+
1. `Intl.NumberFormat` does not always behave consistently across browsers. `cryptoformat` does some manual overrides in order to ensure that "MYR123.00" is displayed as "RM123.00", for example.
|
|
62
|
+
2. Given that country detection for locale is quite hard to do, e.g. "en-MY", `cryptoformat` does not try to do country sniffing. It is the responsibility of the caller to provide that if possible, but providing only "en" should also work for the most part, but not perfectly: users in different regions may expect a different formatting for the same language.
|
|
63
|
+
|
|
64
|
+
## Development
|
|
65
|
+
|
|
66
|
+
### Deployment
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
npm run build && npm run submit
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### File Sturcture
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
|- lib
|
|
76
|
+
|- index.js (Build with babel)
|
|
77
|
+
|- src
|
|
78
|
+
- index.js (Main Code)
|
|
79
|
+
- index.d.ts (Types for TypeScript)
|
|
80
|
+
- test.js (Test with Jest)
|
|
81
|
+
```
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// A map of supported currency codes to currency symbols.
|
|
4
|
+
const supportedCurrencySymbols = {
|
|
5
|
+
BTC: "₿",
|
|
6
|
+
ETH: "Ξ",
|
|
7
|
+
USD: "$",
|
|
8
|
+
CAD: "C$",
|
|
9
|
+
GBP: "£",
|
|
10
|
+
EUR: "€",
|
|
11
|
+
CHF: "Fr.",
|
|
12
|
+
SEK: "kr",
|
|
13
|
+
JPY: "¥",
|
|
14
|
+
CNY: "¥",
|
|
15
|
+
INR: "₹",
|
|
16
|
+
RUB: "₽",
|
|
17
|
+
AUD: "A$",
|
|
18
|
+
HKD: "HK$",
|
|
19
|
+
SGD: "S$",
|
|
20
|
+
TWD: "NT$",
|
|
21
|
+
BRL: "R$",
|
|
22
|
+
KRW: "₩",
|
|
23
|
+
ZAR: "R",
|
|
24
|
+
MYR: "RM",
|
|
25
|
+
IDR: "Rp",
|
|
26
|
+
NZD: "NZ$",
|
|
27
|
+
MXN: "MX$",
|
|
28
|
+
PHP: "₱",
|
|
29
|
+
DKK: "kr.",
|
|
30
|
+
PLN: "zł",
|
|
31
|
+
AED: "DH",
|
|
32
|
+
ARS: "$",
|
|
33
|
+
CLP: "CLP",
|
|
34
|
+
CZK: "Kč",
|
|
35
|
+
HUF: "Ft",
|
|
36
|
+
ILS: "₪",
|
|
37
|
+
KWD: "KD",
|
|
38
|
+
LKR: "Rs",
|
|
39
|
+
NOK: "kr",
|
|
40
|
+
PKR: "₨",
|
|
41
|
+
SAR: "SR",
|
|
42
|
+
THB: "฿",
|
|
43
|
+
TRY: "₺",
|
|
44
|
+
XAU: "XAU",
|
|
45
|
+
XAG: "XAG",
|
|
46
|
+
XDR: "XDR",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// A map of override objects to apply.
|
|
50
|
+
// Format:
|
|
51
|
+
// { location: { start: Boolean, end: Boolean }, forLocales: { <locale>: Boolean } }
|
|
52
|
+
const symbolOverrides = {
|
|
53
|
+
MYR: { location: { start: true }, forLocales: { en: true } },
|
|
54
|
+
SGD: { location: { start: true }, forLocales: { en: true } },
|
|
55
|
+
PHP: { location: { start: true }, forLocales: { en: true } },
|
|
56
|
+
BTC: { location: { start: true }, forLocales: { en: true } },
|
|
57
|
+
ETH: { location: { start: true }, forLocales: { en: true } },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Feature detection for Intl.NumberFormat
|
|
61
|
+
function IntlNumberFormatSupported() {
|
|
62
|
+
return !!(
|
|
63
|
+
typeof Intl == "object" &&
|
|
64
|
+
Intl &&
|
|
65
|
+
typeof Intl.NumberFormat == "function"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isBTCETH(isoCode) {
|
|
70
|
+
return isoCode === "BTC" || isoCode === "ETH";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isCrypto(isoCode) {
|
|
74
|
+
return isBTCETH(isoCode) || supportedCurrencySymbols[isoCode] == null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Function to transform decimal trailing zeroes to exponent
|
|
78
|
+
function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) {
|
|
79
|
+
const decimalTrailingZeroesPattern = new RegExp(`(\\.|,)(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
|
|
80
|
+
|
|
81
|
+
return formattedCurrency.replace(
|
|
82
|
+
decimalTrailingZeroesPattern,
|
|
83
|
+
(_match, separator, decimalTrailingZeroes) => `${separator}0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Function to transform the output from Intl.NumberFormat#format
|
|
88
|
+
function formatCurrencyOverride(formattedCurrency, locale = "en", maximumDecimalTrailingZeroes) {
|
|
89
|
+
if (typeof maximumDecimalTrailingZeroes !== "undefined") {
|
|
90
|
+
formattedCurrency = decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// If currency code remains in front
|
|
94
|
+
const currencyCodeFrontMatch = formattedCurrency.match(/^[A-Z]{3}\s?/);
|
|
95
|
+
if (currencyCodeFrontMatch != null) {
|
|
96
|
+
const code = currencyCodeFrontMatch[0].trim(); // trim possible trailing space
|
|
97
|
+
|
|
98
|
+
// Replace currency code with symbol if whitelisted.
|
|
99
|
+
const overrideObj = symbolOverrides[code];
|
|
100
|
+
if (
|
|
101
|
+
overrideObj &&
|
|
102
|
+
overrideObj.location.start &&
|
|
103
|
+
overrideObj.forLocales[locale]
|
|
104
|
+
) {
|
|
105
|
+
return formattedCurrency.replace(
|
|
106
|
+
currencyCodeFrontMatch[0],
|
|
107
|
+
supportedCurrencySymbols[code]
|
|
108
|
+
);
|
|
109
|
+
} else {
|
|
110
|
+
return formattedCurrency;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// If currency code is at the back
|
|
115
|
+
const currencyCodeBackMatch = formattedCurrency.match(/[A-Z]{3}$/);
|
|
116
|
+
if (currencyCodeBackMatch != null) {
|
|
117
|
+
const code = currencyCodeBackMatch[0];
|
|
118
|
+
|
|
119
|
+
// Replace currency code with symbol if whitelisted.
|
|
120
|
+
const overrideObj = symbolOverrides[code];
|
|
121
|
+
if (
|
|
122
|
+
overrideObj &&
|
|
123
|
+
overrideObj.location.end &&
|
|
124
|
+
overrideObj.forLocales[locale]
|
|
125
|
+
) {
|
|
126
|
+
return formattedCurrency.replace(code, supportedCurrencySymbols[code]);
|
|
127
|
+
} else {
|
|
128
|
+
return formattedCurrency;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return formattedCurrency;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Generates a formatter from Intl.NumberFormat
|
|
136
|
+
function generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig) {
|
|
137
|
+
try {
|
|
138
|
+
const params = {
|
|
139
|
+
style: "currency",
|
|
140
|
+
currency: isoCode,
|
|
141
|
+
currencyDisplay: "symbol",
|
|
142
|
+
};
|
|
143
|
+
if (numDecimals !== undefined) {
|
|
144
|
+
params.minimumFractionDigits = numDecimals;
|
|
145
|
+
params.maximumFractionDigits = numDecimals;
|
|
146
|
+
} else if (numSigFig !== undefined) {
|
|
147
|
+
params.maximumSignificantDigits = numSigFig;
|
|
148
|
+
}
|
|
149
|
+
return new Intl.NumberFormat(locale, params);
|
|
150
|
+
} catch (e) {
|
|
151
|
+
// Unsupported currency, etc.
|
|
152
|
+
// Use primitive fallback
|
|
153
|
+
return generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Generates a primitive fallback formatter with no symbol support.
|
|
158
|
+
function generateFallbackFormatter(isoCode, locale, numDecimals = 2, maximumSignificantDigits = 4) {
|
|
159
|
+
isoCode = isoCode.toUpperCase();
|
|
160
|
+
|
|
161
|
+
if (numDecimals > 2) {
|
|
162
|
+
return {
|
|
163
|
+
format: (value) => {
|
|
164
|
+
return isCrypto(isoCode)
|
|
165
|
+
? `${value.toFixed(numDecimals)} ${isoCode}`
|
|
166
|
+
: `${isoCode} ${value.toFixed(numDecimals)}`;
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
} else {
|
|
170
|
+
return {
|
|
171
|
+
format: (value) => {
|
|
172
|
+
let formattedValue = value;
|
|
173
|
+
// try using Intl.NumberFormat when possible to support max significant digits
|
|
174
|
+
try {
|
|
175
|
+
formattedValue = new Intl.NumberFormat(locale, { maximumSignificantDigits }).format(value);
|
|
176
|
+
} catch (e) {}
|
|
177
|
+
|
|
178
|
+
return isCrypto(isoCode)
|
|
179
|
+
? `${formattedValue.toLocaleString(locale)} ${isoCode}`
|
|
180
|
+
: `${isoCode} ${formattedValue.toLocaleString(locale)}`;
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function generateAbbreviatedFormatter(isoCode, locale) {
|
|
187
|
+
// Show regular numbers if no Intl.NumberFormat support.
|
|
188
|
+
if (!IntlNumberFormatSupported()) {
|
|
189
|
+
return generateFallbackFormatter(isoCode, locale, 0);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 3 };
|
|
193
|
+
|
|
194
|
+
// Currency symbol is supported if currency is Fiat/BTC/ETH.
|
|
195
|
+
if (!isCrypto(isoCode) || isBTCETH(isoCode)) {
|
|
196
|
+
numberFormatOptions.style = "currency";
|
|
197
|
+
numberFormatOptions.currency = isoCode;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return new Intl.NumberFormat(locale, numberFormatOptions);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
|
|
204
|
+
const isNumberFormatSupported = IntlNumberFormatSupported();
|
|
205
|
+
|
|
206
|
+
const useIntlNumberFormatter =
|
|
207
|
+
isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
|
|
208
|
+
return useIntlNumberFormatter
|
|
209
|
+
? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
|
|
210
|
+
: generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// State variables
|
|
214
|
+
let currentISOCode;
|
|
215
|
+
let currentLocale;
|
|
216
|
+
let currencyFormatterNormal;
|
|
217
|
+
let currencyFormatterNoDecimal;
|
|
218
|
+
let currencyFormatterMedium;
|
|
219
|
+
let currencyFormatterTwoDecimal;
|
|
220
|
+
let currencyFormatterSmall;
|
|
221
|
+
let currencyFormatterVerySmall;
|
|
222
|
+
let currencyFormatterVeryVerySmall;
|
|
223
|
+
let currencyFormatter15DP;
|
|
224
|
+
let currencyFormatter18DP;
|
|
225
|
+
let currencyFormatterAbbreviated;
|
|
226
|
+
|
|
227
|
+
// If a page has to display multiple currencies, formatters would have to be created for each of them
|
|
228
|
+
// To save some effort, we save formatters for reuse
|
|
229
|
+
let formattersCache = {};
|
|
230
|
+
|
|
231
|
+
function clearCache() {
|
|
232
|
+
formattersCache = {};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function initializeFormatters(isoCode, locale) {
|
|
236
|
+
const cacheKey = `${isoCode}-${locale}`;
|
|
237
|
+
const cachedFormatter = formattersCache[cacheKey];
|
|
238
|
+
|
|
239
|
+
currencyFormatterNormal = cachedFormatter
|
|
240
|
+
? cachedFormatter.currencyFormatterNormal
|
|
241
|
+
: generateFormatter(isoCode, locale);
|
|
242
|
+
currencyFormatterNoDecimal = cachedFormatter
|
|
243
|
+
? cachedFormatter.currencyFormatterNoDecimal
|
|
244
|
+
: generateFormatter(isoCode, locale, 0);
|
|
245
|
+
currencyFormatterMedium = cachedFormatter
|
|
246
|
+
? cachedFormatter.currencyFormatterMedium
|
|
247
|
+
: generateFormatter(isoCode, locale, 3);
|
|
248
|
+
currencyFormatterTwoDecimal = cachedFormatter
|
|
249
|
+
? cachedFormatter.currencyFormatterTwoDecimal
|
|
250
|
+
: generateFormatter(isoCode, locale, 2);
|
|
251
|
+
currencyFormatterSmall = cachedFormatter
|
|
252
|
+
? cachedFormatter.currencyFormatterSmall
|
|
253
|
+
: generateFormatter(isoCode, locale, 6);
|
|
254
|
+
currencyFormatterVerySmall = cachedFormatter
|
|
255
|
+
? cachedFormatter.currencyFormatterVerySmall
|
|
256
|
+
: generateFormatter(isoCode, locale, 8);
|
|
257
|
+
currencyFormatterVeryVerySmall = cachedFormatter
|
|
258
|
+
? cachedFormatter.currencyFormatterVeryVerySmall
|
|
259
|
+
: generateFormatter(isoCode, locale, 12);
|
|
260
|
+
currencyFormatter15DP = cachedFormatter
|
|
261
|
+
? cachedFormatter.currencyFormatter15DP
|
|
262
|
+
: generateFormatter(isoCode, locale, 15);
|
|
263
|
+
currencyFormatter18DP = cachedFormatter
|
|
264
|
+
? cachedFormatter.currencyFormatter18DP
|
|
265
|
+
: generateFormatter(isoCode, locale, 18);
|
|
266
|
+
currencyFormatterAbbreviated = cachedFormatter
|
|
267
|
+
? cachedFormatter.currencyFormatterAbbreviated
|
|
268
|
+
: generateAbbreviatedFormatter(isoCode, locale);
|
|
269
|
+
|
|
270
|
+
// Save in cache
|
|
271
|
+
if (cachedFormatter == null) {
|
|
272
|
+
formattersCache[cacheKey] = {};
|
|
273
|
+
formattersCache[cacheKey].currencyFormatterNormal = currencyFormatterNormal;
|
|
274
|
+
formattersCache[cacheKey].currencyFormatterNoDecimal = currencyFormatterNoDecimal;
|
|
275
|
+
formattersCache[cacheKey].currencyFormatterMedium = currencyFormatterMedium;
|
|
276
|
+
formattersCache[cacheKey].currencyFormatterTwoDecimal = currencyFormatterTwoDecimal;
|
|
277
|
+
formattersCache[cacheKey].currencyFormatterSmall = currencyFormatterSmall;
|
|
278
|
+
formattersCache[cacheKey].currencyFormatterVerySmall = currencyFormatterVerySmall;
|
|
279
|
+
formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
|
|
280
|
+
formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
|
|
281
|
+
formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
|
|
282
|
+
formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Moderate crypto amount threshold
|
|
287
|
+
const MEDIUM_CRYPTO_THRESHOLD = 50;
|
|
288
|
+
// Large crypto amount threshold
|
|
289
|
+
const LARGE_CRYPTO_THRESHOLD = 1000;
|
|
290
|
+
// No decimal threshold for large amounts
|
|
291
|
+
const NO_DECIMAL_THRESHOLD = 100000;
|
|
292
|
+
|
|
293
|
+
function formatCurrency(
|
|
294
|
+
amount,
|
|
295
|
+
isoCode,
|
|
296
|
+
locale = "en",
|
|
297
|
+
raw = false,
|
|
298
|
+
noDecimal = false,
|
|
299
|
+
abbreviated = false,
|
|
300
|
+
) {
|
|
301
|
+
isoCode = isoCode.toUpperCase();
|
|
302
|
+
let maximumDecimalTrailingZeroes = undefined;
|
|
303
|
+
|
|
304
|
+
if (currentISOCode !== isoCode || currentLocale != locale) {
|
|
305
|
+
currentISOCode = isoCode;
|
|
306
|
+
currentLocale = locale;
|
|
307
|
+
|
|
308
|
+
// Formatters are tied to currency code, we try to initialize as infrequently as possible.
|
|
309
|
+
initializeFormatters(isoCode, locale);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (abbreviated) {
|
|
313
|
+
let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount);
|
|
314
|
+
|
|
315
|
+
// Manually add currency code to the back for non-BTC/ETH crypto currencies.
|
|
316
|
+
if (isCrypto(isoCode) && !isBTCETH(isoCode)) {
|
|
317
|
+
formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return formatCurrencyOverride(formattedAbbreviatedCurrency, locale);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (noDecimal === true && amount > 100.0) {
|
|
324
|
+
return formatCurrencyOverride(
|
|
325
|
+
currencyFormatterNoDecimal.format(amount),
|
|
326
|
+
locale
|
|
327
|
+
);
|
|
328
|
+
} else if (typeof noDecimal === "object" && noDecimal !== null) {
|
|
329
|
+
if (raw) {
|
|
330
|
+
// Limit to max n decimal places if applicable
|
|
331
|
+
let raw_amount = noDecimal.hasOwnProperty("decimalPlaces")
|
|
332
|
+
? amount.toFixed(noDecimal.decimalPlaces)
|
|
333
|
+
: amount;
|
|
334
|
+
// Round off to number of significant figures without trailing 0's
|
|
335
|
+
return `${parseFloat(raw_amount).toPrecision(noDecimal.significantFigures) / 1}`;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (noDecimal.hasOwnProperty("maximumDecimalTrailingZeroes")) {
|
|
339
|
+
maximumDecimalTrailingZeroes = noDecimal.maximumDecimalTrailingZeroes;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (noDecimal.hasOwnProperty("decimalPlaces") && noDecimal.hasOwnProperty("significantFigures")) {
|
|
343
|
+
// Show specified number of significant digits with cutoff of specified fraction digits
|
|
344
|
+
const currencyFormatterCustom = generateFormatter(
|
|
345
|
+
isoCode,
|
|
346
|
+
locale,
|
|
347
|
+
undefined,
|
|
348
|
+
noDecimal.significantFigures
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
return formatCurrencyOverride(
|
|
352
|
+
currencyFormatterCustom.format(
|
|
353
|
+
Number.parseFloat(amount.toFixed(noDecimal.decimalPlaces))
|
|
354
|
+
),
|
|
355
|
+
locale,
|
|
356
|
+
maximumDecimalTrailingZeroes
|
|
357
|
+
);
|
|
358
|
+
} else if (noDecimal.hasOwnProperty("decimalPlaces") || noDecimal.hasOwnProperty("significantFigures")) {
|
|
359
|
+
const currencyFormatterCustom = generateFormatter(
|
|
360
|
+
isoCode,
|
|
361
|
+
locale,
|
|
362
|
+
noDecimal.decimalPlaces,
|
|
363
|
+
noDecimal.significantFigures
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
return formatCurrencyOverride(
|
|
367
|
+
currencyFormatterCustom.format(amount),
|
|
368
|
+
locale,
|
|
369
|
+
maximumDecimalTrailingZeroes
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (isCrypto(isoCode)) {
|
|
375
|
+
let price = parseFloat(amount);
|
|
376
|
+
|
|
377
|
+
if (raw) {
|
|
378
|
+
if (amount === 0.0) {
|
|
379
|
+
return price.toFixed(2);
|
|
380
|
+
}
|
|
381
|
+
return price.toPrecision(8);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (amount === 0.0) {
|
|
385
|
+
return formatCurrencyOverride(
|
|
386
|
+
currencyFormatterNormal.format(amount),
|
|
387
|
+
locale,
|
|
388
|
+
maximumDecimalTrailingZeroes
|
|
389
|
+
);
|
|
390
|
+
} else if (price >= LARGE_CRYPTO_THRESHOLD) {
|
|
391
|
+
// Large crypto amount, show no decimal value
|
|
392
|
+
return formatCurrencyOverride(
|
|
393
|
+
currencyFormatterNoDecimal.format(amount),
|
|
394
|
+
locale
|
|
395
|
+
);
|
|
396
|
+
} else if (
|
|
397
|
+
price >= MEDIUM_CRYPTO_THRESHOLD &&
|
|
398
|
+
price < LARGE_CRYPTO_THRESHOLD
|
|
399
|
+
) {
|
|
400
|
+
// Medium crypto amount, show 3 fraction digits
|
|
401
|
+
return formatCurrencyOverride(
|
|
402
|
+
currencyFormatterMedium.format(amount),
|
|
403
|
+
locale,
|
|
404
|
+
maximumDecimalTrailingZeroes
|
|
405
|
+
);
|
|
406
|
+
} else if (price >= 1.0 && price < MEDIUM_CRYPTO_THRESHOLD) {
|
|
407
|
+
// crypto amount, show 6 fraction digits
|
|
408
|
+
return formatCurrencyOverride(
|
|
409
|
+
currencyFormatterSmall.format(amount),
|
|
410
|
+
locale,
|
|
411
|
+
maximumDecimalTrailingZeroes
|
|
412
|
+
);
|
|
413
|
+
} else if (price >= 0.000001 && price < 1.0) {
|
|
414
|
+
// crypto amount, show 8 fraction digits
|
|
415
|
+
return formatCurrencyOverride(
|
|
416
|
+
currencyFormatterVerySmall.format(amount),
|
|
417
|
+
locale,
|
|
418
|
+
maximumDecimalTrailingZeroes
|
|
419
|
+
);
|
|
420
|
+
} else if (price >= 10**-9 && price < 10**-6) {
|
|
421
|
+
return formatCurrencyOverride(
|
|
422
|
+
currencyFormatterVeryVerySmall.format(amount),
|
|
423
|
+
locale,
|
|
424
|
+
maximumDecimalTrailingZeroes
|
|
425
|
+
);
|
|
426
|
+
} else if (price >= 10**-12 && price < 10**-9) {
|
|
427
|
+
return formatCurrencyOverride(
|
|
428
|
+
currencyFormatter15DP.format(amount),
|
|
429
|
+
locale,
|
|
430
|
+
maximumDecimalTrailingZeroes
|
|
431
|
+
);
|
|
432
|
+
} else if (price < 10**-12) {
|
|
433
|
+
return formatCurrencyOverride(
|
|
434
|
+
currencyFormatter18DP.format(amount),
|
|
435
|
+
locale,
|
|
436
|
+
maximumDecimalTrailingZeroes
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
const unsigned_amount = Math.abs(amount);
|
|
441
|
+
if (raw) {
|
|
442
|
+
if (unsigned_amount < 10**-12) {
|
|
443
|
+
return amount.toFixed(18);
|
|
444
|
+
} else if (unsigned_amount < 10**-9) {
|
|
445
|
+
return amount.toFixed(15);
|
|
446
|
+
} else if (unsigned_amount < 10**-6) {
|
|
447
|
+
return amount.toFixed(12);
|
|
448
|
+
} else if (unsigned_amount < 10**-3) {
|
|
449
|
+
return amount.toFixed(8);
|
|
450
|
+
} else if (unsigned_amount < 1.0) {
|
|
451
|
+
return amount.toFixed(6);
|
|
452
|
+
} else {
|
|
453
|
+
return amount.toFixed(2);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (unsigned_amount === 0.0) {
|
|
458
|
+
return formatCurrencyOverride(
|
|
459
|
+
currencyFormatterNormal.format(amount),
|
|
460
|
+
locale,
|
|
461
|
+
maximumDecimalTrailingZeroes
|
|
462
|
+
);
|
|
463
|
+
} else if (unsigned_amount < 10**-12) {
|
|
464
|
+
return formatCurrencyOverride(
|
|
465
|
+
currencyFormatter18DP.format(amount),
|
|
466
|
+
locale,
|
|
467
|
+
maximumDecimalTrailingZeroes
|
|
468
|
+
);
|
|
469
|
+
} else if (unsigned_amount < 10**-9) {
|
|
470
|
+
return formatCurrencyOverride(
|
|
471
|
+
currencyFormatter15DP.format(amount),
|
|
472
|
+
locale,
|
|
473
|
+
maximumDecimalTrailingZeroes
|
|
474
|
+
);
|
|
475
|
+
} else if (unsigned_amount < 10**-6) {
|
|
476
|
+
return formatCurrencyOverride(
|
|
477
|
+
currencyFormatterVeryVerySmall.format(amount),
|
|
478
|
+
locale,
|
|
479
|
+
maximumDecimalTrailingZeroes
|
|
480
|
+
);
|
|
481
|
+
} else if (unsigned_amount < 0.05) {
|
|
482
|
+
return formatCurrencyOverride(
|
|
483
|
+
currencyFormatterVerySmall.format(amount),
|
|
484
|
+
locale,
|
|
485
|
+
maximumDecimalTrailingZeroes
|
|
486
|
+
);
|
|
487
|
+
} else if (unsigned_amount < 1.0) {
|
|
488
|
+
return formatCurrencyOverride(
|
|
489
|
+
currencyFormatterSmall.format(amount),
|
|
490
|
+
locale,
|
|
491
|
+
maximumDecimalTrailingZeroes
|
|
492
|
+
);
|
|
493
|
+
} else if (isoCode === "JPY" && unsigned_amount < 100) {
|
|
494
|
+
return formatCurrencyOverride(
|
|
495
|
+
currencyFormatterTwoDecimal.format(amount),
|
|
496
|
+
locale,
|
|
497
|
+
maximumDecimalTrailingZeroes
|
|
498
|
+
);
|
|
499
|
+
} else if (unsigned_amount > NO_DECIMAL_THRESHOLD) {
|
|
500
|
+
return formatCurrencyOverride(
|
|
501
|
+
currencyFormatterNoDecimal.format(amount),
|
|
502
|
+
locale
|
|
503
|
+
);
|
|
504
|
+
} else {
|
|
505
|
+
// Let the formatter do what it seems best. In particular, we should not set any fraction amount for Japanese Yen
|
|
506
|
+
return formatCurrencyOverride(
|
|
507
|
+
currencyFormatterNormal.format(amount),
|
|
508
|
+
locale
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// HarmonyOS migration note: package.json declares "type": "module", so this .js
|
|
515
|
+
// entry is loaded as an ES module by Node/jest ESM loaders. Keep the CommonJS
|
|
516
|
+
// assignments guarded (for tools that still load this file as CommonJS) and add
|
|
517
|
+
// real ES module exports (upstream v0.8.3 rollup output has unguarded CJS
|
|
518
|
+
// assignments, which throw under the package's own "type": "module" on any platform).
|
|
519
|
+
if (typeof exports === "object" && typeof module === "object") {
|
|
520
|
+
exports.clearCache = clearCache;
|
|
521
|
+
exports.formatCurrency = formatCurrency;
|
|
522
|
+
exports.isCrypto = isCrypto;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export { clearCache, formatCurrency, isCrypto };
|