Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add formatLargeValueCurrency #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,18 @@ export function formatCurrency(
amount: number,
isoCode: string,
locale: string,
raw: boolean
raw: false
): string;
export function formatCurrency(
amount: number,
isoCode: string,
locale: string,
raw: true
): number;

// format large value currency
export function formatLargeValueCurrency(
amount: number,
isoCode: string,
locale?: string
): string;
49 changes: 42 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
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 @@ -125,7 +125,7 @@ function generateIntlNumberFormatter(isoCode, locale, numDecimals) {
currency: isoCode,
currencyDisplay: "symbol",
minimumFractionDigits: numDecimals,
maximumFractionDigits: numDecimals
maximumFractionDigits: numDecimals,
});
} catch (e) {
// Unsupported currency, etc.
Expand All @@ -141,19 +141,19 @@ 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)}`;
}
},
};
}
}
Expand Down Expand Up @@ -331,3 +331,38 @@ export function formatCurrency(amount, isoCode, locale = "en", raw = false) {
}
}
}

export function formatLargeValueCurrency(amount, isoCode, locale = "en") {
isoCode = isoCode.toUpperCase();
const formattedAmount = formatNumber(amount);
if (isCrypto(isoCode)) {
const formatted = Number.parseFloat(formattedAmount.value).toFixed(3);
return isBTCETH(isoCode)
? supportedCurrencySymbols[isoCode] + formatted + formattedAmount.suffix
: formatted + formattedAmount.suffix + " " + isoCode;
} else {
const formatted = formatCurrency(formattedAmount.value, isoCode, locale);
return formatted + formattedAmount.suffix;
}
}

function formatNumber(val) {
if (Math.abs(Number(val)) >= 1.0e9) {
return {
suffix: "B",
value: Math.abs(Number(val)) / 1.0e9,
};
} else if (Math.abs(Number(val)) >= 1.0e6) {
return {
suffix: "M",
value: Math.abs(Number(val)) / 1.0e6,
};
} else if (Math.abs(Number(val)) >= 1.0e3) {
return {
suffix: "K",
value: Math.abs(Number(val)) / 1.0e3,
};
} else {
return { suffix: "", value: val };
}
}
43 changes: 43 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { formatCurrency, isCrypto, clearCache } from "./index";
import { formatLargeValueCurrency } from ".";

test("isCrypto", () => {
expect(isCrypto("BTC")).toBe(true);
Expand Down Expand Up @@ -166,3 +167,45 @@ describe("Format Currency correctly", () => {
expect(formatCurrency(32.034, "JPY", "en")).toEqual("¥32.03");
});
});

describe("Format large value currency", () => {
beforeAll(() => {
clearCache();
});

it("Formats thousands", () => {
expect(formatLargeValueCurrency(1234.02, "USD", "en")).toEqual("$1.23K");
expect(formatLargeValueCurrency(11234.02, "JPY", "en")).toEqual("¥11.23K");
expect(formatLargeValueCurrency(1234.5, "PHP", "en")).toEqual("₱1.23K");
expect(formatLargeValueCurrency(1234, "DOGE", "en")).toEqual("1.234K DOGE");
expect(formatLargeValueCurrency(1234, "BTC", "en")).toEqual("₿1.234K");
});

it("Formats millions", () => {
expect(formatLargeValueCurrency(1234567.02, "USD", "en")).toEqual("$1.23M");
expect(formatLargeValueCurrency(11234567.02, "JPY", "en")).toEqual(
"¥11.23M"
);
expect(formatLargeValueCurrency(1234567, "PHP", "en")).toEqual("₱1.23M");
expect(formatLargeValueCurrency(1234567, "DOGE", "en")).toEqual(
"1.235M DOGE"
);
expect(formatLargeValueCurrency(1234567, "BTC", "en")).toEqual("₿1.235M");
});

it("Formats billions", () => {
expect(formatLargeValueCurrency(1234567890.02, "USD", "en")).toEqual(
"$1.23B"
);
expect(formatLargeValueCurrency(11234567890.02, "JPY", "en")).toEqual(
"¥11.23B"
);
expect(formatLargeValueCurrency(1234567890, "PHP", "en")).toEqual("₱1.23B");
expect(formatLargeValueCurrency(1234567890, "DOGE", "en")).toEqual(
"1.235B DOGE"
);
expect(formatLargeValueCurrency(1234567890, "BTC", "en")).toEqual(
"₿1.235B"
);
});
});