Skip to content

Commit 1ebde9f

Browse files
fix(shared): simplify emoji-safe truncation and reduce duplicate tests
1 parent 93e877f commit 1ebde9f

2 files changed

Lines changed: 25 additions & 90 deletions

File tree

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,15 @@ import { describe, expect, it } from "vite-plus/test";
33
import { truncate } from "./String.ts";
44

55
describe("truncate", () => {
6-
it("keeps short text and trims whitespace", () => {
7-
expect(truncate(" hello ", 10)).toBe("hello");
8-
});
9-
10-
it("cuts long text at the limit", () => {
11-
expect(truncate("abcdefgh", 5)).toBe("abcde...");
12-
});
13-
146
it("never leaves half of an emoji at the cut", () => {
157
expect(truncate("abcd😀 more", 5)).toBe("abcd...");
168
expect(truncate("abc😀 more", 5)).toBe("abc😀...");
179
});
1810

19-
it("never cuts a ZWJ emoji sequence in half", () => {
20-
const family = "👨‍👩‍👧‍👦";
21-
expect(truncate(`${family} x`, 4)).toBe("...");
22-
expect(truncate(`${family} x`, 6)).toBe("...");
23-
expect(truncate(`ab${family} x`, 13)).toBe(`ab${family}...`);
24-
expect(truncate(`ab${family} x`, 12)).toBe("ab...");
25-
});
26-
27-
it("keeps a skin tone, variation selector or combining mark with its base", () => {
11+
it("keeps a grapheme cluster whole at the cut", () => {
12+
expect(truncate("ab👨‍👩‍👧‍👦 x", 12)).toBe("ab...");
2813
expect(truncate("abc👍🏽 z", 5)).toBe("abc...");
29-
expect(truncate("abc👍🏽 z", 7)).toBe("abc👍🏽...");
30-
expect(truncate("abc❤️ z", 4)).toBe("abc...");
31-
expect(truncate("abc1️⃣ z", 5)).toBe("abc...");
32-
expect(truncate("cafe\u0301 au lait", 4)).toBe("caf...");
33-
});
34-
35-
it("never splits a flag emoji", () => {
36-
expect(truncate("🇳🇱abc", 3)).toBe("...");
37-
expect(truncate("🇳🇱abc", 5)).toBe("🇳🇱a...");
14+
expect(truncate("café au lait", 4)).toBe("caf...");
3815
expect(truncate("🇳🇱🇩🇪abc", 6)).toBe("🇳🇱...");
3916
});
4017
});

‎packages/shared/src/String.ts‎

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,30 @@ export function truncate(text: string, maxLength = 50): string {
33
if (trimmed.length <= maxLength) {
44
return trimmed;
55
}
6-
7-
// Back off to a grapheme boundary so the cut never splits a surrogate pair,
8-
// drops a skin tone, variation selector or combining mark, breaks a ZWJ
9-
// sequence, or separates the two halves of a flag.
10-
let end = maxLength;
11-
while (isMidCluster(trimmed, end)) {
12-
end = codePointStart(trimmed, end);
13-
}
14-
return `${trimmed.slice(0, end)}...`;
15-
}
16-
17-
const isHighSurrogate = (code: number): boolean => code >= 0xd800 && code <= 0xdbff;
18-
const isLowSurrogate = (code: number): boolean => code >= 0xdc00 && code <= 0xdfff;
19-
const isRegionalIndicator = (code: number | undefined): boolean =>
20-
code !== undefined && code >= 0x1f1e6 && code <= 0x1f1ff;
21-
22-
// Code points that attach to the one before them: combining marks (which
23-
// include variation selectors and the keycap mark), ZWJ, skin-tone modifiers
24-
// and emoji tag characters. Intl.Segmenter would cover these, but Hermes lacks it.
25-
function attachesToPrevious(code: number | undefined): boolean {
26-
if (code === undefined) return false;
27-
return (
28-
code === 0x200d ||
29-
(code >= 0x1f3fb && code <= 0x1f3ff) ||
30-
(code >= 0xe0020 && code <= 0xe007f) ||
31-
/\p{M}/u.test(String.fromCodePoint(code))
32-
);
6+
return `${trimmed.slice(0, clusterBoundaryBefore(trimmed, maxLength))}...`;
337
}
348

35-
// Start index of the code point ending at text[index - 1].
36-
function codePointStart(text: string, index: number): number {
37-
return index > 1 &&
38-
isLowSurrogate(text.charCodeAt(index - 1)) &&
39-
isHighSurrogate(text.charCodeAt(index - 2))
40-
? index - 2
41-
: index - 1;
42-
}
9+
// Code points that extend the grapheme cluster before them: ZWJ, skin-tone
10+
// modifiers, emoji tag characters and combining marks (which include variation
11+
// selectors and the keycap mark). Intl.Segmenter would cover all of this, but
12+
// Hermes lacks it and the no-hermes-unsupported-apis lint rule bans it here.
13+
const CLUSTER_EXTENDER = /^(?:‍|[\u{1f3fb}-\u{1f3ff}\u{e0020}-\u{e007f}]|\p{M})$/u;
14+
const REGIONAL_INDICATOR = /^[\u{1f1e6}-\u{1f1ff}]$/u;
4315

44-
// True when a cut at `index` falls inside a grapheme cluster: between the
45-
// halves of a surrogate pair, before a code point that attaches to the
46-
// previous one, after a ZWJ, or between the two regional indicators of a flag.
47-
function isMidCluster(text: string, index: number): boolean {
48-
if (index <= 0 || index >= text.length) {
49-
return false;
50-
}
51-
if (isHighSurrogate(text.charCodeAt(index - 1)) && isLowSurrogate(text.charCodeAt(index))) {
52-
return true;
53-
}
54-
if (attachesToPrevious(text.codePointAt(index))) {
55-
return true;
56-
}
57-
const start = codePointStart(text, index);
58-
const code = text.codePointAt(start);
59-
if (code === 0x200d) {
60-
return true;
61-
}
62-
if (isRegionalIndicator(code) && isRegionalIndicator(text.codePointAt(index))) {
63-
// RIs pair from the start of their run: an odd number of RIs before this
64-
// one means it completes a flag, so the cut after it is safe.
65-
let count = 0;
66-
for (let i = start; i > 0;) {
67-
i = codePointStart(text, i);
68-
if (!isRegionalIndicator(text.codePointAt(i))) break;
69-
count += 1;
70-
}
71-
return count % 2 === 0;
16+
// Regional indicators form pairs from the start of their run.
17+
function clusterBoundaryBefore(text: string, limit: number): number {
18+
let boundary = 0;
19+
let index = 0;
20+
let afterJoiner = false;
21+
let openFlag = false;
22+
for (const char of text) {
23+
if (index > limit) break;
24+
const isRegionalIndicator = REGIONAL_INDICATOR.test(char);
25+
const joins = afterJoiner || CLUSTER_EXTENDER.test(char) || (openFlag && isRegionalIndicator);
26+
if (!joins) boundary = index;
27+
afterJoiner = char === "‍";
28+
openFlag = isRegionalIndicator && !openFlag;
29+
index += char.length;
7230
}
73-
return false;
31+
return boundary;
7432
}

0 commit comments

Comments
 (0)