fix #253: trim long text without space (#256)

This commit is contained in:
lenhanphung
2025-10-27 17:53:01 +07:00
committed by GitHub
parent cdaed8a7c1
commit 2355ee8771
3 changed files with 50 additions and 9 deletions
+22
View File
@@ -0,0 +1,22 @@
export function trimLongTextWithoutSpace(text: string): string {
const words = text.split(/\s+/);
const hasLongWord = words.some((word) => word.length > 25);
if (!hasLongWord) {
return text;
}
let result = "";
for (const word of words) {
if (word.length > 25) {
const remaining = 20 - result.length;
result += word.substring(0, Math.max(remaining - 3, 5)) + "...";
break;
} else {
result += (result ? " " : "") + word;
}
}
return result;
}