@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.
@@ -1,506 +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);
152
- }
153
- }
154
-
155
- // Generates a primitive fallback formatter with no symbol support.
156
- function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
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
- return isCrypto(isoCode)
171
- ? `${value.toLocaleString(locale)} ${isoCode}`
172
- : `${isoCode} ${value.toLocaleString(locale)}`;
173
- },
174
- };
175
- }
176
- }
177
-
178
- function generateAbbreviatedFormatter(isoCode, locale) {
179
- // Show regular numbers if no Intl.NumberFormat support.
180
- if (!IntlNumberFormatSupported()) {
181
- return generateFallbackFormatter(isoCode, locale, 0);
182
- }
183
-
184
- let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 3 };
185
-
186
- // Currency symbol is supported if currency is Fiat/BTC/ETH.
187
- if (!isCrypto(isoCode) || isBTCETH(isoCode)) {
188
- numberFormatOptions.style = "currency";
189
- numberFormatOptions.currency = isoCode;
190
- }
191
-
192
- return new Intl.NumberFormat(locale, numberFormatOptions);
193
- }
194
-
195
- function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
196
- const isNumberFormatSupported = IntlNumberFormatSupported();
197
-
198
- const useIntlNumberFormatter =
199
- isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
200
- return useIntlNumberFormatter
201
- ? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
202
- : generateFallbackFormatter(isoCode, locale, numDecimals);
203
- }
204
-
205
- // State variables
206
- let currentISOCode;
207
- let currentLocale;
208
- let currencyFormatterNormal;
209
- let currencyFormatterNoDecimal;
210
- let currencyFormatterMedium;
211
- let currencyFormatterTwoDecimal;
212
- let currencyFormatterSmall;
213
- let currencyFormatterVerySmall;
214
- let currencyFormatterVeryVerySmall;
215
- let currencyFormatter15DP;
216
- let currencyFormatter18DP;
217
- let currencyFormatterAbbreviated;
218
-
219
- // If a page has to display multiple currencies, formatters would have to be created for each of them
220
- // To save some effort, we save formatters for reuse
221
- let formattersCache = {};
222
-
223
- function clearCache() {
224
- formattersCache = {};
225
- }
226
-
227
- function initializeFormatters(isoCode, locale) {
228
- const cacheKey = `${isoCode}-${locale}`;
229
- const cachedFormatter = formattersCache[cacheKey];
230
-
231
- currencyFormatterNormal = cachedFormatter
232
- ? cachedFormatter.currencyFormatterNormal
233
- : generateFormatter(isoCode, locale);
234
- currencyFormatterNoDecimal = cachedFormatter
235
- ? cachedFormatter.currencyFormatterNoDecimal
236
- : generateFormatter(isoCode, locale, 0);
237
- currencyFormatterMedium = cachedFormatter
238
- ? cachedFormatter.currencyFormatterMedium
239
- : generateFormatter(isoCode, locale, 3);
240
- currencyFormatterTwoDecimal = cachedFormatter
241
- ? cachedFormatter.currencyFormatterTwoDecimal
242
- : generateFormatter(isoCode, locale, 2);
243
- currencyFormatterSmall = cachedFormatter
244
- ? cachedFormatter.currencyFormatterSmall
245
- : generateFormatter(isoCode, locale, 6);
246
- currencyFormatterVerySmall = cachedFormatter
247
- ? cachedFormatter.currencyFormatterVerySmall
248
- : generateFormatter(isoCode, locale, 8);
249
- currencyFormatterVeryVerySmall = cachedFormatter
250
- ? cachedFormatter.currencyFormatterVeryVerySmall
251
- : generateFormatter(isoCode, locale, 12);
252
- currencyFormatter15DP = cachedFormatter
253
- ? cachedFormatter.currencyFormatter15DP
254
- : generateFormatter(isoCode, locale, 15);
255
- currencyFormatter18DP = cachedFormatter
256
- ? cachedFormatter.currencyFormatter18DP
257
- : generateFormatter(isoCode, locale, 18);
258
- currencyFormatterAbbreviated = cachedFormatter
259
- ? cachedFormatter.currencyFormatterAbbreviated
260
- : generateAbbreviatedFormatter(isoCode, locale);
261
-
262
- // Save in cache
263
- if (cachedFormatter == null) {
264
- formattersCache[cacheKey] = {};
265
- formattersCache[cacheKey].currencyFormatterNormal = currencyFormatterNormal;
266
- formattersCache[cacheKey].currencyFormatterNoDecimal = currencyFormatterNoDecimal;
267
- formattersCache[cacheKey].currencyFormatterMedium = currencyFormatterMedium;
268
- formattersCache[cacheKey].currencyFormatterTwoDecimal = currencyFormatterTwoDecimal;
269
- formattersCache[cacheKey].currencyFormatterSmall = currencyFormatterSmall;
270
- formattersCache[cacheKey].currencyFormatterVerySmall = currencyFormatterVerySmall;
271
- formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
272
- formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
273
- formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
274
- formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated;
275
- }
276
- }
277
-
278
- // Moderate crypto amount threshold
279
- const MEDIUM_CRYPTO_THRESHOLD = 50;
280
- // Large crypto amount threshold
281
- const LARGE_CRYPTO_THRESHOLD = 1000;
282
- // No decimal threshold for large amounts
283
- const NO_DECIMAL_THRESHOLD = 100000;
284
-
285
- function formatCurrency(
286
- amount,
287
- isoCode,
288
- locale = "en",
289
- raw = false,
290
- noDecimal = false,
291
- abbreviated = false,
292
- ) {
293
- isoCode = isoCode.toUpperCase();
294
- let maximumDecimalTrailingZeroes = undefined;
295
-
296
- if (currentISOCode !== isoCode || currentLocale != locale) {
297
- currentISOCode = isoCode;
298
- currentLocale = locale;
299
-
300
- // Formatters are tied to currency code, we try to initialize as infrequently as possible.
301
- initializeFormatters(isoCode, locale);
302
- }
303
-
304
- if (abbreviated) {
305
- let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount);
306
-
307
- // Manually add currency code to the back for non-BTC/ETH crypto currencies.
308
- if (isCrypto(isoCode) && !isBTCETH(isoCode)) {
309
- formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`;
310
- }
311
-
312
- return formatCurrencyOverride(formattedAbbreviatedCurrency, locale);
313
- }
314
-
315
- if (noDecimal === true && amount > 100.0) {
316
- return formatCurrencyOverride(
317
- currencyFormatterNoDecimal.format(amount),
318
- locale
319
- );
320
- } else if (typeof noDecimal === "object" && noDecimal !== null) {
321
- if (raw) {
322
- // Limit to max n decimal places if applicable
323
- let raw_amount = noDecimal.hasOwnProperty("decimalPlaces")
324
- ? amount.toFixed(noDecimal.decimalPlaces)
325
- : amount;
326
- // Round off to number of significant figures without trailing 0's
327
- return `${parseFloat(raw_amount).toPrecision(noDecimal.significantFigures) / 1}`;
328
- }
329
-
330
- if (noDecimal.hasOwnProperty("maximumDecimalTrailingZeroes")) {
331
- maximumDecimalTrailingZeroes = noDecimal.maximumDecimalTrailingZeroes;
332
- }
333
-
334
- if (noDecimal.hasOwnProperty("decimalPlaces") && noDecimal.hasOwnProperty("significantFigures")) {
335
- // Show specified number of significant digits with cutoff of specified fraction digits
336
- const currencyFormatterCustom = generateFormatter(
337
- isoCode,
338
- locale,
339
- undefined,
340
- noDecimal.significantFigures
341
- );
342
-
343
- return formatCurrencyOverride(
344
- currencyFormatterCustom.format(
345
- Number.parseFloat(amount.toFixed(noDecimal.decimalPlaces))
346
- ),
347
- locale,
348
- maximumDecimalTrailingZeroes
349
- );
350
- } else if (noDecimal.hasOwnProperty("decimalPlaces") || noDecimal.hasOwnProperty("significantFigures")) {
351
- const currencyFormatterCustom = generateFormatter(
352
- isoCode,
353
- locale,
354
- noDecimal.decimalPlaces,
355
- noDecimal.significantFigures
356
- );
357
-
358
- return formatCurrencyOverride(
359
- currencyFormatterCustom.format(amount),
360
- locale,
361
- maximumDecimalTrailingZeroes
362
- );
363
- }
364
- }
365
-
366
- if (isCrypto(isoCode)) {
367
- let price = parseFloat(amount);
368
-
369
- if (raw) {
370
- if (amount === 0.0) {
371
- return price.toFixed(2);
372
- }
373
- return price.toPrecision(8);
374
- }
375
-
376
- if (amount === 0.0) {
377
- return formatCurrencyOverride(
378
- currencyFormatterNormal.format(amount),
379
- locale,
380
- maximumDecimalTrailingZeroes
381
- );
382
- } else if (price >= LARGE_CRYPTO_THRESHOLD) {
383
- // Large crypto amount, show no decimal value
384
- return formatCurrencyOverride(
385
- currencyFormatterNoDecimal.format(amount),
386
- locale
387
- );
388
- } else if (
389
- price >= MEDIUM_CRYPTO_THRESHOLD &&
390
- price < LARGE_CRYPTO_THRESHOLD
391
- ) {
392
- // Medium crypto amount, show 3 fraction digits
393
- return formatCurrencyOverride(
394
- currencyFormatterMedium.format(amount),
395
- locale,
396
- maximumDecimalTrailingZeroes
397
- );
398
- } else if (price >= 1.0 && price < MEDIUM_CRYPTO_THRESHOLD) {
399
- // crypto amount, show 6 fraction digits
400
- return formatCurrencyOverride(
401
- currencyFormatterSmall.format(amount),
402
- locale,
403
- maximumDecimalTrailingZeroes
404
- );
405
- } else if (price >= 0.000001 && price < 1.0) {
406
- // crypto amount, show 8 fraction digits
407
- return formatCurrencyOverride(
408
- currencyFormatterVerySmall.format(amount),
409
- locale,
410
- maximumDecimalTrailingZeroes
411
- );
412
- } else if (price >= 10**-9 && price < 10**-6) {
413
- return formatCurrencyOverride(
414
- currencyFormatterVeryVerySmall.format(amount),
415
- locale,
416
- maximumDecimalTrailingZeroes
417
- );
418
- } else if (price >= 10**-12 && price < 10**-9) {
419
- return formatCurrencyOverride(
420
- currencyFormatter15DP.format(amount),
421
- locale,
422
- maximumDecimalTrailingZeroes
423
- );
424
- } else if (price < 10**-12) {
425
- return formatCurrencyOverride(
426
- currencyFormatter18DP.format(amount),
427
- locale,
428
- maximumDecimalTrailingZeroes
429
- );
430
- }
431
- } else {
432
- const unsigned_amount = Math.abs(amount);
433
- if (raw) {
434
- if (unsigned_amount < 10**-12) {
435
- return amount.toFixed(18);
436
- } else if (unsigned_amount < 10**-9) {
437
- return amount.toFixed(15);
438
- } else if (unsigned_amount < 10**-6) {
439
- return amount.toFixed(12);
440
- } else if (unsigned_amount < 10**-3) {
441
- return amount.toFixed(8);
442
- } else if (unsigned_amount < 1.0) {
443
- return amount.toFixed(6);
444
- } else {
445
- return amount.toFixed(2);
446
- }
447
- }
448
-
449
- if (unsigned_amount === 0.0) {
450
- return formatCurrencyOverride(
451
- currencyFormatterNormal.format(amount),
452
- locale,
453
- maximumDecimalTrailingZeroes
454
- );
455
- } else if (unsigned_amount < 10**-12) {
456
- return formatCurrencyOverride(
457
- currencyFormatter18DP.format(amount),
458
- locale,
459
- maximumDecimalTrailingZeroes
460
- );
461
- } else if (unsigned_amount < 10**-9) {
462
- return formatCurrencyOverride(
463
- currencyFormatter15DP.format(amount),
464
- locale,
465
- maximumDecimalTrailingZeroes
466
- );
467
- } else if (unsigned_amount < 10**-6) {
468
- return formatCurrencyOverride(
469
- currencyFormatterVeryVerySmall.format(amount),
470
- locale,
471
- maximumDecimalTrailingZeroes
472
- );
473
- } else if (unsigned_amount < 0.05) {
474
- return formatCurrencyOverride(
475
- currencyFormatterVerySmall.format(amount),
476
- locale,
477
- maximumDecimalTrailingZeroes
478
- );
479
- } else if (unsigned_amount < 1.0) {
480
- return formatCurrencyOverride(
481
- currencyFormatterSmall.format(amount),
482
- locale,
483
- maximumDecimalTrailingZeroes
484
- );
485
- } else if (isoCode === "JPY" && unsigned_amount < 100) {
486
- return formatCurrencyOverride(
487
- currencyFormatterTwoDecimal.format(amount),
488
- locale,
489
- maximumDecimalTrailingZeroes
490
- );
491
- } else if (unsigned_amount > NO_DECIMAL_THRESHOLD) {
492
- return formatCurrencyOverride(
493
- currencyFormatterNoDecimal.format(amount),
494
- locale
495
- );
496
- } else {
497
- // Let the formatter do what it seems best. In particular, we should not set any fraction amount for Japanese Yen
498
- return formatCurrencyOverride(
499
- currencyFormatterNormal.format(amount),
500
- locale
501
- );
502
- }
503
- }
504
- }
505
-
506
- export { clearCache, formatCurrency, isCrypto };