-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathudiff.js
38 lines (32 loc) · 1.08 KB
/
udiff.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
var DiffMatchPatch = require('diff-match-patch');
var dmp = new DiffMatchPatch();
function udiff (config) {
return function diff (text1, text2) {
var patch;
if (config && shouldUseLineLevelDiff(text1, config)) {
patch = udiffLines(text1, text2);
} else {
patch = udiffChars(text1, text2);
}
return decodeURIComponent(patch);
};
}
function shouldUseLineLevelDiff (text, config) {
return config.lineDiffThreshold < text.split(/\r\n|\r|\n/).length;
}
function udiffLines(text1, text2) {
/*jshint camelcase: false */
var a = dmp.diff_linesToChars_(text1, text2);
var diffs = dmp.diff_main(a.chars1, a.chars2, false);
dmp.diff_charsToLines_(diffs, a.lineArray);
dmp.diff_cleanupSemantic(diffs);
return dmp.patch_toText(dmp.patch_make(text1, diffs));
}
function udiffChars (text1, text2) {
/*jshint camelcase: false */
var diffs = dmp.diff_main(text1, text2, false);
dmp.diff_cleanupSemantic(diffs);
return dmp.patch_toText(dmp.patch_make(text1, diffs));
}
module.exports = udiff;