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