Open History to see the last commit
1// Thai/Lao AM vowels (U+0E33, U+0EB3) are Lo letters with SpacingMark grapheme-break class.
2const EXTENDER = /[\p{M}\p{Grapheme_Extend}\p{Emoji_Modifier}\u0e33\u0eb3]/u;
3const EMOJI_EXTENDER = /[\p{Grapheme_Extend}\p{Emoji_Modifier}]/u;
4const PICTOGRAPH = /\p{Extended_Pictographic}/u;
5const PREPEND =
6 /[\u0600-\u0605\u06DD\u070F\u0890\u0891\u08E2\u0D4E\u{110BD}\u{110CD}\u{111C2}\u{111C3}\u{113D1}\u{1193F}\u{11941}\u{11A84}-\u{11A89}\u{11D46}\u{11F02}]/u;
7const CONTROL = /^(?:[\p{Cc}\p{Zl}\p{Zp}\p{Cf}]|(?=\p{Cn})\p{Default_Ignorable_Code_Point})$/u;
8const ZWJ = "\u200d";
9// UAX #29 GB9c: an Indic consonant after a virama joins the previous consonant's cluster.
10const CONJUNCT_LINKER = /[\u094D\u09CD\u0ACD\u0B4D\u0C4D\u0D4D]/u;
11const CONJUNCT_CONSONANT =
12 /[\u0915-\u0939\u0958-\u095F\u0978-\u097F\u0995-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09DC\u09DD\u09DF\u09F0\u09F1\u0A95-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0AF9\u0B15-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B5C\u0B5D\u0B5F\u0B71\u0C15-\u0C28\u0C2A-\u0C39\u0C58-\u0C5A\u0D15-\u0D3A]/u;
13// Indic_Conjunct_Break=Extend: grapheme extenders and ZWJ, but not ZWNJ, which explicitly breaks the conjunct.
14const CONJUNCT_EXTEND = /(?!\u200c)[\p{Grapheme_Extend}\p{Emoji_Modifier}\u200d]/u;
15
16/** Trims a cut prefix, also dropping trailing Prepend characters that would join `next` (UAX #29 GB9b). */
17export function trimCutPrefix(prefix: string, next: string): string {
18 const trimmed = prefix.trimEnd();
19 const after = trimmed.length < prefix.length ? prefix.slice(trimmed.length) : next;
20 const cp = after.codePointAt(0);
21 const c = cp === undefined ? "" : String.fromCodePoint(cp);
22 if (!c || (CONTROL.test(c) && !PREPEND.test(c) && !EXTENDER.test(c) && c !== ZWJ)) return trimmed;
23 let end = trimmed.length;
24 while (end > 0) {
25 const ch = Array.from(trimmed.slice(Math.max(0, end - 2), end)).at(-1) ?? "";
26 if (!PREPEND.test(ch)) break;
27 end -= ch.length;
28 }
29 return end === trimmed.length
30 ? trimmed
31 : trimCutPrefix(trimmed.slice(0, end), trimmed.slice(end));
32}
33
34export function truncate(text: string, maxLength = 50): string {
35 const trimmed = text.trim();
36 let end = Math.trunc(maxLength);
37 const characters: string[] = [];
38 for (const character of trimmed) {
39 characters.push(character);
40 if (maxLength >= 0 && characters.length === end + 1) break;
41 }
42 if (characters.length <= maxLength) {
43 return trimmed;
44 }
45
46 // GB9: a joiner always belongs to the character before it.
47 while (end > 0 && (EXTENDER.test(characters[end] ?? "") || characters[end] === ZWJ)) {
48 end -= 1;
49 }
50 while (end > 0 && CONJUNCT_CONSONANT.test(characters[end] ?? "")) {
51 let start = end - 1;
52 let linked = false;
53 while (start > 0 && CONJUNCT_EXTEND.test(characters[start] ?? "")) {
54 linked ||= CONJUNCT_LINKER.test(characters[start] ?? "");
55 start -= 1;
56 }
57 if (!linked || !CONJUNCT_CONSONANT.test(characters[start] ?? "")) break;
58 end = start;
59 }
60 while (end > 0) {
61 const joiner = end - 1;
62 if (characters[joiner] !== ZWJ || !PICTOGRAPH.test(characters[end] ?? "")) break;
63
64 let start = joiner - 1;
65 while (EMOJI_EXTENDER.test(characters[start] ?? "")) {
66 start -= 1;
67 }
68 if (!PICTOGRAPH.test(characters[start] ?? "")) break;
69 end = start;
70 }
71
72 // Regional indicators pair up from the start of their run, so an odd count
73 // before the cut means it would split a flag.
74 if (/\p{RI}/u.test(characters[end] ?? "")) {
75 let pairedIndicators = 0;
76 while (/\p{RI}/u.test(characters[end - 1 - pairedIndicators] ?? "")) {
77 pairedIndicators += 1;
78 }
79 end -= pairedIndicators % 2;
80 }
81
82 return `${trimCutPrefix(characters.slice(0, end).join(""), characters[end] ?? "")}...`;
83}