Shitaye-FrontEnd/src/lib/currency.ts

40 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export type CurrencyCode = "USD" | "EUR" | "GBP" | "AED";
export const CURRENCY_OPTIONS: { code: CurrencyCode; shortLabel: string }[] = [
{ code: "USD", shortLabel: "USD" },
{ code: "EUR", shortLabel: "EUR" },
{ code: "GBP", shortLabel: "GBP" },
{ code: "AED", shortLabel: "AED" },
];
/** Display amount = catalog USD × rate (illustrative mock rates). */
export const USD_TO: Record<CurrencyCode, number> = {
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";
}
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);
}