Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

String.ts

109 lines5.4 KB

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: Linker and Consonant come from Unicode 17.0 DerivedCoreProperties.txt.
10const CONJUNCT_LINKER =
11 /[\u094D\u09CD\u0ACD\u0B4D\u0C4D\u0D4D\u1039\u17D2\u1A60\u1B44\u1BAB\uA9C0\uAAF6\u{10A3F}\u{11133}\u{113D0}\u{1193E}\u{11A47}\u{11A99}\u{11F42}]/u;
12const CONJUNCT_CONSONANT =
13 /[\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\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065-\u1066\u106E-\u1070\u1075-\u1081\u108E\u1780-\u17B3\u1A20-\u1A54\u1B0B-\u1B0C\u1B13-\u1B33\u1B45-\u1B4C\u1B83-\u1BA0\u1BAE-\u1BAF\u1BBB-\u1BBD\uA989-\uA98B\uA98F-\uA9B2\uA9E0-\uA9E4\uA9E7-\uA9EF\uA9FA-\uA9FE\uAA60-\uAA6F\uAA71-\uAA73\uAA7A\uAA7E-\uAA7F\uAAE0-\uAAEA\uABC0-\uABDA\u{10A00}\u{10A10}-\u{10A13}\u{10A15}-\u{10A17}\u{10A19}-\u{10A35}\u{11103}-\u{11126}\u{11144}\u{11147}\u{11380}-\u{11389}\u{1138B}\u{1138E}\u{11390}-\u{113B5}\u{11900}-\u{11906}\u{11909}\u{1190C}-\u{11913}\u{11915}-\u{11916}\u{11918}-\u{1192F}\u{11A00}\u{11A0B}-\u{11A32}\u{11A50}\u{11A5C}-\u{11A83}\u{11F04}-\u{11F10}\u{11F12}-\u{11F33}]/u;
14// Indic_Conjunct_Break=Extend: grapheme extenders and ZWJ, but not ZWNJ, which explicitly breaks the conjunct.
15const CONJUNCT_EXTEND = /(?!\u200c)[\p{Grapheme_Extend}\p{Emoji_Modifier}\u200d]/u;
16// UAX #29 GB6-GB8: conjoining jamo ranges come from Unicode 17.0 HangulSyllableType.txt.
17const HANGUL_L = /[\u1100-\u115F\uA960-\uA97C]/u;
18const HANGUL_V = /[\u1160-\u11A7\uD7B0-\uD7C6]/u;
19const HANGUL_T = /[\u11A8-\u11FF\uD7CB-\uD7FB]/u;
20const HANGUL_JOIN = /^(?:L (?:L|V|LV|LVT)|(?:LV|V) [VT]|(?:LVT|T) T)$/;
21​
22function hangulSyllableType(character: string): string {
23 const code = character.codePointAt(0) ?? 0;
24 // Every 28th precomposed syllable from U+AC00 has no final consonant.
25 if (code >= 0xac00 && code <= 0xd7a3) return (code - 0xac00) % 28 === 0 ? "LV" : "LVT";
26 if (HANGUL_L.test(character)) return "L";
27 if (HANGUL_V.test(character)) return "V";
28 return HANGUL_T.test(character) ? "T" : "";
30​
31/** Trims a cut prefix, also dropping trailing Prepend characters that would join `next` (UAX #29 GB9b). */
32export function trimCutPrefix(prefix: string, next: string): string {
33 const trimmed = prefix.trimEnd();
34 const after = trimmed.length < prefix.length ? prefix.slice(trimmed.length) : next;
35 const cp = after.codePointAt(0);
36 const c = cp === undefined ? "" : String.fromCodePoint(cp);
37 if (!c || (CONTROL.test(c) && !PREPEND.test(c) && !EXTENDER.test(c) && c !== ZWJ)) return trimmed;
38 let end = trimmed.length;
39 while (end > 0) {
40 const ch = Array.from(trimmed.slice(Math.max(0, end - 2), end)).at(-1) ?? "";
41 if (!PREPEND.test(ch)) break;
42 end -= ch.length;
43 }
44 return end === trimmed.length
45 ? trimmed
46 : trimCutPrefix(trimmed.slice(0, end), trimmed.slice(end));
48​
49export function truncate(text: string, maxLength = 50): string {
50 const trimmed = text.trim();
51 let end = Math.trunc(maxLength);
52 const characters: string[] = [];
53 for (const character of trimmed) {
54 characters.push(character);
55 if (maxLength >= 0 && characters.length === end + 1) break;
56 }
57 if (characters.length <= maxLength) {
58 return trimmed;
59 }
60​
61 // GB9: a joiner always belongs to the character before it.
62 while (end > 0 && (EXTENDER.test(characters[end] ?? "") || characters[end] === ZWJ)) {
63 end -= 1;
64 }
65 while (
66 end > 0 &&
67 HANGUL_JOIN.test(
68 `${hangulSyllableType(characters[end - 1] ?? "")} ${hangulSyllableType(characters[end] ?? "")}`,
69 )
70 ) {
71 end -= 1;
72 }
73 while (end > 0 && CONJUNCT_CONSONANT.test(characters[end] ?? "")) {
74 let start = end - 1;
75 let linked = false;
76 while (start > 0) {
77 const character = characters[start] ?? "";
78 const isLinker = CONJUNCT_LINKER.test(character);
79 if (!isLinker && !CONJUNCT_EXTEND.test(character)) break;
80 linked ||= isLinker;
81 start -= 1;
82 }
83 if (!linked || !CONJUNCT_CONSONANT.test(characters[start] ?? "")) break;
84 end = start;
85 }
86 while (end > 0) {
87 const joiner = end - 1;
88 if (characters[joiner] !== ZWJ || !PICTOGRAPH.test(characters[end] ?? "")) break;
89​
90 let start = joiner - 1;
91 while (EMOJI_EXTENDER.test(characters[start] ?? "")) {
92 start -= 1;
93 }
94 if (!PICTOGRAPH.test(characters[start] ?? "")) break;
95 end = start;
96 }
97​
98 // Regional indicators pair up from the start of their run, so an odd count
99 // before the cut means it would split a flag.
100 if (/\p{RI}/u.test(characters[end] ?? "")) {
101 let pairedIndicators = 0;
102 while (/\p{RI}/u.test(characters[end - 1 - pairedIndicators] ?? "")) {
103 pairedIndicators += 1;
104 }
105 end -= pairedIndicators % 2;
106 }
107​
108 return `${trimCutPrefix(characters.slice(0, end).join(""), characters[end] ?? "")}...`;