4

I'd like to use the google diff/match/patch lib in an angular app to show the difference between two texts.

My usage would be something like this:

public ContentHtml: SafeHtml;
compare(text1: string, text2: string):void {
        var dmp = new diff_match_patch();

        dmp.Diff_Timeout = 1;
        dmp.Diff_EditCost = 4;

        var d = dmp.diff_main(text1, text2);
        dmp.diff_cleanupSemantic(d);
        var ds = dmp.diff_prettyHtml(d);

        this.ContentHtml = this._sanitizer.bypassSecurityTrustHtml(ds);
}

Problem is, how do I import diff_match_patch for this line?
var dmp = new diff_match_patch();

2 Answers 2

4

You need to import the npm package for angular project in package.json.

"diff-match-patch": "^1.0.5",

and import in component as:

import { Component } from '@angular/core';
import DiffMatchPatch from 'diff-match-patch';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';

  getDiff() {
    var dmp = new DiffMatchPatch();
    var diff = dmp.diff_main('Hello', 'Hallo');
    // Result: [(-1, "Hell"), (1, "G"), (0, "o"), (1, "odbye"), (0, " World.")]
    dmp.diff_cleanupSemantic(diff);
    // Result: [(-1, "Hello"), (1, "Goodbye"), (0, " World.")]
    console.log(diff);
  }

}

Here is a demo code

Sign up to request clarification or add additional context in comments.

5 Comments

Do I need to add the package to webpack or something, too? After adding this I got an error on loading Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__ (bootstrap:79) and I'm not sure why
@Sam : I tried using the same code on a small demo project of angular and things worked fine when I served using ng serve and also tried ng build --prod . Are you getting the error when you are running ng s ? For the provided error, there are few solutions I found : stackoverflow.com/questions/41549923/… . Do let me know more about your implementation.
@Sam: any luck ?
In the end it was as simple as cleaning up the cache :/ Sorry it took me so long...
@Sam : Glad to hear that it got fixed. Cheers !
3

Building on top of @Shashank's answer -- to ensure type safety, you can also install the corresponding type defs via:

npm i -D @types/diff-match-patch
or 
yarn add -D @types/diff-match-patch

These type defs haven't changed much since 2017 which means you won't be able to use

import DiffMatchPatch from 'diff-match-patch';

because the types have no default export — they only export the rather poorly named diff_match_patch class (which looks like a function) -- which you were trying to run in the first place!

However, typescript also supports import renaming with which you can import the instantiable class we're all used to:

import { diff_match_patch as DiffMatchPatch } from 'diff-match-patch';

Once imported properly, you'll be able to enjoy intellisense and more:

enter image description here

1 Comment

You'll probably also need to add diff-match-patch to your angular.json config now to avoid the commonJS warning: "allowedCommonJsDependencies": [ "diff-match-patch" ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.