forked from Talenttrust/Talenttrust-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruncateAddress.ts
More file actions
21 lines (17 loc) · 870 Bytes
/
Copy pathtruncateAddress.ts
File metadata and controls
21 lines (17 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { isValidStellarAddress, normalizeStellarAddress } from './stellarAddress';
/**
* Truncates a Stellar address (or any string) by keeping the start and end, and inserting an ellipsis.
* The string is left untouched if its total length is at or below the threshold: `prefixLength + suffixLength + 3`.
* This prevents truncating strings that wouldn't actually be shortened.
*/
export function truncateAddress(value: string, prefixLength = 6, suffixLength = 4): string {
if (!value) {
return '';
}
const normalizedValue = normalizeStellarAddress(value);
const displayValue = isValidStellarAddress(normalizedValue) ? normalizedValue : value;
if (displayValue.length <= prefixLength + suffixLength + 3) {
return displayValue;
}
return `${displayValue.slice(0, prefixLength)}...${displayValue.slice(-suffixLength)}`;
}