Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Updated fastDiff code comment. Added test for replacing emoji with an…
Browse files Browse the repository at this point in the history
…other emoji.
  • Loading branch information
niegowski committed Mar 26, 2020
1 parent 4876fcc commit 2cccbad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/fastdiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ export default function fastDiff( a, b, cmp, atomicChanges = false ) {
return a === b;
};

// Transform text or any iterable into arrays for easier, consistent processing.
// Array.from was used here but it generated incorrect results for multi-byte unicode sequences.
// Convert the string (or any array-like object - eg. NodeList) to an array by using the slice() method because,
// unlike Array.from(), it returns array of UTF-16 code units instead of the code points of a string.
// One code point might be a surrogate pair of two code units. All text offsets are expected to be in code units.
// See ckeditor/ckeditor5#3147.
//
// We need to make sure here that fastDiff() works identical to diff().
if ( !Array.isArray( a ) ) {
a = Array.prototype.slice.call( a );
}
Expand Down
8 changes: 8 additions & 0 deletions tests/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ describe( 'diff', () => {
it( 'should properly replace emoji', () => {
expect( diff( 'a🙂b', 'axb' ) ).to.deep.equal( [ 'equal', ...emojiDiffDelete, 'insert', 'equal' ] );
} );

it( 'should properly replace one emoji with another', () => {
// 😄 = '\ud83d\ude04' = 2 chars
// Note both emoji have same first code unit
expect( diff( 'a🙂b', 'a😄b' ) ).to.deep.equal(
[ 'equal', 'equal', ...emojiDiffInsert.slice( 1 ), ...emojiDiffDelete.slice( 1 ), 'equal' ]
);
} );
} );

describe( 'combined emoji - unicode ZWJ sequence', () => {
Expand Down

0 comments on commit 2cccbad

Please sign in to comment.