@coingecko/cryptoformat 0.8.0 → 0.8.2

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.
@@ -78,11 +78,11 @@ function isCrypto(isoCode) {
78
78
 
79
79
  // Function to transform decimal trailing zeroes to exponent
80
80
  function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) {
81
- const decimalTrailingZeroesPattern = new RegExp(`\\.(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
81
+ const decimalTrailingZeroesPattern = new RegExp(`(\\.|,)(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
82
82
 
83
83
  return formattedCurrency.replace(
84
84
  decimalTrailingZeroesPattern,
85
- (match, decimalTrailingZeroes) => `.0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
85
+ (_match, separator, decimalTrailingZeroes) => `${separator}0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
86
86
  )
87
87
  }
88
88
 
@@ -152,12 +152,12 @@ function generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig) {
152
152
  } catch (e) {
153
153
  // Unsupported currency, etc.
154
154
  // Use primitive fallback
155
- return generateFallbackFormatter(isoCode, locale, numDecimals);
155
+ return generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
156
156
  }
157
157
  }
158
158
 
159
159
  // Generates a primitive fallback formatter with no symbol support.
160
- function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
160
+ function generateFallbackFormatter(isoCode, locale, numDecimals = 2, maximumSignificantDigits = 4) {
161
161
  isoCode = isoCode.toUpperCase();
162
162
 
163
163
  if (numDecimals > 2) {
@@ -171,9 +171,15 @@ function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
171
171
  } else {
172
172
  return {
173
173
  format: (value) => {
174
+ let formattedValue = value;
175
+ // try using Intl.NumberFormat when possible to support max significant digits
176
+ try {
177
+ formattedValue = new Intl.NumberFormat(locale, { maximumSignificantDigits }).format(value);
178
+ } catch (e) {}
179
+
174
180
  return isCrypto(isoCode)
175
- ? `${value.toLocaleString(locale)} ${isoCode}`
176
- : `${isoCode} ${value.toLocaleString(locale)}`;
181
+ ? `${formattedValue.toLocaleString(locale)} ${isoCode}`
182
+ : `${isoCode} ${formattedValue.toLocaleString(locale)}`;
177
183
  },
178
184
  };
179
185
  }
@@ -203,7 +209,7 @@ function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
203
209
  isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
204
210
  return useIntlNumberFormatter
205
211
  ? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
206
- : generateFallbackFormatter(isoCode, locale, numDecimals);
212
+ : generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
207
213
  }
208
214
 
209
215
  // State variables
@@ -74,11 +74,11 @@ function isCrypto(isoCode) {
74
74
 
75
75
  // Function to transform decimal trailing zeroes to exponent
76
76
  function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) {
77
- const decimalTrailingZeroesPattern = new RegExp(`\\.(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
77
+ const decimalTrailingZeroesPattern = new RegExp(`(\\.|,)(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
78
78
 
79
79
  return formattedCurrency.replace(
80
80
  decimalTrailingZeroesPattern,
81
- (match, decimalTrailingZeroes) => `.0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
81
+ (_match, separator, decimalTrailingZeroes) => `${separator}0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
82
82
  )
83
83
  }
84
84
 
@@ -148,12 +148,12 @@ function generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig) {
148
148
  } catch (e) {
149
149
  // Unsupported currency, etc.
150
150
  // Use primitive fallback
151
- return generateFallbackFormatter(isoCode, locale, numDecimals);
151
+ return generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
152
152
  }
153
153
  }
154
154
 
155
155
  // Generates a primitive fallback formatter with no symbol support.
156
- function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
156
+ function generateFallbackFormatter(isoCode, locale, numDecimals = 2, maximumSignificantDigits = 4) {
157
157
  isoCode = isoCode.toUpperCase();
158
158
 
159
159
  if (numDecimals > 2) {
@@ -167,9 +167,15 @@ function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
167
167
  } else {
168
168
  return {
169
169
  format: (value) => {
170
+ let formattedValue = value;
171
+ // try using Intl.NumberFormat when possible to support max significant digits
172
+ try {
173
+ formattedValue = new Intl.NumberFormat(locale, { maximumSignificantDigits }).format(value);
174
+ } catch (e) {}
175
+
170
176
  return isCrypto(isoCode)
171
- ? `${value.toLocaleString(locale)} ${isoCode}`
172
- : `${isoCode} ${value.toLocaleString(locale)}`;
177
+ ? `${formattedValue.toLocaleString(locale)} ${isoCode}`
178
+ : `${isoCode} ${formattedValue.toLocaleString(locale)}`;
173
179
  },
174
180
  };
175
181
  }
@@ -199,7 +205,7 @@ function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
199
205
  isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
200
206
  return useIntlNumberFormatter
201
207
  ? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
202
- : generateFallbackFormatter(isoCode, locale, numDecimals);
208
+ : generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
203
209
  }
204
210
 
205
211
  // State variables
@@ -80,11 +80,11 @@
80
80
 
81
81
  // Function to transform decimal trailing zeroes to exponent
82
82
  function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) {
83
- const decimalTrailingZeroesPattern = new RegExp(`\\.(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
83
+ const decimalTrailingZeroesPattern = new RegExp(`(\\.|,)(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`);
84
84
 
85
85
  return formattedCurrency.replace(
86
86
  decimalTrailingZeroesPattern,
87
- (match, decimalTrailingZeroes) => `.0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
87
+ (_match, separator, decimalTrailingZeroes) => `${separator}0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`,
88
88
  )
89
89
  }
90
90
 
@@ -154,12 +154,12 @@
154
154
  } catch (e) {
155
155
  // Unsupported currency, etc.
156
156
  // Use primitive fallback
157
- return generateFallbackFormatter(isoCode, locale, numDecimals);
157
+ return generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
158
158
  }
159
159
  }
160
160
 
161
161
  // Generates a primitive fallback formatter with no symbol support.
162
- function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {
162
+ function generateFallbackFormatter(isoCode, locale, numDecimals = 2, maximumSignificantDigits = 4) {
163
163
  isoCode = isoCode.toUpperCase();
164
164
 
165
165
  if (numDecimals > 2) {
@@ -173,9 +173,15 @@
173
173
  } else {
174
174
  return {
175
175
  format: (value) => {
176
+ let formattedValue = value;
177
+ // try using Intl.NumberFormat when possible to support max significant digits
178
+ try {
179
+ formattedValue = new Intl.NumberFormat(locale, { maximumSignificantDigits }).format(value);
180
+ } catch (e) {}
181
+
176
182
  return isCrypto(isoCode)
177
- ? `${value.toLocaleString(locale)} ${isoCode}`
178
- : `${isoCode} ${value.toLocaleString(locale)}`;
183
+ ? `${formattedValue.toLocaleString(locale)} ${isoCode}`
184
+ : `${isoCode} ${formattedValue.toLocaleString(locale)}`;
179
185
  },
180
186
  };
181
187
  }
@@ -205,7 +211,7 @@
205
211
  isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
206
212
  return useIntlNumberFormatter
207
213
  ? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
208
- : generateFallbackFormatter(isoCode, locale, numDecimals);
214
+ : generateFallbackFormatter(isoCode, locale, numDecimals, numSigFig);
209
215
  }
210
216
 
211
217
  // State variables
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coingecko/cryptoformat",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
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",