@coingecko/cryptoformat
Advanced tools
@@ -168,2 +168,19 @@ 'use strict'; | ||
| function generateAbbreviatedFormatter(isoCode, locale) { | ||
| // Show regular numbers if no Intl.NumberFormat support. | ||
| if (!IntlNumberFormatSupported()) { | ||
| return generateFallbackFormatter(isoCode, locale, 0); | ||
| } | ||
| let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 2 }; | ||
| // Currency symbol is supported if currency is Fiat/BTC/ETH. | ||
| if (!isCrypto(isoCode) || isBTCETH(isoCode)) { | ||
| numberFormatOptions.style = "currency"; | ||
| numberFormatOptions.currency = isoCode; | ||
| } | ||
| return new Intl.NumberFormat(locale, numberFormatOptions); | ||
| } | ||
| function generateFormatter(isoCode, locale, numDecimals, numSigFig) { | ||
@@ -191,2 +208,3 @@ const isNumberFormatSupported = IntlNumberFormatSupported(); | ||
| let currencyFormatter18DP; | ||
| let currencyFormatterAbbreviated; | ||
@@ -232,2 +250,5 @@ // If a page has to display multiple currencies, formatters would have to be created for each of them | ||
| : generateFormatter(isoCode, locale, 18); | ||
| currencyFormatterAbbreviated = cachedFormatter | ||
| ? cachedFormatter.currencyFormatterAbbreviated | ||
| : generateAbbreviatedFormatter(isoCode, locale); | ||
@@ -246,2 +267,3 @@ // Save in cache | ||
| formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP; | ||
| formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated; | ||
| } | ||
@@ -262,3 +284,4 @@ } | ||
| raw = false, | ||
| noDecimal = false | ||
| noDecimal = false, | ||
| abbreviated = false, | ||
| ) { | ||
@@ -275,2 +298,13 @@ isoCode = isoCode.toUpperCase(); | ||
| if (abbreviated) { | ||
| let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount); | ||
| // Manually add currency code to the back for non-BTC/ETH crypto currencies. | ||
| if (isCrypto(isoCode) && !isBTCETH(isoCode)) { | ||
| formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`; | ||
| } | ||
| return formatCurrencyOverride(formattedAbbreviatedCurrency, locale); | ||
| } | ||
| if (noDecimal === true && amount > 100.0) { | ||
@@ -277,0 +311,0 @@ return formatCurrencyOverride( |
@@ -164,2 +164,19 @@ // A map of supported currency codes to currency symbols. | ||
| function generateAbbreviatedFormatter(isoCode, locale) { | ||
| // Show regular numbers if no Intl.NumberFormat support. | ||
| if (!IntlNumberFormatSupported()) { | ||
| return generateFallbackFormatter(isoCode, locale, 0); | ||
| } | ||
| let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 2 }; | ||
| // Currency symbol is supported if currency is Fiat/BTC/ETH. | ||
| if (!isCrypto(isoCode) || isBTCETH(isoCode)) { | ||
| numberFormatOptions.style = "currency"; | ||
| numberFormatOptions.currency = isoCode; | ||
| } | ||
| return new Intl.NumberFormat(locale, numberFormatOptions); | ||
| } | ||
| function generateFormatter(isoCode, locale, numDecimals, numSigFig) { | ||
@@ -187,2 +204,3 @@ const isNumberFormatSupported = IntlNumberFormatSupported(); | ||
| let currencyFormatter18DP; | ||
| let currencyFormatterAbbreviated; | ||
@@ -228,2 +246,5 @@ // If a page has to display multiple currencies, formatters would have to be created for each of them | ||
| : generateFormatter(isoCode, locale, 18); | ||
| currencyFormatterAbbreviated = cachedFormatter | ||
| ? cachedFormatter.currencyFormatterAbbreviated | ||
| : generateAbbreviatedFormatter(isoCode, locale); | ||
@@ -242,2 +263,3 @@ // Save in cache | ||
| formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP; | ||
| formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated; | ||
| } | ||
@@ -258,3 +280,4 @@ } | ||
| raw = false, | ||
| noDecimal = false | ||
| noDecimal = false, | ||
| abbreviated = false, | ||
| ) { | ||
@@ -271,2 +294,13 @@ isoCode = isoCode.toUpperCase(); | ||
| if (abbreviated) { | ||
| let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount); | ||
| // Manually add currency code to the back for non-BTC/ETH crypto currencies. | ||
| if (isCrypto(isoCode) && !isBTCETH(isoCode)) { | ||
| formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`; | ||
| } | ||
| return formatCurrencyOverride(formattedAbbreviatedCurrency, locale); | ||
| } | ||
| if (noDecimal === true && amount > 100.0) { | ||
@@ -273,0 +307,0 @@ return formatCurrencyOverride( |
@@ -170,2 +170,19 @@ (function (global, factory) { | ||
| function generateAbbreviatedFormatter(isoCode, locale) { | ||
| // Show regular numbers if no Intl.NumberFormat support. | ||
| if (!IntlNumberFormatSupported()) { | ||
| return generateFallbackFormatter(isoCode, locale, 0); | ||
| } | ||
| let numberFormatOptions = { style: "decimal", notation: "compact", minimumFractionDigits: 0, maximumFractionDigits: 2 }; | ||
| // Currency symbol is supported if currency is Fiat/BTC/ETH. | ||
| if (!isCrypto(isoCode) || isBTCETH(isoCode)) { | ||
| numberFormatOptions.style = "currency"; | ||
| numberFormatOptions.currency = isoCode; | ||
| } | ||
| return new Intl.NumberFormat(locale, numberFormatOptions); | ||
| } | ||
| function generateFormatter(isoCode, locale, numDecimals, numSigFig) { | ||
@@ -193,2 +210,3 @@ const isNumberFormatSupported = IntlNumberFormatSupported(); | ||
| let currencyFormatter18DP; | ||
| let currencyFormatterAbbreviated; | ||
@@ -234,2 +252,5 @@ // If a page has to display multiple currencies, formatters would have to be created for each of them | ||
| : generateFormatter(isoCode, locale, 18); | ||
| currencyFormatterAbbreviated = cachedFormatter | ||
| ? cachedFormatter.currencyFormatterAbbreviated | ||
| : generateAbbreviatedFormatter(isoCode, locale); | ||
@@ -248,2 +269,3 @@ // Save in cache | ||
| formattersCache[cacheKey].currencyFormatter18DP = currencyFormatter18DP; | ||
| formattersCache[cacheKey].currencyFormatterAbbreviated = currencyFormatterAbbreviated; | ||
| } | ||
@@ -264,3 +286,4 @@ } | ||
| raw = false, | ||
| noDecimal = false | ||
| noDecimal = false, | ||
| abbreviated = false, | ||
| ) { | ||
@@ -277,2 +300,13 @@ isoCode = isoCode.toUpperCase(); | ||
| if (abbreviated) { | ||
| let formattedAbbreviatedCurrency = currencyFormatterAbbreviated.format(amount); | ||
| // Manually add currency code to the back for non-BTC/ETH crypto currencies. | ||
| if (isCrypto(isoCode) && !isBTCETH(isoCode)) { | ||
| formattedAbbreviatedCurrency = `${formattedAbbreviatedCurrency} ${isoCode}`; | ||
| } | ||
| return formatCurrencyOverride(formattedAbbreviatedCurrency, locale); | ||
| } | ||
| if (noDecimal === true && amount > 100.0) { | ||
@@ -279,0 +313,0 @@ return formatCurrencyOverride( |
+9
-1
@@ -27,2 +27,10 @@ export function IntlNumberFormatSupported(): boolean; | ||
| ): string; | ||
| export function formatCurrency( | ||
| amount: number, | ||
| isoCode: string, | ||
| locale: string, | ||
| raw: boolean, | ||
| noDecimal: boolean | decimalConfig, | ||
| abbreviated: boolean, | ||
| ): string; | ||
@@ -32,2 +40,2 @@ interface decimalConfig { | ||
| significantFigures?: number | ||
| } | ||
| } |
+1
-1
| { | ||
| "name": "@coingecko/cryptoformat", | ||
| "version": "0.5.4", | ||
| "version": "0.6.0", | ||
| "description": "Javascript library to format and display cryptocurrencies and fiat", | ||
@@ -5,0 +5,0 @@ "main": "lib/cryptoformat.cjs.js", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
51099
8.92%1347
7.16%0
-100%