@@ -4,8 +4,9 @@ export function truncate(text: string, maxLength = 50): string {
44 return trimmed ;
55 }
66
7- // Back off to a grapheme boundary so the cut never splits a surrogate
8- // pair, a ZWJ sequence, or a flag pair in half.
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.
910 let end = maxLength ;
1011 while ( isMidCluster ( trimmed , end ) ) {
1112 end = codePointStart ( trimmed , end ) ;
@@ -15,35 +16,54 @@ export function truncate(text: string, maxLength = 50): string {
1516
1617const isHighSurrogate = ( code : number ) : boolean => code >= 0xd800 && code <= 0xdbff ;
1718const isLowSurrogate = ( code : number ) : boolean => code >= 0xdc00 && code <= 0xdfff ;
18- const isRegionalIndicator = ( code : number ) : boolean => code >= 0x1f1e6 && code <= 0x1f1ff ;
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+ ) ;
33+ }
1934
2035// Start index of the code point ending at text[index - 1].
2136function codePointStart ( text : string , index : number ) : number {
22- return index > 1 && isLowSurrogate ( text . charCodeAt ( index - 1 ) ) && isHighSurrogate ( text . charCodeAt ( index - 2 ) )
37+ return index > 1 &&
38+ isLowSurrogate ( text . charCodeAt ( index - 1 ) ) &&
39+ isHighSurrogate ( text . charCodeAt ( index - 2 ) )
2340 ? index - 2
2441 : index - 1 ;
2542}
2643
2744// True when a cut at `index` falls inside a grapheme cluster: between the
28- // halves of a surrogate pair, after a ZWJ whose partner is cut off, or
29- // between the two regional indicators of a flag.
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.
3047function isMidCluster ( text : string , index : number ) : boolean {
3148 if ( index <= 0 || index >= text . length ) {
3249 return false ;
3350 }
3451 if ( isHighSurrogate ( text . charCodeAt ( index - 1 ) ) && isLowSurrogate ( text . charCodeAt ( index ) ) ) {
3552 return true ;
3653 }
54+ if ( attachesToPrevious ( text . codePointAt ( index ) ) ) {
55+ return true ;
56+ }
3757 const start = codePointStart ( text , index ) ;
3858 const code = text . codePointAt ( start ) ;
3959 if ( code === 0x200d ) {
4060 return true ;
4161 }
42- if ( isRegionalIndicator ( code ) ) {
62+ if ( isRegionalIndicator ( code ) && isRegionalIndicator ( text . codePointAt ( index ) ) ) {
4363 // RIs pair from the start of their run: an odd number of RIs before this
4464 // one means it completes a flag, so the cut after it is safe.
4565 let count = 0 ;
46- for ( let i = start ; i > 0 ; ) {
66+ for ( let i = start ; i > 0 ; ) {
4767 i = codePointStart ( text , i ) ;
4868 if ( ! isRegionalIndicator ( text . codePointAt ( i ) ) ) break ;
4969 count += 1 ;
0 commit comments