From bb063e49e305210316681369d3d6eb8e779cd58b Mon Sep 17 00:00:00 2001 From: Jamie Wong Date: Fri, 23 Jun 2023 13:22:53 -0700 Subject: [PATCH] Fix trimTextMid (#431) There was a subtle bug in `trimTextMid` caused by calling substring methods with non-integer values. This happens because `findValueBisect` returns non-integer values, and there was no special handling of this. The bug results in strings cutting off many of the last few characters in a string, rather than always displaying it when possible. Before: image After: image Fixes #411 --- src/lib/text-utils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/text-utils.ts b/src/lib/text-utils.ts index f2383ca..5fd52a0 100644 --- a/src/lib/text-utils.ts +++ b/src/lib/text-utils.ts @@ -43,8 +43,8 @@ export function buildTrimmedText(text: string, length: number): TrimmedTextResul let prefixLength = Math.floor(length / 2) const suffixLength = length - prefixLength - 1 - const prefix = text.substr(0, prefixLength) - const suffix = text.substr(text.length - suffixLength, suffixLength) + const prefix = text.substring(0, prefixLength) + const suffix = text.substring(text.length - suffixLength, text.length) const trimmedString = prefix + ELLIPSIS + suffix return { trimmedString, @@ -69,11 +69,11 @@ export function trimTextMid( 0, text.length, n => { - return cachedMeasureTextWidth(ctx, buildTrimmedText(text, n).trimmedString) + return cachedMeasureTextWidth(ctx, buildTrimmedText(text, Math.floor(n)).trimmedString) }, maxWidth, ) - return buildTrimmedText(text, lo) + return buildTrimmedText(text, Math.floor(lo)) } enum IndexTypeInTrimmed {