-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
96 lines (83 loc) · 3.62 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { currencies } from "./config/currencies";
/**
* Formats the given amount according to the specified currency token and locale.
* @param {Object} options - Options for formatting the amount.
* @param {number|string} options.amount - The amount to format (as a number or string).
* @param {string} options.token - The currency token representing the currency.
* @param {boolean} [options.symbolToTheLeft=false] - Determines whether the currency symbol should be placed to the left of the formatted amount.
* @param {string} [options.locale="fr-FR"] - The locale used for formatting the amount. It can be either "fr-FR" or "en-US".
* @param {boolean} [options.showCurrencyCode=false] - Determines whether the currency code should be displayed alongside the formatted amount.
* @param {string} [options.negativeFormat="minus"] - Determines how negative numbers should be formatted. Possible values: "minus" or "parentheses". Default: "minus".
* @returns {string} The formatted amount with the currency symbol.
* @throws {Error} Throws an error if the currency token is unknown.
*/
export function asAmount({
amount,
token,
symbolToTheLeft = false,
locale = "fr-FR",
showCurrencyCode = false,
negativeFormat = "minus",
}: {
amount: number | string;
token: string;
symbolToTheLeft?: boolean;
locale?: "fr-FR" | "en-US";
showCurrencyCode?: boolean;
negativeFormat?: "minus" | "parentheses";
}): string {
// Retrieve currency information based on the provided token
const selectedCurrency = currencies[token];
// Throw an error if the currency token is unknown
if (typeof selectedCurrency === "undefined") {
throw new Error(`Unknown token ${token}`);
}
// Extract currency symbol and decimal precision
const currencySymbol = selectedCurrency.symbol;
const currencyDecimals = selectedCurrency.decimals;
// Calculate amount without decimals
const amountWithoutDecimals = Number(amount) / 10 ** currencyDecimals;
// Format the amountWithoutDecimals with thousands separator and fixed decimal places
let formattedAmount = amountWithoutDecimals.toLocaleString(locale, {
minimumFractionDigits: currencyDecimals,
maximumFractionDigits: currencyDecimals,
});
// Handle negative formatting
if (amountWithoutDecimals < 0) {
if (negativeFormat === "parentheses") {
formattedAmount = formattedAmount.replace("-", ""); // Remove minus sign
// Enclose amount in parentheses
if (symbolToTheLeft) {
return showCurrencyCode
? `(${token} ${formattedAmount})`
: `(${currencySymbol} ${formattedAmount})`;
}
return showCurrencyCode
? `(${formattedAmount} ${token})`
: `(${formattedAmount} ${currencySymbol})`;
}
}
if (symbolToTheLeft) {
return showCurrencyCode
? `${token} ${formattedAmount}`
: `${currencySymbol} ${formattedAmount}`;
}
return showCurrencyCode
? `${formattedAmount} ${token}`
: `${formattedAmount} ${currencySymbol}`;
}
/**
* Calculates the amount without decimals based on the specified currency token.
* @param {number} amount - The amount to convert to without decimals.
* @param {string} token - The currency token representing the currency.
* @returns {number} The amount without decimals.
* @throws {Error} Throws an error if the currency token is unknown.
*/
export function asAmountWithoutDecimals(amount: number, token: string): number {
const selectedCurrency = currencies[token];
if (typeof selectedCurrency === "undefined") {
throw new Error(`Unknown token ${token}`);
}
const currencyDecimals = selectedCurrency.decimals;
return Number(amount) * 10 ** currencyDecimals;
}