Refactor truncateAddress to truncateMiddle(str, maxLen)
All checks were successful
check / check (push) Successful in 14s
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:
@@ -65,14 +65,11 @@ function timeAgo(timestamp) {
|
|||||||
return years + " year" + (years !== 1 ? "s" : "") + " ago";
|
return years + " year" + (years !== 1 ? "s" : "") + " ago";
|
||||||
}
|
}
|
||||||
|
|
||||||
function truncateAddress(address, amountLen) {
|
function truncateMiddle(str, maxLen) {
|
||||||
const excess = amountLen - 10;
|
if (str.length <= maxLen) return str;
|
||||||
if (excess <= 0) return address;
|
if (maxLen < 5) return str.slice(0, maxLen);
|
||||||
const charsToRemove = excess;
|
const half = Math.floor((maxLen - 1) / 2);
|
||||||
const available = address.length - charsToRemove - 1;
|
return str.slice(0, half) + "\u2026" + str.slice(-(maxLen - 1 - half));
|
||||||
if (available < 10) return address;
|
|
||||||
const half = Math.floor(available / 2);
|
|
||||||
return address.slice(0, half) + "\u2026" + address.slice(-half);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeHtml(s) {
|
function escapeHtml(s) {
|
||||||
@@ -130,7 +127,8 @@ function renderTransactions(txs) {
|
|||||||
line2.className = "flex justify-between";
|
line2.className = "flex justify-between";
|
||||||
const addr = document.createElement("span");
|
const addr = document.createElement("span");
|
||||||
addr.className = "pr-2";
|
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;
|
addr.title = counterparty;
|
||||||
const amount = document.createElement("span");
|
const amount = document.createElement("span");
|
||||||
amount.className = "shrink-0";
|
amount.className = "shrink-0";
|
||||||
|
|||||||
Reference in New Issue
Block a user