-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.js
219 lines (175 loc) · 6.61 KB
/
diff.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function getDiff(oldText, newText, unit) {
let fragment = document.createDocumentFragment();
/* Make array of words (' ') or characters ('') */
oldText = oldText
.replaceAll(' ', ' «space» ')
.replaceAll('\n', ' \n ')
.replace(/([^\w«»])/g, ' $1 ')
.split(" ")
.filter(x => x);
newText = newText
.replaceAll(' ', ' «space» ')
.replaceAll('\n', ' \n ')
.replace(/([^\w«»])/g, ' $1 ')
.split(" ")
.filter(x => x);
/*Generate diff object*/
let diff = patienceDiff(oldText, newText);
/* In this case, the 'lines' property refers to words or characters */
diff.lines.forEach((o) => {
var color = "";
var bg = "";
var deco = "";
/*Format*/
/* if (o.line == '▶' || o.line == '◀') { color = 'purple' } */ //FIX: Only works when unit = 'char' (i.e. not ▶word◀)
if (o.aIndex < 0) {
/*INSERTION*/
bg = '#e3fff6';
color = 'dodgerblue';
deco = 'underline';
/*Show added line breaks*/
if (o.line == "\n") {o.line = "\n[↵]"}
} else if (o.bIndex < 0) {
/*DELETION*/
bg = '#ffe8e6';
color = '#cf0000';
deco = 'line-through';
/*Show removed lines breaks*/
if (o.line == "\n") {o.line = "[↵]"}
}
span = document.createElement('span');
span.style.backgroundColor = bg;
span.style.color = color;
span.style.textDecoration = deco;
/* if (unit == 'word') */ o.line = o.line.replaceAll('«space»', ' ');
span.appendChild(document.createTextNode(o.line));
if (o.line == '[↵]\n' || o.line == '\n') span.appendChild(document.createElement('br')); /*Show ALL line breaks*/
fragment.appendChild(span);
});
return fragment;
}
function patienceDiff(aLines, bLines, diffPlusFlag) {
/*https://github.com/jonTrent/PatienceDiff/blob/dev/PatienceDiff.js*/
function findUnique(arr, lo, hi) {
var lineMap = new Map();
for (let i = lo; i <= hi; i++) {
let line = arr[i];
if (lineMap.has(line)) {
lineMap.get(line).count++;
lineMap.get(line).index = i;
} else {
lineMap.set(line, {count:1, index: i});
}
}
lineMap.forEach((val, key, map) => {
if (val.count !== 1) {
map.delete(key);
} else {
map.set(key, val.index);
}
});
return lineMap;
}
function uniqueCommon(aArray, aLo, aHi, bArray, bLo, bHi) {
let ma = findUnique(aArray, aLo, aHi);
let mb = findUnique(bArray, bLo, bHi);
ma.forEach((val, key, map) => {
if (mb.has(key)) {
map.set(key, {indexA: val, indexB: mb.get(key)});
} else {
map.delete(key);
}
});
return ma;
}
function longestCommonSubsequence(abMap) {
var ja = [];
abMap.forEach((val, key, map) => {
let i = 0;
while (ja[i] && ja[i][ja[i].length-1].indexB < val.indexB) {
i++;
}
if (!ja[i]) {
ja[i] = [];
}
if (0 < i) {
val.prev = ja[i-1][ja[i-1].length - 1];
}
ja[i].push(val);
});
var lcs = [];
if (0 < ja.length) {
let n = ja.length - 1;
var lcs = [ja[n][ja[n].length - 1]];
while (lcs[lcs.length - 1].prev) {
lcs.push(lcs[lcs.length - 1].prev);
}
}
return lcs.reverse();
}
let result = [];
let deleted = 0;
let inserted = 0;
let aMove = [];
let aMoveIndex = [];
let bMove = [];
let bMoveIndex = [];
function addToResult(aIndex, bIndex) {
if (bIndex < 0) {
aMove.push(aLines[aIndex]);
aMoveIndex.push(result.length);
deleted++;
} else if (aIndex < 0) {
bMove.push(bLines[bIndex]);
bMoveIndex.push(result.length);
inserted++;
}
result.push({line: 0 <= aIndex ? aLines[aIndex] : bLines[bIndex], aIndex: aIndex, bIndex: bIndex});
}
function addSubMatch(aLo, aHi, bLo, bHi) {
while (aLo <= aHi && bLo <= bHi && aLines[aLo] === bLines[bLo]) {
addToResult(aLo++, bLo++);
}
let aHiTemp = aHi;
while (aLo <= aHi && bLo <= bHi && aLines[aHi] === bLines[bHi]) {
aHi--;
bHi--;
}
let uniqueCommonMap = uniqueCommon(aLines, aLo, aHi, bLines, bLo, bHi);
if (uniqueCommonMap.size === 0) {
while (aLo <= aHi) {
addToResult(aLo++, -1);
}
while (bLo <= bHi) {
addToResult(-1, bLo++);
}
} else {
recurseLCS(aLo, aHi, bLo, bHi, uniqueCommonMap);
}
while (aHi < aHiTemp) {
addToResult(++aHi, ++bHi);
}
}
function recurseLCS(aLo, aHi, bLo, bHi, uniqueCommonMap) {
var x = longestCommonSubsequence(uniqueCommonMap || uniqueCommon(aLines, aLo, aHi, bLines, bLo, bHi));
if (x.length === 0) {
addSubMatch(aLo, aHi, bLo, bHi);
} else {
if (aLo < x[0].indexA || bLo < x[0].indexB) {
addSubMatch(aLo, x[0].indexA-1, bLo, x[0].indexB-1);
}
let i;
for (i = 0; i < x.length - 1; i++) {
addSubMatch(x[i].indexA, x[i+1].indexA-1, x[i].indexB, x[i+1].indexB-1);
}
if (x[i].indexA <= aHi || x[i].indexB <= bHi) {
addSubMatch(x[i].indexA, aHi, x[i].indexB, bHi);
}
}
}
recurseLCS(0, aLines.length-1, 0, bLines.length-1);
if (diffPlusFlag) {
return {lines: result, lineCountDeleted: deleted, lineCountInserted: inserted, lineCountMoved: 0, aMove: aMove, aMoveIndex: aMoveIndex, bMove: bMove, bMoveIndex: bMoveIndex};
}
return {lines: result, lineCountDeleted: deleted, lineCountInserted: inserted, lineCountMoved:0};
}