Commit c375a10a authored by isstuev's avatar isstuev

use binary search in prod for the 1st time in my life

parent 2dc65c83
...@@ -46,14 +46,21 @@ const HashStringShortenDynamic = ({ hash, fontWeight = '400' }: Props) => { ...@@ -46,14 +46,21 @@ const HashStringShortenDynamic = ({ hash, fontWeight = '400' }: Props) => {
const parentWidth = getWidth(parent); const parentWidth = getWidth(parent);
if (getWidth(shadowEl) > parentWidth) { if (getWidth(shadowEl) > parentWidth) {
for (let i = 1; i <= hash.length - TAIL_LENGTH - HEAD_MIN_LENGTH; i++) { const tail = hash.slice(-TAIL_LENGTH);
const res = hash.slice(0, hash.length - i - TAIL_LENGTH) + '...' + hash.slice(-TAIL_LENGTH); let leftI = HEAD_MIN_LENGTH;
let rightI = hash.length - TAIL_LENGTH;
while (rightI - leftI > 1) {
const medI = ((rightI - leftI) % 2) ? leftI + (rightI - leftI + 1) / 2 : leftI + (rightI - leftI) / 2;
const res = hash.slice(0, medI) + '...' + tail;
shadowEl.textContent = res; shadowEl.textContent = res;
if (getWidth(shadowEl) < parentWidth || i === hash.length - TAIL_LENGTH - HEAD_MIN_LENGTH) { if (getWidth(shadowEl) < parentWidth) {
setDisplayedString(res); leftI = medI;
break; } else {
rightI = medI;
} }
} }
setDisplayedString(hash.slice(0, rightI - 1) + '...' + tail);
} else { } else {
setDisplayedString(hash); setDisplayedString(hash);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment