forked from Heliobond/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
112 lines (102 loc) · 3.62 KB
/
Copy pathformat.ts
File metadata and controls
112 lines (102 loc) · 3.62 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
/*
* 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 pmounts (#369). */
export function roundToCents(value: number): number {
return roundToDecimals(value, 2)
}
/**
* Formats a number to a fixed number of decimals, rounding once with
* {@link 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 the vault share price with the shared precision used everywhere
* the figure appears (deposit preview, admin stat cell, data source) so the
* same value reads identically across screens (#394).
*/
export function formatSharePrice(value: number): string {
return formatDecimal(value, 4)
}
/**
* 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)
}
/** The shared number of decimals used for on-screen share prices. */
export const SHARE_PRICE_DECIMALS = 4
/** Formats a share price to the shared precision, rounding once. */
export function formatSharePrice(value: number): string {
return formatDecimal(value, SHARE_PRICE_DECIMALS)
}
/** Data shape for the landing pool counters. */
export interface PoolData {
totalAssets: number
projectsFunded: number
projectedRate: 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): {
totalAssets: string
projectsFunded: string
projectedRate: string
} {
return {
totalAssets: formatMoney(pool.totalAssets, { includeSymbol: true }),
projectsFunded: String(pool.projectsFunded),
projectedRate: formatDecimal(pool.projectedRate, 1),
}
}