@coingecko/cryptoformat 0.5.4 → 0.7.0

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.
@@ -165,6 +165,23 @@ function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
165
165
  }
166
166
  }
167
167
 
168
+ function generateAbbreviatedFormatter(isoCode, locale) {
169
+ // Show regular numbers if no Intl.NumberFormat support.
170
+ if (!IntlNumberFormatSupported()) {
171
+ return generateFallbackFormatter(isoCode, locale, 0);
172
+ }
173
+
174
+ let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 3 };
175
+
176
+ // Currency symbol is supported if currency is Fiat/BTC/ETH.
177
+ if (!isCrypto(isoCode) || isBTCETH(isoCode)) {
178
+ numberFormatOptions.style = "currency";
179
+ numberFormatOptions.currency = isoCode;
180
+ }
181
+
182
+ return new Intl.NumberFormat(locale, numberFormatOptions);
183
+ }
184
+
168
185
  function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
169
186
  const isNumberFormatSupported = IntlNumberFormatSupported();
170
187
 
@@ -187,6 +204,7 @@ let currencyFormatterVerySmall;
187
204
  let currencyFormatterVeryVerySmall;
188
205
  let currencyFormatter15DP;
189
206
  let currencyFormatter18DP;
207
+ let currencyFormatterAbbreviated;
190
208
 
191
209
  // If a page has to display multiple currencies, formatters would have to be created for each of them
192
210
  // To save some effort, we save formatters for reuse
@@ -227,6 +245,9 @@ function initializeFormatters(isoCode, locale) {
227
245
  currencyFormatter18DP = cachedFormatter
228
246
  ? cachedFormatter.currencyFormatter18DP
229
247
  : generateFormatter(isoCode, locale, 18);
248
+ currencyFormatterAbbreviated = cachedFormatter
249
+ ? cachedFormatter.currencyFormatterAbbreviated
250
+ : generateAbbreviatedFormatter(isoCode, locale);
230
251
 
231
252
  // Save in cache
232
253
  if (cachedFormatter == null) {
@@ -240,6 +261,7 @@ function initializeFormatters(isoCode, locale) {
240
261
  formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
241
262
  formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
242
263
  formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
264
+ formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated;
243
265
  }
244
266
  }
245
267
 
@@ -255,7 +277,8 @@ function formatCurrency(
255
277
  isoCode,
256
278
  locale = "en",
257
279
  raw = false,
258
- noDecimal = false
280
+ noDecimal = false,
281
+ abbreviated = false,
259
282
  ) {
260
283
  isoCode = isoCode.toUpperCase();
261
284
 
@@ -267,6 +290,17 @@ function formatCurrency(
267
290
  initializeFormatters(isoCode, locale);
268
291
  }
269
292
 
293
+ if (abbreviated) {
294
+ let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount);
295
+
296
+ // Manually add currency code to the back for non-BTC/ETH crypto currencies.
297
+ if (isCrypto(isoCode) && !isBTCETH(isoCode)) {
298
+ formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`;
299
+ }
300
+
301
+ return formatCurrencyOverride(formattedAbbreviatedCurrency, locale);
302
+ }
303
+
270
304
  if (noDecimal === true && amount > 100.0) {
271
305
  return formatCurrencyOverride(
272
306
  currencyFormatterNoDecimal.format(amount),
@@ -161,6 +161,23 @@ function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
161
161
  }
162
162
  }
163
163
 
164
+ function generateAbbreviatedFormatter(isoCode, locale) {
165
+ // Show regular numbers if no Intl.NumberFormat support.
166
+ if (!IntlNumberFormatSupported()) {
167
+ return generateFallbackFormatter(isoCode, locale, 0);
168
+ }
169
+
170
+ let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 3 };
171
+
172
+ // Currency symbol is supported if currency is Fiat/BTC/ETH.
173
+ if (!isCrypto(isoCode) || isBTCETH(isoCode)) {
174
+ numberFormatOptions.style = "currency";
175
+ numberFormatOptions.currency = isoCode;
176
+ }
177
+
178
+ return new Intl.NumberFormat(locale, numberFormatOptions);
179
+ }
180
+
164
181
  function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
165
182
  const isNumberFormatSupported = IntlNumberFormatSupported();
166
183
 
@@ -183,6 +200,7 @@ let currencyFormatterVerySmall;
183
200
  let currencyFormatterVeryVerySmall;
184
201
  let currencyFormatter15DP;
185
202
  let currencyFormatter18DP;
203
+ let currencyFormatterAbbreviated;
186
204
 
187
205
  // If a page has to display multiple currencies, formatters would have to be created for each of them
188
206
  // To save some effort, we save formatters for reuse
@@ -223,6 +241,9 @@ function initializeFormatters(isoCode, locale) {
223
241
  currencyFormatter18DP = cachedFormatter
224
242
  ? cachedFormatter.currencyFormatter18DP
225
243
  : generateFormatter(isoCode, locale, 18);
244
+ currencyFormatterAbbreviated = cachedFormatter
245
+ ? cachedFormatter.currencyFormatterAbbreviated
246
+ : generateAbbreviatedFormatter(isoCode, locale);
226
247
 
227
248
  // Save in cache
228
249
  if (cachedFormatter == null) {
@@ -236,6 +257,7 @@ function initializeFormatters(isoCode, locale) {
236
257
  formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
237
258
  formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
238
259
  formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
260
+ formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated;
239
261
  }
240
262
  }
241
263
 
@@ -251,7 +273,8 @@ function formatCurrency(
251
273
  isoCode,
252
274
  locale = "en",
253
275
  raw = false,
254
- noDecimal = false
276
+ noDecimal = false,
277
+ abbreviated = false,
255
278
  ) {
256
279
  isoCode = isoCode.toUpperCase();
257
280
 
@@ -263,6 +286,17 @@ function formatCurrency(
263
286
  initializeFormatters(isoCode, locale);
264
287
  }
265
288
 
289
+ if (abbreviated) {
290
+ let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount);
291
+
292
+ // Manually add currency code to the back for non-BTC/ETH crypto currencies.
293
+ if (isCrypto(isoCode) && !isBTCETH(isoCode)) {
294
+ formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`;
295
+ }
296
+
297
+ return formatCurrencyOverride(formattedAbbreviatedCurrency, locale);
298
+ }
299
+
266
300
  if (noDecimal === true && amount > 100.0) {
267
301
  return formatCurrencyOverride(
268
302
  currencyFormatterNoDecimal.format(amount),
@@ -167,6 +167,23 @@
167
167
  }
168
168
  }
169
169
 
170
+ function generateAbbreviatedFormatter(isoCode, locale) {
171
+ // Show regular numbers if no Intl.NumberFormat support.
172
+ if (!IntlNumberFormatSupported()) {
173
+ return generateFallbackFormatter(isoCode, locale, 0);
174
+ }
175
+
176
+ let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 3 };
177
+
178
+ // Currency symbol is supported if currency is Fiat/BTC/ETH.
179
+ if (!isCrypto(isoCode) || isBTCETH(isoCode)) {
180
+ numberFormatOptions.style = "currency";
181
+ numberFormatOptions.currency = isoCode;
182
+ }
183
+
184
+ return new Intl.NumberFormat(locale, numberFormatOptions);
185
+ }
186
+
170
187
  function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
171
188
  const isNumberFormatSupported = IntlNumberFormatSupported();
172
189
 
@@ -189,6 +206,7 @@
189
206
  let currencyFormatterVeryVerySmall;
190
207
  let currencyFormatter15DP;
191
208
  let currencyFormatter18DP;
209
+ let currencyFormatterAbbreviated;
192
210
 
193
211
  // If a page has to display multiple currencies, formatters would have to be created for each of them
194
212
  // To save some effort, we save formatters for reuse
@@ -229,6 +247,9 @@
229
247
  currencyFormatter18DP = cachedFormatter
230
248
  ? cachedFormatter.currencyFormatter18DP
231
249
  : generateFormatter(isoCode, locale, 18);
250
+ currencyFormatterAbbreviated = cachedFormatter
251
+ ? cachedFormatter.currencyFormatterAbbreviated
252
+ : generateAbbreviatedFormatter(isoCode, locale);
232
253
 
233
254
  // Save in cache
234
255
  if (cachedFormatter == null) {
@@ -242,6 +263,7 @@
242
263
  formattersCache[cacheKey].currencyFormatterVeryVerySmall = currencyFormatterVeryVerySmall;
243
264
  formattersCache[cacheKey].currencyFormatter15DP = currencyFormatter15DP;
244
265
  formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP;
266
+ formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated;
245
267
  }
246
268
  }
247
269
 
@@ -257,7 +279,8 @@
257
279
  isoCode,
258
280
  locale = "en",
259
281
  raw = false,
260
- noDecimal = false
282
+ noDecimal = false,
283
+ abbreviated = false,
261
284
  ) {
262
285
  isoCode = isoCode.toUpperCase();
263
286
 
@@ -269,6 +292,17 @@
269
292
  initializeFormatters(isoCode, locale);
270
293
  }
271
294
 
295
+ if (abbreviated) {
296
+ let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount);
297
+
298
+ // Manually add currency code to the back for non-BTC/ETH crypto currencies.
299
+ if (isCrypto(isoCode) && !isBTCETH(isoCode)) {
300
+ formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`;
301
+ }
302
+
303
+ return formatCurrencyOverride(formattedAbbreviatedCurrency, locale);
304
+ }
305
+
272
306
  if (noDecimal === true && amount > 100.0) {
273
307
  return formatCurrencyOverride(
274
308
  currencyFormatterNoDecimal.format(amount),
package/lib/index.d.ts CHANGED
@@ -24,8 +24,16 @@ export function formatCurrency(
24
24
  raw: boolean,
25
25
  noDecimal: boolean | decimalConfig
26
26
  ): string;
27
+ export function formatCurrency(
28
+ amount: number,
29
+ isoCode: string,
30
+ locale: string,
31
+ raw: boolean,
32
+ noDecimal: boolean | decimalConfig,
33
+ abbreviated: boolean,
34
+ ): string;
27
35
 
28
36
  interface decimalConfig {
29
37
  decimalPlaces?: number,
30
38
  significantFigures?: number
31
- }
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coingecko/cryptoformat",
3
- "version": "0.5.4",
3
+ "version": "0.7.0",
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",