Applying or reversing a diff will check for success by comparing input and output against an MD5 hash calculated when creating the diff. Ensures application or reversion of a diff is executed on exactly the same document, preventing information loss.
Installation and usage
Install the package with the following command. Optionally specify the --save parameter to include diff-replay as a dependency in package.json.
npm install diff-replay
var diffReplay = require("diff-replay");var diffresult = diffReplay.diffChars("old text", "new text");var newText = diffReplay.applyDiff("old text", diffresult);
In the code sample applyDiff() will throw an exception if the MD5 hash of "old text" does not match diffresult.oldStringMD5Hash.
Use the functions 'diffChars(oldString, newString)', 'diffWords(oldString, newString)' and 'diffLines(oldString, newString)' to get the diff of the desired level of detail.
Use the function 'applyDiff(oldString, diff)' to get the newString. Use the function 'reverseDiff(newString, diff)' to get the oldString.
You can round-trip applyDiff and reverseDiff, like so:
var diffresult = diffReplay.diffChars("oldString", "newString");var newString = diffReplay.applyDiff("oldString", diffresult);var oldString = diffReplay.reverseDiff(newString, diffresult);assert.equal(oldString, "oldString");