@coingecko/cryptoformat 0.4.4 → 0.5.4

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.
@@ -185,6 +185,8 @@ let currencyFormatterTwoDecimal;
185
185
  let currencyFormatterSmall;
186
186
  let currencyFormatterVerySmall;
187
187
  let currencyFormatterVeryVerySmall;
188
+ let currencyFormatter15DP;
189
+ let currencyFormatter18DP;
188
190
 
189
191
  // If a page has to display multiple currencies, formatters would have to be created for each of them
190
192
  // To save some effort, we save formatters for reuse
@@ -219,6 +221,12 @@ function initializeFormatters(isoCode, locale) {
219
221
  currencyFormatterVeryVerySmall = cachedFormatter
220
222
  ? cachedFormatter.currencyFormatterVeryVerySmall
221
223
  : generateFormatter(isoCode, locale, 12);
224
+ currencyFormatter15DP = cachedFormatter
225
+ ? cachedFormatter.currencyFormatter15DP
226
+ : generateFormatter(isoCode, locale, 15);
227
+ currencyFormatter18DP = cachedFormatter
228
+ ? cachedFormatter.currencyFormatter18DP
229
+ : generateFormatter(isoCode, locale, 18);
222
230
 
223
231
  // Save in cache
224
232
  if (cachedFormatter == null) {
@@ -230,6 +238,8 @@ function initializeFormatters(isoCode, locale) {
230
238
  formattersCache[cacheKey].currencyFormatterSmall = currencyFormatterSmall;
231
239
  formattersCache[cacheKey].currencyFormatterVerySmall = currencyFormatterVerySmall;
232
240
  formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
241
+ formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
242
+ formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
233
243
  }
234
244
  }
235
245
 
@@ -345,19 +355,32 @@ function formatCurrency(
345
355
  currencyFormatterVerySmall.format(amount),
346
356
  locale
347
357
  );
348
- } else {
349
- // Crypto amount < 0.000001, show 12 fraction digits
358
+ } else if (price >= 10**-9 && price < 10**-6) {
350
359
  return formatCurrencyOverride(
351
360
  currencyFormatterVeryVerySmall.format(amount),
352
361
  locale
353
362
  );
363
+ } else if (price >= 10**-12 && price < 10**-9) {
364
+ return formatCurrencyOverride(
365
+ currencyFormatter15DP.format(amount),
366
+ locale
367
+ );
368
+ } else if (price < 10**-12) {
369
+ return formatCurrencyOverride(
370
+ currencyFormatter18DP.format(amount),
371
+ locale
372
+ );
354
373
  }
355
374
  } else {
356
375
  const unsigned_amount = Math.abs(amount);
357
376
  if (raw) {
358
- if (unsigned_amount < 0.00001) {
377
+ if (unsigned_amount < 10**-12) {
378
+ return amount.toFixed(18);
379
+ } else if (unsigned_amount < 10**-9) {
380
+ return amount.toFixed(15);
381
+ } else if (unsigned_amount < 10**-6) {
359
382
  return amount.toFixed(12);
360
- } else if (unsigned_amount < 0.001) {
383
+ } else if (unsigned_amount < 10**-3) {
361
384
  return amount.toFixed(8);
362
385
  } else if (unsigned_amount < 1.0) {
363
386
  return amount.toFixed(6);
@@ -371,7 +394,17 @@ function formatCurrency(
371
394
  currencyFormatterNormal.format(amount),
372
395
  locale
373
396
  );
374
- } else if (unsigned_amount < 0.000001) {
397
+ } else if (unsigned_amount < 10**-12) {
398
+ return formatCurrencyOverride(
399
+ currencyFormatter18DP.format(amount),
400
+ locale
401
+ );
402
+ } else if (unsigned_amount < 10**-9) {
403
+ return formatCurrencyOverride(
404
+ currencyFormatter15DP.format(amount),
405
+ locale
406
+ );
407
+ } else if (unsigned_amount < 10**-6) {
375
408
  return formatCurrencyOverride(
376
409
  currencyFormatterVeryVerySmall.format(amount),
377
410
  locale
@@ -181,6 +181,8 @@ let currencyFormatterTwoDecimal;
181
181
  let currencyFormatterSmall;
182
182
  let currencyFormatterVerySmall;
183
183
  let currencyFormatterVeryVerySmall;
184
+ let currencyFormatter15DP;
185
+ let currencyFormatter18DP;
184
186
 
185
187
  // If a page has to display multiple currencies, formatters would have to be created for each of them
186
188
  // To save some effort, we save formatters for reuse
@@ -215,6 +217,12 @@ function initializeFormatters(isoCode, locale) {
215
217
  currencyFormatterVeryVerySmall = cachedFormatter
216
218
  ? cachedFormatter.currencyFormatterVeryVerySmall
217
219
  : generateFormatter(isoCode, locale, 12);
220
+ currencyFormatter15DP = cachedFormatter
221
+ ? cachedFormatter.currencyFormatter15DP
222
+ : generateFormatter(isoCode, locale, 15);
223
+ currencyFormatter18DP = cachedFormatter
224
+ ? cachedFormatter.currencyFormatter18DP
225
+ : generateFormatter(isoCode, locale, 18);
218
226
 
219
227
  // Save in cache
220
228
  if (cachedFormatter == null) {
@@ -226,6 +234,8 @@ function initializeFormatters(isoCode, locale) {
226
234
  formattersCache[cacheKey].currencyFormatterSmall = currencyFormatterSmall;
227
235
  formattersCache[cacheKey].currencyFormatterVerySmall = currencyFormatterVerySmall;
228
236
  formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
237
+ formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
238
+ formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
229
239
  }
230
240
  }
231
241
 
@@ -341,19 +351,32 @@ function formatCurrency(
341
351
  currencyFormatterVerySmall.format(amount),
342
352
  locale
343
353
  );
344
- } else {
345
- // Crypto amount < 0.000001, show 12 fraction digits
354
+ } else if (price >= 10**-9 && price < 10**-6) {
346
355
  return formatCurrencyOverride(
347
356
  currencyFormatterVeryVerySmall.format(amount),
348
357
  locale
349
358
  );
359
+ } else if (price >= 10**-12 && price < 10**-9) {
360
+ return formatCurrencyOverride(
361
+ currencyFormatter15DP.format(amount),
362
+ locale
363
+ );
364
+ } else if (price < 10**-12) {
365
+ return formatCurrencyOverride(
366
+ currencyFormatter18DP.format(amount),
367
+ locale
368
+ );
350
369
  }
351
370
  } else {
352
371
  const unsigned_amount = Math.abs(amount);
353
372
  if (raw) {
354
- if (unsigned_amount < 0.00001) {
373
+ if (unsigned_amount < 10**-12) {
374
+ return amount.toFixed(18);
375
+ } else if (unsigned_amount < 10**-9) {
376
+ return amount.toFixed(15);
377
+ } else if (unsigned_amount < 10**-6) {
355
378
  return amount.toFixed(12);
356
- } else if (unsigned_amount < 0.001) {
379
+ } else if (unsigned_amount < 10**-3) {
357
380
  return amount.toFixed(8);
358
381
  } else if (unsigned_amount < 1.0) {
359
382
  return amount.toFixed(6);
@@ -367,7 +390,17 @@ function formatCurrency(
367
390
  currencyFormatterNormal.format(amount),
368
391
  locale
369
392
  );
370
- } else if (unsigned_amount < 0.000001) {
393
+ } else if (unsigned_amount < 10**-12) {
394
+ return formatCurrencyOverride(
395
+ currencyFormatter18DP.format(amount),
396
+ locale
397
+ );
398
+ } else if (unsigned_amount < 10**-9) {
399
+ return formatCurrencyOverride(
400
+ currencyFormatter15DP.format(amount),
401
+ locale
402
+ );
403
+ } else if (unsigned_amount < 10**-6) {
371
404
  return formatCurrencyOverride(
372
405
  currencyFormatterVeryVerySmall.format(amount),
373
406
  locale
@@ -187,6 +187,8 @@
187
187
  let currencyFormatterSmall;
188
188
  let currencyFormatterVerySmall;
189
189
  let currencyFormatterVeryVerySmall;
190
+ let currencyFormatter15DP;
191
+ let currencyFormatter18DP;
190
192
 
191
193
  // If a page has to display multiple currencies, formatters would have to be created for each of them
192
194
  // To save some effort, we save formatters for reuse
@@ -221,6 +223,12 @@
221
223
  currencyFormatterVeryVerySmall = cachedFormatter
222
224
  ? cachedFormatter.currencyFormatterVeryVerySmall
223
225
  : generateFormatter(isoCode, locale, 12);
226
+ currencyFormatter15DP = cachedFormatter
227
+ ? cachedFormatter.currencyFormatter15DP
228
+ : generateFormatter(isoCode, locale, 15);
229
+ currencyFormatter18DP = cachedFormatter
230
+ ? cachedFormatter.currencyFormatter18DP
231
+ : generateFormatter(isoCode, locale, 18);
224
232
 
225
233
  // Save in cache
226
234
  if (cachedFormatter == null) {
@@ -232,6 +240,8 @@
232
240
  formattersCache[cacheKey].currencyFormatterSmall = currencyFormatterSmall;
233
241
  formattersCache[cacheKey].currencyFormatterVerySmall = currencyFormatterVerySmall;
234
242
  formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
243
+ formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
244
+ formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
235
245
  }
236
246
  }
237
247
 
@@ -347,19 +357,32 @@
347
357
  currencyFormatterVerySmall.format(amount),
348
358
  locale
349
359
  );
350
- } else {
351
- // Crypto amount < 0.000001, show 12 fraction digits
360
+ } else if (price >= 10**-9 && price < 10**-6) {
352
361
  return formatCurrencyOverride(
353
362
  currencyFormatterVeryVerySmall.format(amount),
354
363
  locale
355
364
  );
365
+ } else if (price >= 10**-12 && price < 10**-9) {
366
+ return formatCurrencyOverride(
367
+ currencyFormatter15DP.format(amount),
368
+ locale
369
+ );
370
+ } else if (price < 10**-12) {
371
+ return formatCurrencyOverride(
372
+ currencyFormatter18DP.format(amount),
373
+ locale
374
+ );
356
375
  }
357
376
  } else {
358
377
  const unsigned_amount = Math.abs(amount);
359
378
  if (raw) {
360
- if (unsigned_amount < 0.00001) {
379
+ if (unsigned_amount < 10**-12) {
380
+ return amount.toFixed(18);
381
+ } else if (unsigned_amount < 10**-9) {
382
+ return amount.toFixed(15);
383
+ } else if (unsigned_amount < 10**-6) {
361
384
  return amount.toFixed(12);
362
- } else if (unsigned_amount < 0.001) {
385
+ } else if (unsigned_amount < 10**-3) {
363
386
  return amount.toFixed(8);
364
387
  } else if (unsigned_amount < 1.0) {
365
388
  return amount.toFixed(6);
@@ -373,7 +396,17 @@
373
396
  currencyFormatterNormal.format(amount),
374
397
  locale
375
398
  );
376
- } else if (unsigned_amount < 0.000001) {
399
+ } else if (unsigned_amount < 10**-12) {
400
+ return formatCurrencyOverride(
401
+ currencyFormatter18DP.format(amount),
402
+ locale
403
+ );
404
+ } else if (unsigned_amount < 10**-9) {
405
+ return formatCurrencyOverride(
406
+ currencyFormatter15DP.format(amount),
407
+ locale
408
+ );
409
+ } else if (unsigned_amount < 10**-6) {
377
410
  return formatCurrencyOverride(
378
411
  currencyFormatterVeryVerySmall.format(amount),
379
412
  locale
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coingecko/cryptoformat",
3
- "version": "0.4.4",
3
+ "version": "0.5.4",
4
4
  "description": "Javascript library to format and display cryptocurrencies and fiat",
5
5
  "main": "lib/cryptoformat.cjs.js",
6
6
  "module": "lib/cryptoformat.esm.js",