@@ -9,7 +9,11 @@ import diffSequences from '@jest/diff-sequences';
99import { DIFF_DELETE , DIFF_EQUAL , DIFF_INSERT , Diff } from './cleanupSemantic' ;
1010
1111const diffStrings = ( a : string , b : string ) : Array < Diff > => {
12- const isCommon = ( aIndex : number , bIndex : number ) => a [ aIndex ] === b [ bIndex ] ;
12+ // Split strings into code points to handle surrogate pairs.
13+ const aCodepoints = [ ...a ] ;
14+ const bCodepoints = [ ...b ] ;
15+ const isCommon = ( aIndex : number , bIndex : number ) =>
16+ aCodepoints [ aIndex ] === bCodepoints [ bIndex ] ;
1317
1418 let aIndex = 0 ;
1519 let bIndex = 0 ;
@@ -21,25 +25,36 @@ const diffStrings = (a: string, b: string): Array<Diff> => {
2125 bCommon : number ,
2226 ) => {
2327 if ( aIndex !== aCommon ) {
24- diffs . push ( new Diff ( DIFF_DELETE , a . slice ( aIndex , aCommon ) ) ) ;
28+ diffs . push (
29+ new Diff ( DIFF_DELETE , aCodepoints . slice ( aIndex , aCommon ) . join ( '' ) ) ,
30+ ) ;
2531 }
2632 if ( bIndex !== bCommon ) {
27- diffs . push ( new Diff ( DIFF_INSERT , b . slice ( bIndex , bCommon ) ) ) ;
33+ diffs . push (
34+ new Diff ( DIFF_INSERT , bCodepoints . slice ( bIndex , bCommon ) . join ( '' ) ) ,
35+ ) ;
2836 }
2937
3038 aIndex = aCommon + nCommon ; // number of characters compared in a
3139 bIndex = bCommon + nCommon ; // number of characters compared in b
32- diffs . push ( new Diff ( DIFF_EQUAL , b . slice ( bCommon , bIndex ) ) ) ;
40+ diffs . push (
41+ new Diff ( DIFF_EQUAL , bCodepoints . slice ( bCommon , bIndex ) . join ( '' ) ) ,
42+ ) ;
3343 } ;
3444
35- diffSequences ( a . length , b . length , isCommon , foundSubsequence ) ;
45+ diffSequences (
46+ aCodepoints . length ,
47+ bCodepoints . length ,
48+ isCommon ,
49+ foundSubsequence ,
50+ ) ;
3651
3752 // After the last common subsequence, push remaining change items.
38- if ( aIndex !== a . length ) {
39- diffs . push ( new Diff ( DIFF_DELETE , a . slice ( aIndex ) ) ) ;
53+ if ( aIndex !== aCodepoints . length ) {
54+ diffs . push ( new Diff ( DIFF_DELETE , aCodepoints . slice ( aIndex ) . join ( '' ) ) ) ;
4055 }
41- if ( bIndex !== b . length ) {
42- diffs . push ( new Diff ( DIFF_INSERT , b . slice ( bIndex ) ) ) ;
56+ if ( bIndex !== bCodepoints . length ) {
57+ diffs . push ( new Diff ( DIFF_INSERT , bCodepoints . slice ( bIndex ) . join ( '' ) ) ) ;
4358 }
4459
4560 return diffs ;
0 commit comments