Refactor truncateAddress to truncateMiddle(str, maxLen)
All checks were successful
check / check (push) Successful in 14s

Clean signature that takes a target length instead of leaking
the amount-length calculation into the function.
This commit is contained in:
2026-02-26 02:23:30 +07:00
parent 9abe570e43
commit c278be65ed

View File

@@ -65,14 +65,11 @@ function timeAgo(timestamp) {
return years + " year" + (years !== 1 ? "s" : "") + " ago";
}
function truncateAddress(address, amountLen) {
const excess = amountLen - 10;
if (excess <= 0) return address;
const charsToRemove = excess;
const available = address.length - charsToRemove - 1;
if (available < 10) return address;
const half = Math.floor(available / 2);
return address.slice(0, half) + "\u2026" + address.slice(-half);
function truncateMiddle(str, maxLen) {
if (str.length <= maxLen) return str;
if (maxLen < 5) return str.slice(0, maxLen);
const half = Math.floor((maxLen - 1) / 2);
return str.slice(0, half) + "\u2026" + str.slice(-(maxLen - 1 - half));
}
function escapeHtml(s) {
@@ -130,7 +127,8 @@ function renderTransactions(txs) {
line2.className = "flex justify-between";
const addr = document.createElement("span");
addr.className = "pr-2";
addr.textContent = truncateAddress(counterparty, amountStr.length);
const maxAddr = Math.max(10, 42 - Math.max(0, amountStr.length - 10));
addr.textContent = truncateMiddle(counterparty, maxAddr);
addr.title = counterparty;
const amount = document.createElement("span");
amount.className = "shrink-0";