Skip to content

Fix text diffing by replacing 'diff-match-patch' with '@dmsnell/diff-match-patch' #378

Closed
@atsjo

Description

@atsjo

'diff-match-patch' have issues with surrogate pairs when diffing, you can test it in the live demo by changing:
left

...
"summary": "South America😀👍...
...

right

...
"summary": "South America👍...
...

Then the diff below disappears and you can see this error in console:

URIError: URI malformed
    at encodeURI (<anonymous>)
    at diff_match_patch.patch_obj.toString (demo.js:1476:30)
    at Array.join (<anonymous>)
    at diff_match_patch.patch_toText (demo.js:1377:21)
    at diff (demo.js:2457:28)
    at textsDiffFilter (demo.js:2490:24)
    at Pipe.process (demo.js:1570:9)
    at Processor.process (demo.js:1537:18)
    at DiffPatcher.diff (demo.js:2560:29)
    at compare2 (demo.js:3470:30)

In my own use of jsondiffpatch I have replaced diff-match-patch like this:

import type { Options } from 'jsondiffpatch';
import { diff_match_patch } from '@dmsnell/diff-match-patch';
/** replacing diff-match-patch with version from dmsnell which fixes surrogate pair problems  */
export const CustomDMP = diff_match_patch as unknown as NonNullable<Options['textDiff']>['diffMatchPatch'];

and then importing CustomDMP into jsondiffpatch via options...

As a bonus @dmsnell/diff-match-patch is also exported as esm...

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions