Repository navigation
Expand file tree
/
Copy pathformat.ts
More file actions
95 lines (88 loc) · 2.94 KB
/
Copy pathformat.ts
File metadata and controls
95 lines (88 loc) · 2.94 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
/**
* Rounds a number to a fixed number of decimals using decimal (not binary
* floating-point) precision, so 1.005 rounds to 1.01 rather than the 1.00 that
* `Math.round(1.005 * 100) / 100` or `(1.005).toFixed(2)` produce because
* 1.005 has no exact binary representation (#369).
*/
export function roundToDecimals(value: number, decimals: number): number {
const factor = Math.pow(10, decimals)
return Math.round((value + Number.EPSILON) * factor) / factor
}
/** Rounds to whole cents — the shared precision for on-screen USDC amounts (#369). */
export function roundToCents(value: number): number {
return roundToDecimals(value, 2)
}
/**
* Formats a number to a fixed number of decimals, rounding once with
* {roundToDecimals} first so every caller displays the same rounded
* value instead of re-rounding raw floating-point results independently (#369).
*/
export function formatDecimal(value: number, decimals: number): string {
return roundToDecimals(value, decimals).toFixed(decimals)
}
/**
* Formats a number as a localized currency/money string.
* Defaults to 'en-US' formatting.
*/
export function formatMoney(
amount: number,
options?: {
includeSymbol?: boolean
symbol?: string
locale?: string
},
): string {
const locale = options?.locale ?? 'en-US'
const formatted = amount.toLocaleString(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: 0,
})
if (options?.includeSymbol) {
const symbol = options.symbol ?? '$'
return `${symbol}${formatted}`
}
return formatted
}
/**
* Sanitizes input strings by removing non-numeric characters except a single decimal point,
* stripping leading zeros from the whole-number part.
*/
export function sanitizeAmount(val: string): string {
const clean = val.replace(/[^0-9.]/g, '')
const parts = clean.split('.')
const joined = parts.length > 1 ? parts[0] + '.' + parts.slice(1).join('') : clean
const [whole, ...rest] = joined.split('.')
const trimmedWhole = whole.replace(/^0+(?=\d), '')
return rest.length > 0 ? trimmedWhole + '.' + rest.join('.') : trimmedWhole
}
/**
* Parses an investment amount string into a rounded numeric float (2 decimal places).
* Consolidates parsing logic across forms (#417).
*/
export function parseAmount(value: string): number {
const cleaned = sanitizeAmount(value)
const num = parseFloat(cleaned)
return isNaN(num) ? 0 : roundToCents(num)
}
/** Data shape for the landing pool counters. */
export interface PoolData {
tvl: number
investors: number
apy: number
}
/**
* Formats the landing pool counters from the source data.
* This drives the live counters from `HB_DATA.pool` rather than
* hardcoded strings, preventing drift from the data source.
*/
export function formatPoolCounters(pool: PoolData): {
tvl: string
investors: string
apy: string
} {
return {
tvl: formatMoney(pool.tvl, { includeSymbol: true }),
investors: String(pool.investors),
apy: formatDecimal(pool.apy, 1),
}
}