From c278be65edc7f9de469db6e5b5183e3f32f25cc0 Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 26 Feb 2026 02:23:30 +0700 Subject: [PATCH] Refactor truncateAddress to truncateMiddle(str, maxLen) Clean signature that takes a target length instead of leaking the amount-length calculation into the function. --- src/popup/views/addressDetail.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/popup/views/addressDetail.js b/src/popup/views/addressDetail.js index b2770af..447fb94 100644 --- a/src/popup/views/addressDetail.js +++ b/src/popup/views/addressDetail.js @@ -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";