Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atsjo opened this issue Jan 21, 2025 · 1 comment

Comments

@atsjo
Copy link

atsjo commented Jan 21, 2025

'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...

@atsjo
Copy link
Author

atsjo commented Jan 21, 2025

And many thanks for a powerful library with an api that actually allowed me to replace this dependency, and also create my own custom formatter with support for text diff patches👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant