forked from Disciplr-Org/Disciplr-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusdcInput.ts
More file actions
73 lines (60 loc) · 2.17 KB
/
Copy pathusdcInput.ts
File metadata and controls
73 lines (60 loc) · 2.17 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
/**
* Formats a raw USDC amount string for display.
* - Adds thousands grouping (commas) on the integer part
* - Caps decimal places at 7
* - Preserves a trailing dot so the user can continue typing decimals
*/
export function formatUsdcInput(raw: string): string {
if (!raw) return "";
const dotIndex = raw.indexOf(".");
const integerPart = dotIndex === -1 ? raw : raw.slice(0, dotIndex);
let decimalPart = dotIndex === -1 ? "" : raw.slice(dotIndex + 1);
// Cap decimals at 7
if (decimalPart.length > 7) {
decimalPart = decimalPart.slice(0, 7);
}
// Add thousands grouping
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// If raw ends with ".", preserve the decimal point (user is typing decimals)
if (raw.endsWith(".")) {
return formattedInteger + ".";
}
if (decimalPart) {
return formattedInteger + "." + decimalPart;
}
return formattedInteger;
}
/**
* Parses a display USDC amount string (possibly with commas, stray characters)
* into a clean raw number string.
* - Strips all non-digit / non-dot characters
* - Keeps only the first dot (subsequent dots are removed)
* - Caps decimals at 7
* - Normalises leading zeros ("00" → "0", "01" → "1", but keeps "0.")
*/
export function parseUsdcInput(text: string): string {
if (!text) return "";
// Remove everything except digits and dots
let cleaned = text.replace(/[^0-9.]/g, "");
// Keep only the first dot
const parts = cleaned.split(".");
cleaned = parts.shift()! + (parts.length ? "." + parts.join("") : "");
// Cap decimals at 7
const dotIndex = cleaned.indexOf(".");
if (dotIndex !== -1 && cleaned.length - dotIndex - 1 > 7) {
cleaned = cleaned.slice(0, dotIndex + 8);
}
// If the integer part is empty, prepend "0" (e.g. ".5" → "0.5")
if (cleaned.startsWith(".")) {
cleaned = "0" + cleaned;
}
// Normalize leading zeros on the integer part
// e.g. "00" → "0", "01" → "1", but "0." → "0."
if (cleaned.startsWith("0") && cleaned.length > 1 && cleaned[1] !== ".") {
cleaned = cleaned.replace(/^0+/, "");
if (cleaned === "" || cleaned.startsWith(".")) {
cleaned = "0" + cleaned;
}
}
return cleaned;
}