@coingecko/cryptoformat
Advanced tools
+56
-22
@@ -79,4 +79,18 @@ 'use strict'; | ||
| // Function to transform decimal trailing zeroes to exponent | ||
| function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) { | ||
| const decimalTrailingZeroesPattern = new RegExp(`\\.(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`); | ||
| return formattedCurrency.replace( | ||
| decimalTrailingZeroesPattern, | ||
| (match, decimalTrailingZeroes) => `.0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`, | ||
| ) | ||
| } | ||
| // Function to transform the output from Intl.NumberFormat#format | ||
| function formatCurrencyOverride(formattedCurrency, locale = "en") { | ||
| function formatCurrencyOverride(formattedCurrency, locale = "en", maximumDecimalTrailingZeroes) { | ||
| if (typeof maximumDecimalTrailingZeroes !== "undefined") { | ||
| formattedCurrency = decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes); | ||
| } | ||
| // If currency code remains in front | ||
@@ -285,2 +299,3 @@ const currencyCodeFrontMatch = formattedCurrency.match(/^[A-Z]{3}\s?/); | ||
| isoCode = isoCode.toUpperCase(); | ||
| let maximumDecimalTrailingZeroes = undefined; | ||
@@ -319,6 +334,9 @@ if (currentISOCode !== isoCode || currentLocale != locale) { | ||
| return `${parseFloat(raw_amount).toPrecision(noDecimal.significantFigures) / 1}`; | ||
| } else if ( | ||
| noDecimal.hasOwnProperty("decimalPlaces") && | ||
| noDecimal.hasOwnProperty("significantFigures") | ||
| ) { | ||
| } | ||
| if (noDecimal.hasOwnProperty("maximumDecimalTrailingZeroes")) { | ||
| maximumDecimalTrailingZeroes = noDecimal.maximumDecimalTrailingZeroes; | ||
| } | ||
| if (noDecimal.hasOwnProperty("decimalPlaces") && noDecimal.hasOwnProperty("significantFigures")) { | ||
| // Show specified number of significant digits with cutoff of specified fraction digits | ||
@@ -336,5 +354,6 @@ const currencyFormatterCustom = generateFormatter( | ||
| ), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
| } else { | ||
| } else if (noDecimal.hasOwnProperty("decimalPlaces") || noDecimal.hasOwnProperty("significantFigures")) { | ||
| const currencyFormatterCustom = generateFormatter( | ||
@@ -349,3 +368,4 @@ isoCode, | ||
| currencyFormatterCustom.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -368,3 +388,4 @@ } | ||
| currencyFormatterNormal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -384,3 +405,4 @@ } else if (price >= LARGE_CRYPTO_THRESHOLD) { | ||
| currencyFormatterMedium.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -391,3 +413,4 @@ } else if (price >= 1.0 && price < MEDIUM_CRYPTO_THRESHOLD) { | ||
| currencyFormatterSmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -398,3 +421,4 @@ } else if (price >= 0.000001 && price < 1.0) { | ||
| currencyFormatterVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -404,3 +428,4 @@ } else if (price >= 10**-9 && price < 10**-6) { | ||
| currencyFormatterVeryVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -410,3 +435,4 @@ } else if (price >= 10**-12 && price < 10**-9) { | ||
| currencyFormatter15DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -416,3 +442,4 @@ } else if (price < 10**-12) { | ||
| currencyFormatter18DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -441,3 +468,4 @@ } | ||
| currencyFormatterNormal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -447,3 +475,4 @@ } else if (unsigned_amount < 10**-12) { | ||
| currencyFormatter18DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -453,3 +482,4 @@ } else if (unsigned_amount < 10**-9) { | ||
| currencyFormatter15DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -459,3 +489,4 @@ } else if (unsigned_amount < 10**-6) { | ||
| currencyFormatterVeryVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -465,3 +496,4 @@ } else if (unsigned_amount < 0.05) { | ||
| currencyFormatterVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -471,3 +503,4 @@ } else if (unsigned_amount < 1.0) { | ||
| currencyFormatterSmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -477,3 +510,4 @@ } else if (isoCode === "JPY" && unsigned_amount < 100) { | ||
| currencyFormatterTwoDecimal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -480,0 +514,0 @@ } else if (unsigned_amount > NO_DECIMAL_THRESHOLD) { |
+56
-22
@@ -75,4 +75,18 @@ // A map of supported currency codes to currency symbols. | ||
| // Function to transform decimal trailing zeroes to exponent | ||
| function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) { | ||
| const decimalTrailingZeroesPattern = new RegExp(`\\.(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`); | ||
| return formattedCurrency.replace( | ||
| decimalTrailingZeroesPattern, | ||
| (match, decimalTrailingZeroes) => `.0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`, | ||
| ) | ||
| } | ||
| // Function to transform the output from Intl.NumberFormat#format | ||
| function formatCurrencyOverride(formattedCurrency, locale = "en") { | ||
| function formatCurrencyOverride(formattedCurrency, locale = "en", maximumDecimalTrailingZeroes) { | ||
| if (typeof maximumDecimalTrailingZeroes !== "undefined") { | ||
| formattedCurrency = decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes); | ||
| } | ||
| // If currency code remains in front | ||
@@ -281,2 +295,3 @@ const currencyCodeFrontMatch = formattedCurrency.match(/^[A-Z]{3}\s?/); | ||
| isoCode = isoCode.toUpperCase(); | ||
| let maximumDecimalTrailingZeroes = undefined; | ||
@@ -315,6 +330,9 @@ if (currentISOCode !== isoCode || currentLocale != locale) { | ||
| return `${parseFloat(raw_amount).toPrecision(noDecimal.significantFigures) / 1}`; | ||
| } else if ( | ||
| noDecimal.hasOwnProperty("decimalPlaces") && | ||
| noDecimal.hasOwnProperty("significantFigures") | ||
| ) { | ||
| } | ||
| if (noDecimal.hasOwnProperty("maximumDecimalTrailingZeroes")) { | ||
| maximumDecimalTrailingZeroes = noDecimal.maximumDecimalTrailingZeroes; | ||
| } | ||
| if (noDecimal.hasOwnProperty("decimalPlaces") && noDecimal.hasOwnProperty("significantFigures")) { | ||
| // Show specified number of significant digits with cutoff of specified fraction digits | ||
@@ -332,5 +350,6 @@ const currencyFormatterCustom = generateFormatter( | ||
| ), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
| } else { | ||
| } else if (noDecimal.hasOwnProperty("decimalPlaces") || noDecimal.hasOwnProperty("significantFigures")) { | ||
| const currencyFormatterCustom = generateFormatter( | ||
@@ -345,3 +364,4 @@ isoCode, | ||
| currencyFormatterCustom.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -364,3 +384,4 @@ } | ||
| currencyFormatterNormal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -380,3 +401,4 @@ } else if (price >= LARGE_CRYPTO_THRESHOLD) { | ||
| currencyFormatterMedium.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -387,3 +409,4 @@ } else if (price >= 1.0 && price < MEDIUM_CRYPTO_THRESHOLD) { | ||
| currencyFormatterSmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -394,3 +417,4 @@ } else if (price >= 0.000001 && price < 1.0) { | ||
| currencyFormatterVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -400,3 +424,4 @@ } else if (price >= 10**-9 && price < 10**-6) { | ||
| currencyFormatterVeryVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -406,3 +431,4 @@ } else if (price >= 10**-12 && price < 10**-9) { | ||
| currencyFormatter15DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -412,3 +438,4 @@ } else if (price < 10**-12) { | ||
| currencyFormatter18DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -437,3 +464,4 @@ } | ||
| currencyFormatterNormal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -443,3 +471,4 @@ } else if (unsigned_amount < 10**-12) { | ||
| currencyFormatter18DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -449,3 +478,4 @@ } else if (unsigned_amount < 10**-9) { | ||
| currencyFormatter15DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -455,3 +485,4 @@ } else if (unsigned_amount < 10**-6) { | ||
| currencyFormatterVeryVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -461,3 +492,4 @@ } else if (unsigned_amount < 0.05) { | ||
| currencyFormatterVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -467,3 +499,4 @@ } else if (unsigned_amount < 1.0) { | ||
| currencyFormatterSmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -473,3 +506,4 @@ } else if (isoCode === "JPY" && unsigned_amount < 100) { | ||
| currencyFormatterTwoDecimal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -476,0 +510,0 @@ } else if (unsigned_amount > NO_DECIMAL_THRESHOLD) { |
+56
-22
@@ -81,4 +81,18 @@ (function (global, factory) { | ||
| // Function to transform decimal trailing zeroes to exponent | ||
| function decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes) { | ||
| const decimalTrailingZeroesPattern = new RegExp(`\\.(0{${maximumDecimalTrailingZeroes + 1},})(?=[1-9]?)`); | ||
| return formattedCurrency.replace( | ||
| decimalTrailingZeroesPattern, | ||
| (match, decimalTrailingZeroes) => `.0<sub title=\"${formattedCurrency}\">${decimalTrailingZeroes.length}</sub>`, | ||
| ) | ||
| } | ||
| // Function to transform the output from Intl.NumberFormat#format | ||
| function formatCurrencyOverride(formattedCurrency, locale = "en") { | ||
| function formatCurrencyOverride(formattedCurrency, locale = "en", maximumDecimalTrailingZeroes) { | ||
| if (typeof maximumDecimalTrailingZeroes !== "undefined") { | ||
| formattedCurrency = decimalTrailingZeroesToExponent(formattedCurrency, maximumDecimalTrailingZeroes); | ||
| } | ||
| // If currency code remains in front | ||
@@ -287,2 +301,3 @@ const currencyCodeFrontMatch = formattedCurrency.match(/^[A-Z]{3}\s?/); | ||
| isoCode = isoCode.toUpperCase(); | ||
| let maximumDecimalTrailingZeroes = undefined; | ||
@@ -321,6 +336,9 @@ if (currentISOCode !== isoCode || currentLocale != locale) { | ||
| return `${parseFloat(raw_amount).toPrecision(noDecimal.significantFigures) / 1}`; | ||
| } else if ( | ||
| noDecimal.hasOwnProperty("decimalPlaces") && | ||
| noDecimal.hasOwnProperty("significantFigures") | ||
| ) { | ||
| } | ||
| if (noDecimal.hasOwnProperty("maximumDecimalTrailingZeroes")) { | ||
| maximumDecimalTrailingZeroes = noDecimal.maximumDecimalTrailingZeroes; | ||
| } | ||
| if (noDecimal.hasOwnProperty("decimalPlaces") && noDecimal.hasOwnProperty("significantFigures")) { | ||
| // Show specified number of significant digits with cutoff of specified fraction digits | ||
@@ -338,5 +356,6 @@ const currencyFormatterCustom = generateFormatter( | ||
| ), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
| } else { | ||
| } else if (noDecimal.hasOwnProperty("decimalPlaces") || noDecimal.hasOwnProperty("significantFigures")) { | ||
| const currencyFormatterCustom = generateFormatter( | ||
@@ -351,3 +370,4 @@ isoCode, | ||
| currencyFormatterCustom.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -370,3 +390,4 @@ } | ||
| currencyFormatterNormal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -386,3 +407,4 @@ } else if (price >= LARGE_CRYPTO_THRESHOLD) { | ||
| currencyFormatterMedium.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -393,3 +415,4 @@ } else if (price >= 1.0 && price < MEDIUM_CRYPTO_THRESHOLD) { | ||
| currencyFormatterSmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -400,3 +423,4 @@ } else if (price >= 0.000001 && price < 1.0) { | ||
| currencyFormatterVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -406,3 +430,4 @@ } else if (price >= 10**-9 && price < 10**-6) { | ||
| currencyFormatterVeryVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -412,3 +437,4 @@ } else if (price >= 10**-12 && price < 10**-9) { | ||
| currencyFormatter15DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -418,3 +444,4 @@ } else if (price < 10**-12) { | ||
| currencyFormatter18DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -443,3 +470,4 @@ } | ||
| currencyFormatterNormal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -449,3 +477,4 @@ } else if (unsigned_amount < 10**-12) { | ||
| currencyFormatter18DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -455,3 +484,4 @@ } else if (unsigned_amount < 10**-9) { | ||
| currencyFormatter15DP.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -461,3 +491,4 @@ } else if (unsigned_amount < 10**-6) { | ||
| currencyFormatterVeryVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -467,3 +498,4 @@ } else if (unsigned_amount < 0.05) { | ||
| currencyFormatterVerySmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -473,3 +505,4 @@ } else if (unsigned_amount < 1.0) { | ||
| currencyFormatterSmall.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -479,3 +512,4 @@ } else if (isoCode === "JPY" && unsigned_amount < 100) { | ||
| currencyFormatterTwoDecimal.format(amount), | ||
| locale | ||
| locale, | ||
| maximumDecimalTrailingZeroes | ||
| ); | ||
@@ -482,0 +516,0 @@ } else if (unsigned_amount > NO_DECIMAL_THRESHOLD) { |
+3
-2
@@ -37,4 +37,5 @@ export function IntlNumberFormatSupported(): boolean; | ||
| interface decimalConfig { | ||
| decimalPlaces?: number, | ||
| significantFigures?: number | ||
| decimalPlaces?: number, | ||
| significantFigures?: number, | ||
| maximumDecimalTrailingZeroes?: number | ||
| } |
+1
-1
| { | ||
| "name": "@coingecko/cryptoformat", | ||
| "version": "0.7.0", | ||
| "version": "0.8.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
55846
9.29%1435
6.53%0
-100%