forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.js
More file actions
253 lines (219 loc) · 7.73 KB
/
Copy pathcurrency.js
File metadata and controls
253 lines (219 loc) · 7.73 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { DEFAULT_CURRENCY, DEFAULT_LOCALE, FORMAT_CONFIG, INVALID_VALUE_FALLBACK } from "./config";
function normalizeNumericValue(value) {
if (typeof value === "number") {
return Number.isFinite(value) ? value : null;
}
if (typeof value === "string") {
const normalized = value.trim().replace(/,/g, "").replace(/%$/, "");
if (normalized === "") {
return null;
}
const numericValue = Number(normalized);
return Number.isFinite(numericValue) ? numericValue : null;
}
return null;
}
function createFormatter(locale, options) {
try {
return new Intl.NumberFormat(locale || FORMAT_CONFIG.defaultLocale, options);
} catch {
return new Intl.NumberFormat(FORMAT_CONFIG.defaultLocale, options);
}
}
/**
* Format a numeric value as currency using locale-aware grouping.
*
* Invalid, null, undefined, or NaN values return a display-safe fallback.
*
* @param {number|string|null|undefined} value
* @param {object} [options]
* @param {string} [options.currency=FORMAT_CONFIG.currency.currency]
* @param {string} [options.locale=FORMAT_CONFIG.currency.locale]
* @returns {string}
*/
export function formatCurrency(
value,
{ currency = FORMAT_CONFIG.currency.currency, locale = FORMAT_CONFIG.currency.locale } = {}
) {
const numericValue = normalizeNumericValue(value);
if (numericValue === null) {
return FORMAT_CONFIG.invalidValueFallback;
}
const currencyCode =
typeof currency === "string" && currency.trim()
? currency.trim().toUpperCase()
: FORMAT_CONFIG.currency.currency;
const maximumFractionDigits = Number.isInteger(numericValue)
? FORMAT_CONFIG.currency.integerMaximumFractionDigits
: FORMAT_CONFIG.currency.maximumFractionDigits;
try {
return createFormatter(locale, {
style: FORMAT_CONFIG.currency.style,
currency: currencyCode,
maximumFractionDigits,
}).format(numericValue);
} catch {
return createFormatter(locale, {
style: FORMAT_CONFIG.currency.style,
currency: FORMAT_CONFIG.currency.currency,
maximumFractionDigits,
}).format(numericValue);
}
}
/**
* Format a numeric amount with locale-aware grouping and no currency symbol.
*
* Invalid, null, undefined, or NaN values return a display-safe fallback.
*
* @param {number|string|null|undefined} value
* @param {object} [options]
* @param {string} [options.locale=FORMAT_CONFIG.amount.locale]
* @param {number} [options.minimumFractionDigits=FORMAT_CONFIG.amount.minimumFractionDigits]
* @param {number} [options.maximumFractionDigits=FORMAT_CONFIG.amount.maximumFractionDigits]
* @returns {string}
*/
export function formatAmount(
value,
{
locale = FORMAT_CONFIG.amount.locale,
minimumFractionDigits = FORMAT_CONFIG.amount.minimumFractionDigits,
maximumFractionDigits = FORMAT_CONFIG.amount.maximumFractionDigits,
} = {}
) {
const numericValue = normalizeNumericValue(value);
if (numericValue === null) {
return FORMAT_CONFIG.invalidValueFallback;
}
return createFormatter(locale, {
minimumFractionDigits,
maximumFractionDigits,
}).format(numericValue);
}
/**
* Format a currency value using compact notation for large amounts.
* Values >= 1,000 are abbreviated (1K, 1.2M, 3.4B, etc.).
* Values < 1,000 fall back to standard formatCurrency.
*
* @param {number|string|null|undefined} value
* @param {object} [options]
* @param {string} [options.currency=FORMAT_CONFIG.currency.currency]
* @param {string} [options.locale=FORMAT_CONFIG.currency.locale]
* @returns {string}
*/
export function formatCurrencyCompact(
value,
{ currency = FORMAT_CONFIG.currency.currency, locale = FORMAT_CONFIG.currency.locale } = {}
) {
const numericValue = normalizeNumericValue(value);
if (numericValue === null) return FORMAT_CONFIG.invalidValueFallback;
const abs = Math.abs(numericValue);
if (abs >= 1_000_000_000) {
const billions = numericValue / 1_000_000_000;
const formatted = createFormatter(locale, {
maximumFractionDigits: FORMAT_CONFIG.currency.maximumFractionDigits,
}).format(billions);
return `${formatted}B ${currency}`;
}
if (abs >= 1_000_000) {
const millions = numericValue / 1_000_000;
const formatted = createFormatter(locale, {
maximumFractionDigits: FORMAT_CONFIG.currency.maximumFractionDigits,
}).format(millions);
return `${formatted}M ${currency}`;
}
if (abs >= 1_000) {
const thousands = numericValue / 1_000;
const formatted = createFormatter(locale, {
maximumFractionDigits: FORMAT_CONFIG.currency.maximumFractionDigits,
}).format(thousands);
return `${formatted}K ${currency}`;
}
return formatCurrency(value, { currency, locale });
}
/**
* Format a numeric value as a percentage.
*
* Invalid, null, undefined, or NaN values return a display-safe fallback.
*
* @param {number|string|null|undefined} value
* @param {object} [options]
* @param {string} [options.locale=FORMAT_CONFIG.percentage.locale]
* @param {number} [options.minimumFractionDigits=FORMAT_CONFIG.percentage.minimumFractionDigits]
* @param {number} [options.maximumFractionDigits=FORMAT_CONFIG.percentage.maximumFractionDigits]
* @returns {string}
*/
export function formatPercent(
value,
{
locale = FORMAT_CONFIG.percentage.locale,
minimumFractionDigits = FORMAT_CONFIG.percentage.minimumFractionDigits,
maximumFractionDigits = FORMAT_CONFIG.percentage.maximumFractionDigits,
} = {}
) {
const numericValue = normalizeNumericValue(value);
if (numericValue === null) {
return FORMAT_CONFIG.invalidValueFallback;
}
const formattedNumber = createFormatter(locale, {
minimumFractionDigits,
maximumFractionDigits,
}).format(numericValue);
return `${formattedNumber}${FORMAT_CONFIG.percentage.suffix}`;
}
/**
* Format a raw wallet balance into compact display text and full-precision tooltip text.
*
* @param {number|string|null|undefined} balance
* @returns {{ compact: string, full: string }}
*/
export function formatWalletBalance(balance) {
if (balance === null || balance === undefined || balance === "") {
return { compact: INVALID_VALUE_FALLBACK, full: INVALID_VALUE_FALLBACK };
}
if (typeof balance === "number") {
if (!Number.isFinite(balance)) {
return { compact: INVALID_VALUE_FALLBACK, full: INVALID_VALUE_FALLBACK };
}
const full = formatAmount(balance);
const abs = Math.abs(balance);
let compact = full;
if (abs >= 1_000_000_000) {
compact = `${parseFloat((balance / 1_000_000_000).toFixed(2))}B`;
} else if (abs >= 1_000_000) {
compact = `${parseFloat((balance / 1_000_000).toFixed(2))}M`;
} else if (abs >= 1_000) {
compact = `${parseFloat((balance / 1_000).toFixed(2))}K`;
}
return { compact, full };
}
const str = String(balance).trim();
if (!str) {
return { compact: INVALID_VALUE_FALLBACK, full: INVALID_VALUE_FALLBACK };
}
const match = str.match(/^([+-]?[0-9,]+(?:\.[0-9]+)?)\s*(.*)$/);
if (!match) {
return { compact: str, full: str };
}
const rawNumStr = match[1];
const unit = match[2] ? match[2].trim() : "";
const num = Number(rawNumStr.replace(/,/g, ""));
if (!Number.isFinite(num)) {
return { compact: str, full: str };
}
const unitSuffix = unit ? ` ${unit}` : "";
const full = str;
const abs = Math.abs(num);
let compactNum = rawNumStr;
if (abs >= 1_000_000_000) {
compactNum = `${parseFloat((num / 1_000_000_000).toFixed(2))}B`;
} else if (abs >= 1_000_000) {
compactNum = `${parseFloat((num / 1_000_000).toFixed(2))}M`;
} else if (abs >= 1_000) {
compactNum = `${parseFloat((num / 1_000).toFixed(2))}K`;
} else {
compactNum = rawNumStr;
}
const compact = `${compactNum}${unitSuffix}`;
return { compact, full };
}
export { DEFAULT_CURRENCY, DEFAULT_LOCALE, FORMAT_CONFIG, INVALID_VALUE_FALLBACK };