benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.79k stars 466 forks source link

Typescript Usage with `create` #250

Closed sebastiansandqvist closed 4 years ago

sebastiansandqvist commented 6 years ago

How can one set config options when using jsondiffpatch with typescript? I see the Config interface in the typings, but no create function to pass the config to.

russ9443 commented 6 years ago

Looking through the index.d.ts, I was able to figure out that DiffPatcher was the key.

import { diff, Config, DiffPatcher, formatters } from 'jsondiffpatch'; . . . var conf: Config; conf = { objectHash: function(obj) { if (typeof obj._id !== 'undefined') { return obj._id; } if (typeof obj.id !== 'undefined') { return obj.id; } if (typeof obj.name !== 'undefined') { return obj.name; } return JSON.stringify(obj); }, arrays: { detectMove: true, includeValueOnMove: true }, textDiff: { minLength: 60 }, cloneDiffValues: false };

      var jsondiffpatch = new DiffPatcher(conf);
      var delta = jsondiffpatch.diff(this.left, this.right);
      this.htmlOutput = formatters.html.format(delta, this.left);