UNPKG

1.1 kBJavaScriptView Raw
1'use strict';
2
3var DiffMatchPatch = require('googlediff');
4var dmp = new DiffMatchPatch();
5
6function udiff (config) {
7 return function diff (text1, text2) {
8 var patch;
9 if (config && shouldUseLineLevelDiff(text1, config)) {
10 patch = udiffLines(text1, text2);
11 } else {
12 patch = udiffChars(text1, text2);
13 }
14 return decodeURIComponent(patch);
15 };
16}
17
18function shouldUseLineLevelDiff (text, config) {
19 return config.lineDiffThreshold < text.split(/\r\n|\r|\n/).length;
20}
21
22function udiffLines(text1, text2) {
23 /*jshint camelcase: false */
24 var a = dmp.diff_linesToChars_(text1, text2);
25 var diffs = dmp.diff_main(a.chars1, a.chars2, false);
26 dmp.diff_charsToLines_(diffs, a.lineArray);
27 dmp.diff_cleanupSemantic(diffs);
28 return dmp.patch_toText(dmp.patch_make(text1, diffs));
29}
30
31function udiffChars (text1, text2) {
32 /*jshint camelcase: false */
33 var diffs = dmp.diff_main(text1, text2, false);
34 dmp.diff_cleanupSemantic(diffs);
35 return dmp.patch_toText(dmp.patch_make(text1, diffs));
36}
37
38module.exports = udiff;