@coingecko/cryptoformat 0.8.2 → 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.
@@ -1,512 +0,0 @@
1
- // A map of supported currency codes to currency symbols.
2
- const supportedCurrencySymbols = {
3
- BTC: "₿",
4
- ETH: "Ξ",
5
- USD: "$",
6
- CAD: "C$",
7
- GBP: "£",
8
- EUR: "€",
9
- CHF: "Fr.",
10
- SEK: "kr",
11
- JPY: "¥",
12
- CNY: "¥",
13
- INR: "₹",
14
- RUB: "₽",
15
- AUD: "A$",
16
- HKD: "HK$",
17
- SGD: "S$",
18
- TWD: "NT$",
19
- BRL: "R$",
20
- KRW: "₩",
21
- ZAR: "R",
22
- MYR: "RM",
23
- IDR: "Rp",
24
- NZD: "NZ$",
25
- MXN: "MX$",
26
- PHP: "₱",
27
- DKK: "kr.",
28
- PLN: "zł",
29
- AED: "DH",
30
- ARS: "$",
31
- CLP: "CLP",
32
- CZK: "Kč",
33
- HUF: "Ft",
34
- ILS: "₪",
35
- KWD: "KD",
36
- LKR: "Rs",
37
- NOK: "kr",
38
- PKR: "₨",
39
- SAR: "SR",
40
- THB: "฿",
41
- TRY: "₺",
42
- XAU: "XAU",
43
- XAG: "XAG",
44
- XDR: "XDR",
45
- };
46
-
47
- // A map of override objects to apply.
48
- // Format:
49
- // { location: { start: Boolean, end: Boolean }, forLocales: { <locale>: Boolean } }
50
- const symbolOverrides = {
51
- MYR: { location: { start: true }, forLocales: { en: true } },
52
- SGD: { location: { start: true }, forLocales: { en: true } },
53
- PHP: { location: { start: true }, forLocales: { en: true } },
54
- BTC: { location: { start: true }, forLocales: { en: true } },
55
- ETH: { location: { start: true }, forLocales: { en: true } },
56
- };
57
-
58
- // Feature detection for Intl.NumberFormat
59
- function IntlNumberFormatSupported() {
60
- return !!(
61
- typeof Intl == "object" &&
62
- Intl &&
63
- typeof Intl.NumberFormat == "function"
64
- );
65
- }
66
-
67
- function isBTCETH(isoCode) {
68
- return isoCode === "BTC" || isoCode === "ETH";
69
- }
70
-
71
- function isCrypto(isoCode) {
72
- return isBTCETH(isoCode) || supportedCurrencySymbols[isoCode] == null;
73
- }
74
-
75
- // Function to transform decimal trailing zeroes to exponent
76
- function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) {
77
- const decimalTrailingZeroesPattern = new RegExp(`(\\.|,)(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
78
-
79
- return formattedCurrency.replace(
80
- decimalTrailingZeroesPattern,
81
- (_match, separator, decimalTrailingZeroes) => `${separator}0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
82
- )
83
- }
84
-
85
- // Function to transform the output from Intl.NumberFormat#format
86
- function formatCurrencyOverride(formattedCurrency, locale = "en", maximumDecimalTrailingZeroes) {
87
- if (typeof maximumDecimalTrailingZeroes !== "undefined") {
88
- formattedCurrency = decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes);
89
- }
90
-
91
- // If currency code remains in front
92
- const currencyCodeFrontMatch = formattedCurrency.match(/^[A-Z]{3}\s?/);
93
- if (currencyCodeFrontMatch != null) {
94
- const code = currencyCodeFrontMatch[0].trim(); // trim possible trailing space
95
-
96
- // Replace currency code with symbol if whitelisted.
97
- const overrideObj = symbolOverrides[code];
98
- if (
99
- overrideObj &&
100
- overrideObj.location.start &&
101
- overrideObj.forLocales[locale]
102
- ) {
103
- return formattedCurrency.replace(
104
- currencyCodeFrontMatch[0],
105
- supportedCurrencySymbols[code]
106
- );
107
- } else {
108
- return formattedCurrency;
109
- }
110
- }
111
-
112
- // If currency code is at the back
113
- const currencyCodeBackMatch = formattedCurrency.match(/[A-Z]{3}$/);
114
- if (currencyCodeBackMatch != null) {
115
- const code = currencyCodeBackMatch[0];
116
-
117
- // Replace currency code with symbol if whitelisted.
118
- const overrideObj = symbolOverrides[code];
119
- if (
120
- overrideObj &&
121
- overrideObj.location.end &&
122
- overrideObj.forLocales[locale]
123
- ) {
124
- return formattedCurrency.replace(code, supportedCurrencySymbols[code]);
125
- } else {
126
- return formattedCurrency;
127
- }
128
- }
129
-
130
- return formattedCurrency;
131
- }
132
-
133
- // Generates a formatter from Intl.NumberFormat
134
- function generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig) {
135
- try {
136
- const params = {
137
- style: "currency",
138
- currency: isoCode,
139
- currencyDisplay: "symbol",
140
- };
141
- if (numDecimals !== undefined) {
142
- params.minimumFractionDigits = numDecimals;
143
- params.maximumFractionDigits = numDecimals;
144
- } else if (numSigFig !== undefined) {
145
- params.maximumSignificantDigits = numSigFig;
146
- }
147
- return new Intl.NumberFormat(locale, params);
148
- } catch (e) {
149
- // Unsupported currency, etc.
150
- // Use primitive fallback
151
- return generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
152
- }
153
- }
154
-
155
- // Generates a primitive fallback formatter with no symbol support.
156
- function generateFallbackFormatter(isoCode, locale, numDecimals = 2, maximumSignificantDigits = 4) {
157
- isoCode = isoCode.toUpperCase();
158
-
159
- if (numDecimals > 2) {
160
- return {
161
- format: (value) => {
162
- return isCrypto(isoCode)
163
- ? `${value.toFixed(numDecimals)} ${isoCode}`
164
- : `${isoCode} ${value.toFixed(numDecimals)}`;
165
- },
166
- };
167
- } else {
168
- return {
169
- format: (value) => {
170
- let formattedValue = value;
171
- // try using Intl.NumberFormat when possible to support max significant digits
172
- try {
173
- formattedValue = new Intl.NumberFormat(locale, { maximumSignificantDigits }).format(value);
174
- } catch (e) {}
175
-
176
- return isCrypto(isoCode)
177
- ? `${formattedValue.toLocaleString(locale)} ${isoCode}`
178
- : `${isoCode} ${formattedValue.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, numSigFig);
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
- export { clearCache, formatCurrency, isCrypto };