forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-address.ts
More file actions
36 lines (33 loc) · 1.12 KB
/
Copy pathformat-address.ts
File metadata and controls
36 lines (33 loc) · 1.12 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
/**
* Helpers for displaying Stellar public keys / addresses in UI surfaces
* where the full 56-character key would be visually overwhelming.
*/
const DEFAULT_LEAD = 6;
const DEFAULT_TAIL = 4;
/**
* Truncate a Stellar address to a `leading…trailing` form suitable for
* dense UI surfaces (badges, tooltips, table cells).
*
* Short strings shorter than `lead + tail` are returned untouched so the
* caller doesn't have to guard against odd inputs.
*
* @example
* shortenAddress('GABCDEF...XYZ123') // => 'GABCDE…Z123'
* shortenAddress('GABCDEF...XYZ123', 4,2) // => 'GABC…23'
*/
export function shortenAddress(
address: string,
lead: number = DEFAULT_LEAD,
tail: number = DEFAULT_TAIL,
): string {
if (!address) return '';
if (address.length <= lead + tail) return address;
return `${address.slice(0, lead)}…${address.slice(-tail)}`;
}
/**
* Crude shape check for Stellar G-addresses. Does not verify the checksum
* — callers that need full validation should use the Stellar SDK.
*/
export function looksLikeStellarAddress(address: string): boolean {
return /^G[A-Z2-7]{55}$/.test(address);
}