Skip to content

Commit

Permalink
Merge pull request #29 from coingecko/0.4.0
Browse files Browse the repository at this point in the history
0.4.0
  • Loading branch information
superoo7 committed Sep 8, 2020
2 parents 7efd42c + 4f38e44 commit 4ebc0ef
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 108 deletions.
96 changes: 69 additions & 27 deletions lib/cryptoformat.cjs.js
Expand Up @@ -45,7 +45,7 @@ const supportedCurrencySymbols = {
TRY: "₺",
XAU: "XAU",
XAG: "XAG",
XDR: "XDR"
XDR: "XDR",
};

// A map of override objects to apply.
Expand All @@ -56,7 +56,7 @@ const symbolOverrides = {
SGD: { location: { start: true }, forLocales: { en: true } },
PHP: { location: { start: true }, forLocales: { en: true } },
BTC: { location: { start: true }, forLocales: { en: true } },
ETH: { location: { start: true }, forLocales: { en: true } }
ETH: { location: { start: true }, forLocales: { en: true } },
};

// Feature detection for Intl.NumberFormat
Expand Down Expand Up @@ -121,22 +121,25 @@ function formatCurrencyOverride(formattedCurrency, locale = "en") {
}

// Generates a formatter from Intl.NumberFormat
function generateIntlNumberFormatter(isoCode, locale, numDecimals) {
let formatter;
function generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig) {
try {
formatter = new Intl.NumberFormat(locale, {
const params = {
style: "currency",
currency: isoCode,
currencyDisplay: "symbol",
minimumFractionDigits: numDecimals,
maximumFractionDigits: numDecimals
});
};
if (numDecimals !== undefined) {
params.minimumFractionDigits = numDecimals;
params.maximumFractionDigits = numDecimals;
} else if (numSigFig !== undefined) {
params.maximumSignificantDigits = numSigFig;
}
return new Intl.NumberFormat(locale, params);
} catch (e) {
// Unsupported currency, etc.
// Use primitive fallback
return generateFallbackFormatter(isoCode, locale, numDecimals);
}
return formatter;
}

// Generates a primitive fallback formatter with no symbol support.
Expand All @@ -145,30 +148,30 @@ function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {

if (numDecimals > 2) {
return {
format: value => {
format: (value) => {
return isCrypto(isoCode)
? `${value.toFixed(numDecimals)} ${isoCode}`
: `${isoCode} ${value.toFixed(numDecimals)}`;
}
},
};
} else {
return {
format: value => {
format: (value) => {
return isCrypto(isoCode)
? `${value.toLocaleString(locale)} ${isoCode}`
: `${isoCode} ${value.toLocaleString(locale)}`;
}
},
};
}
}

function generateFormatter(isoCode, locale, numDecimals) {
function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
const isNumberFormatSupported = IntlNumberFormatSupported();

const useIntlNumberFormatter =
isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
return useIntlNumberFormatter
? generateIntlNumberFormatter(isoCode, locale, numDecimals)
? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
: generateFallbackFormatter(isoCode, locale, numDecimals);
}

Expand Down Expand Up @@ -236,7 +239,13 @@ const MEDIUM_CRYPTO_THRESHOLD = 50;
// Large crypto amount threshold
const LARGE_CRYPTO_THRESHOLD = 1000;

function formatCurrency(amount, isoCode, locale = "en", raw = false, noDecimal = false) {
function formatCurrency(
amount,
isoCode,
locale = "en",
raw = false,
noDecimal = false
) {
isoCode = isoCode.toUpperCase();

if (currentISOCode !== isoCode || currentLocale != locale) {
Expand All @@ -247,14 +256,54 @@ function formatCurrency(amount, isoCode, locale = "en", raw = false, noDecimal =
initializeFormatters(isoCode, locale);
}

if (isCrypto(isoCode)) {
let price = parseFloat(amount);
if (noDecimal === true && amount > 1.0) {
if (noDecimal === true && amount > 1.0) {
return formatCurrencyOverride(
currencyFormatterNoDecimal.format(amount),
locale
);
} else if (typeof noDecimal === "object" && noDecimal !== null) {
if (raw) {
// Limit to max n decimal places if applicable
let raw_amount = noDecimal.hasOwnProperty("dp")
? amount.toFixed(noDecimal.dp)
: amount;
// Round off to number of significant figures without trailing 0's
return `${parseFloat(raw_amount).toPrecision(noDecimal.sf) / 1}`;
} else if (
noDecimal.hasOwnProperty("dp") &&
noDecimal.hasOwnProperty("sf")
) {
// Show specified number of significant digits with cutoff of specified fraction digits
const currencyFormatterCustom = generateFormatter(
isoCode,
locale,
undefined,
noDecimal.sf
);

return formatCurrencyOverride(
currencyFormatterNoDecimal.format(amount),
currencyFormatterCustom.format(
Number.parseFloat(amount.toFixed(noDecimal.dp))
),
locale
);
} else {
const currencyFormatterCustom = generateFormatter(
isoCode,
locale,
noDecimal.dp,
noDecimal.sf
);

return formatCurrencyOverride(
currencyFormatterCustom.format(amount),
locale
);
}
}

if (isCrypto(isoCode)) {
let price = parseFloat(amount);

if (raw) {
if (amount === 0.0) {
Expand Down Expand Up @@ -297,13 +346,6 @@ function formatCurrency(amount, isoCode, locale = "en", raw = false, noDecimal =
);
}
} else {
if (noDecimal === true && amount > 1.0) {
return formatCurrencyOverride(
currencyFormatterNoDecimal.format(amount),
locale
);
}

const unsigned_amount = Math.abs(amount);
if (raw) {
if (unsigned_amount < 0.001) {
Expand Down
96 changes: 69 additions & 27 deletions lib/cryptoformat.esm.js
Expand Up @@ -41,7 +41,7 @@ const supportedCurrencySymbols = {
TRY: "₺",
XAU: "XAU",
XAG: "XAG",
XDR: "XDR"
XDR: "XDR",
};

// A map of override objects to apply.
Expand All @@ -52,7 +52,7 @@ const symbolOverrides = {
SGD: { location: { start: true }, forLocales: { en: true } },
PHP: { location: { start: true }, forLocales: { en: true } },
BTC: { location: { start: true }, forLocales: { en: true } },
ETH: { location: { start: true }, forLocales: { en: true } }
ETH: { location: { start: true }, forLocales: { en: true } },
};

// Feature detection for Intl.NumberFormat
Expand Down Expand Up @@ -117,22 +117,25 @@ function formatCurrencyOverride(formattedCurrency, locale = "en") {
}

// Generates a formatter from Intl.NumberFormat
function generateIntlNumberFormatter(isoCode, locale, numDecimals) {
let formatter;
function generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig) {
try {
formatter = new Intl.NumberFormat(locale, {
const params = {
style: "currency",
currency: isoCode,
currencyDisplay: "symbol",
minimumFractionDigits: numDecimals,
maximumFractionDigits: numDecimals
});
};
if (numDecimals !== undefined) {
params.minimumFractionDigits = numDecimals;
params.maximumFractionDigits = numDecimals;
} else if (numSigFig !== undefined) {
params.maximumSignificantDigits = numSigFig;
}
return new Intl.NumberFormat(locale, params);
} catch (e) {
// Unsupported currency, etc.
// Use primitive fallback
return generateFallbackFormatter(isoCode, locale, numDecimals);
}
return formatter;
}

// Generates a primitive fallback formatter with no symbol support.
Expand All @@ -141,30 +144,30 @@ function generateFallbackFormatter(isoCode, locale, numDecimals = 2) {

if (numDecimals > 2) {
return {
format: value => {
format: (value) => {
return isCrypto(isoCode)
? `${value.toFixed(numDecimals)} ${isoCode}`
: `${isoCode} ${value.toFixed(numDecimals)}`;
}
},
};
} else {
return {
format: value => {
format: (value) => {
return isCrypto(isoCode)
? `${value.toLocaleString(locale)} ${isoCode}`
: `${isoCode} ${value.toLocaleString(locale)}`;
}
},
};
}
}

function generateFormatter(isoCode, locale, numDecimals) {
function generateFormatter(isoCode, locale, numDecimals, numSigFig) {
const isNumberFormatSupported = IntlNumberFormatSupported();

const useIntlNumberFormatter =
isNumberFormatSupported && (!isCrypto(isoCode) || isBTCETH(isoCode));
return useIntlNumberFormatter
? generateIntlNumberFormatter(isoCode, locale, numDecimals)
? generateIntlNumberFormatter(isoCode, locale, numDecimals, numSigFig)
: generateFallbackFormatter(isoCode, locale, numDecimals);
}

Expand Down Expand Up @@ -232,7 +235,13 @@ const MEDIUM_CRYPTO_THRESHOLD = 50;
// Large crypto amount threshold
const LARGE_CRYPTO_THRESHOLD = 1000;

function formatCurrency(amount, isoCode, locale = "en", raw = false, noDecimal = false) {
function formatCurrency(
amount,
isoCode,
locale = "en",
raw = false,
noDecimal = false
) {
isoCode = isoCode.toUpperCase();

if (currentISOCode !== isoCode || currentLocale != locale) {
Expand All @@ -243,14 +252,54 @@ function formatCurrency(amount, isoCode, locale = "en", raw = false, noDecimal =
initializeFormatters(isoCode, locale);
}

if (isCrypto(isoCode)) {
let price = parseFloat(amount);
if (noDecimal === true && amount > 1.0) {
if (noDecimal === true && amount > 1.0) {
return formatCurrencyOverride(
currencyFormatterNoDecimal.format(amount),
locale
);
} else if (typeof noDecimal === "object" && noDecimal !== null) {
if (raw) {
// Limit to max n decimal places if applicable
let raw_amount = noDecimal.hasOwnProperty("dp")
? amount.toFixed(noDecimal.dp)
: amount;
// Round off to number of significant figures without trailing 0's
return `${parseFloat(raw_amount).toPrecision(noDecimal.sf) / 1}`;
} else if (
noDecimal.hasOwnProperty("dp") &&
noDecimal.hasOwnProperty("sf")
) {
// Show specified number of significant digits with cutoff of specified fraction digits
const currencyFormatterCustom = generateFormatter(
isoCode,
locale,
undefined,
noDecimal.sf
);

return formatCurrencyOverride(
currencyFormatterNoDecimal.format(amount),
currencyFormatterCustom.format(
Number.parseFloat(amount.toFixed(noDecimal.dp))
),
locale
);
} else {
const currencyFormatterCustom = generateFormatter(
isoCode,
locale,
noDecimal.dp,
noDecimal.sf
);

return formatCurrencyOverride(
currencyFormatterCustom.format(amount),
locale
);
}
}

if (isCrypto(isoCode)) {
let price = parseFloat(amount);

if (raw) {
if (amount === 0.0) {
Expand Down Expand Up @@ -293,13 +342,6 @@ function formatCurrency(amount, isoCode, locale = "en", raw = false, noDecimal =
);
}
} else {
if (noDecimal === true && amount > 1.0) {
return formatCurrencyOverride(
currencyFormatterNoDecimal.format(amount),
locale
);
}

const unsigned_amount = Math.abs(amount);
if (raw) {
if (unsigned_amount < 0.001) {
Expand Down

0 comments on commit 4ebc0ef

Please sign in to comment.