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