41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
export type CurrencyCode = "USD" | "EUR" | "GBP" | "AED" | "ETB";
|
|
|
|
export const CURRENCY_OPTIONS: { code: CurrencyCode; shortLabel: string }[] = [
|
|
{ code: "ETB", shortLabel: "ETB" },
|
|
{ code: "USD", shortLabel: "USD" },
|
|
{ code: "EUR", shortLabel: "EUR" },
|
|
{ code: "GBP", shortLabel: "GBP" },
|
|
{ code: "AED", shortLabel: "AED" },
|
|
];
|
|
|
|
export const USD_TO: Record<CurrencyCode, number> = {
|
|
ETB: 1,
|
|
USD: 1,
|
|
EUR: 0.93,
|
|
GBP: 0.79,
|
|
AED: 3.67,
|
|
};
|
|
|
|
export function isCurrencyCode(v: string): v is CurrencyCode {
|
|
return v === "USD" || v === "EUR" || v === "GBP" || v === "AED" || v === "ETB";
|
|
}
|
|
|
|
export function convertFromUsd(usd: number, code: CurrencyCode): number {
|
|
const n = usd * USD_TO[code];
|
|
return Math.round(n * 100) / 100;
|
|
}
|
|
|
|
export function formatMoneyFromUsd(
|
|
usd: number,
|
|
code: CurrencyCode,
|
|
maximumFractionDigits: 0 | 1 | 2 = 2,
|
|
): string {
|
|
const amount = convertFromUsd(usd, code);
|
|
return new Intl.NumberFormat("en-US", {
|
|
style: "currency",
|
|
currency: code,
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits,
|
|
}).format(amount);
|
|
}
|