@@ -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