Skip to content

Commit 4c89387

Browse files
committed
fix(shared): truncate keeps emoji whole
slice() counts UTF-16 units, so an odd maxLength split a surrogate pair and left a broken character in titles and work-log labels. Cut by code point.
1 parent c6ef3b4 commit 4c89387

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎packages/shared/src/String.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { truncate } from "./String.ts";
4+
5+
describe("truncate", () => {
6+
it("leaves short text alone", () => {
7+
expect(truncate("hello", 50)).toBe("hello");
8+
});
9+
10+
it("cuts by Unicode code point so emoji stay whole", () => {
11+
expect(truncate("👍👍👍", 5)).toBe("👍👍👍");
12+
expect(truncate("👍👍👍👍", 3)).toBe("👍👍👍...");
13+
});
14+
15+
it("trims before measuring", () => {
16+
expect(truncate(" hello ", 5)).toBe("hello");
17+
});
18+
});

‎packages/shared/src/String.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export function truncate(text: string, maxLength = 50): string {
22
const trimmed = text.trim();
3-
if (trimmed.length <= maxLength) {
3+
const characters = Array.from(trimmed);
4+
if (characters.length <= maxLength) {
45
return trimmed;
56
}
67

7-
return `${trimmed.slice(0, maxLength)}...`;
8+
return `${characters.slice(0, maxLength).join("")}...`;
89
}

0 commit comments

Comments
 (0)