Get started with diff-match-patch CDN
Apache-2.0 licensed
Diff-Match-Patch is a library for efficiently computing, representing, and applying text differences.
Tags:- diff
- match
- patch
- text-processing
- difference
Stable version
Copied!
How to start using diff-match-patch CDN
// Include the diff-match-patch library from the CDN
const DiffMatchPatch = (() => {
const script = document.createElement('script');
script.src = 'https://cdn.cdnhub.io/diff-match-patch/1.0.5/index.js';
document.head.appendChild(script);
return new Promise(resolve => {
script.onload = () => {
document.head.removeChild(script);
resolve(diff_match_patch);
};
});
})();
// Function to compare two strings and return the differences
async function compareStrings(oldText, newText) {
const dmp = await DiffMatchPatch;
const diff = dmp.diff_main(oldText, newText, null);
const patch = dmp.diff_apply(null, oldText, diff);
return { diff, patch };
}
// Example usage:
const oldText = 'The quick brown fox jumps over the lazy dog.';
const newText = 'The quick red fox jumps over the lazy cat.';
compareStrings(oldText, newText)
.then(result => {
console.log('Diff:', result.diff);
console.log('Patch:', result.patch);
})
.catch(error => {
console.error('Error:', error);
});
Copied!
Copied!