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