fix(string-utils): guard truncateWithEllipsisInMiddle for small maxLength and slice(-0)

This commit is contained in:
plebeius
2026-03-01 18:36:05 +08:00
parent 27b2d6fd8a
commit 8296d70fd6
+3 -1
View File
@@ -28,6 +28,8 @@ export function formatUserIDForDisplay(userID: string | undefined, maxDomainLeng
export function truncateWithEllipsisInMiddle(str: string, maxLength: number = 50): string { export function truncateWithEllipsisInMiddle(str: string, maxLength: number = 50): string {
if (!str || str.length <= maxLength) return str; if (!str || str.length <= maxLength) return str;
const ellipsis = '...'; const ellipsis = '...';
if (maxLength <= ellipsis.length) return str.slice(0, maxLength);
const half = Math.floor((maxLength - ellipsis.length) / 2); const half = Math.floor((maxLength - ellipsis.length) / 2);
return str.slice(0, half) + ellipsis + str.slice(-half); const right = half > 0 ? str.slice(-half) : '';
return str.slice(0, half) + ellipsis + right;
} }