-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffInline.diff.js
204 lines (172 loc) · 4.38 KB
/
diffInline.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
/**
* Сравнение массивов с генерацией разницы
* @param a {Array} первый массив
* @param b {Array} второй массив
* @returns {Object} объект формата:
* {
* old // удалённое
* default // неизменное
* new // добавленное
* data: {
* type // вид разницы [old, default, new]
* value // значение разницы
* }
* }
*/
// https://github.com/umdjs/umd
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
}
else if (typeof module === 'object' && module.exports) {
module.exports = factory;
}
else {
root.diff = factory;
};
} (this, function (a, b, depth) {
// depth — ширина строки
// length — количество строк
// пустота
var empty = 0;
// массив с результатом
var result = {
old: empty,
default: empty,
new: empty,
data: []
};
// вспомогательные массивы
var tmpA = [];
var tmpB = [];
var tmpD = [];
var checkA = [];
var checkB = [];
var checkA_ = [];
var check_B = [];
// счётчики
var indexA = empty;
var indexB = empty;
// вспомогательные счётчики
var i = empty;
var j = empty;
var k = empty;
var m = empty;
// идём по массивам
while (1) {
// для каждого массива свой индекс с соответствующим сдвигом
indexA += ((tmpA.length + tmpD.length) * depth);
indexB += ((tmpB.length + tmpD.length) * depth);
// пополняем результат
result.data = result.data.concat(tmpA);
result.data = result.data.concat(tmpB);
result.data = result.data.concat(tmpD);
// меняем статистику
result.old += tmpA.length;
result.new += tmpB.length;
result.default += tmpD.length;
// обнуляем вспомогательные массивы
tmpA.length = empty;
tmpB.length = empty;
tmpD.length = empty;
// если нет ни старого, ни нового
if (indexA >= a.length && indexB >= b.length) {
return result;
}
// если нет нового
else if (indexB >= b.length) {
tmpA.push({
type: 'old',
line: indexA / depth
});
}
// если нет старого
else if (indexA >= a.length) {
tmpB.push({
type: 'new',
line: indexB / depth
});
}
// если не изменилось
else if (isEqual(indexA, indexB)) {
tmpD.push({
type: 'default',
line: 0
});
}
// если есть отличия
else {
// вспомогательные счётчики
i = indexB;
j = indexA;
k = i;
m = j;
// вспомогательные массивы
checkA.length = [];
checkB.length = [];
checkA_ = [];
check_B = [];
// проверяем, было ли старое
while (j < a.length && !isEqual(j, indexB)) {
checkA.push({
type: 'old',
line: j / depth
});
j += depth;
};
// проверяем, есть ли новое
while (i < b.length && !isEqual(indexA, i)) {
checkB.push({
type: 'new',
line: i / depth
});
i += depth;
};
// проверяем, произошла ли замена
while (m < a.length && k < b.length && !isEqual(m, k)) {
checkA_.push({
type: 'old',
line: m / depth
});
check_B.push({
type: 'new',
line: k / depth
});
m += depth;
k += depth;
};
// произошла замена
if (
(checkA.length + checkB.length === checkA_.length + check_B.length)
|| checkA_.length + check_B.length <= checkA.length
&& checkA_.length + check_B.length <= checkB.length
) {
indexA += checkA_.length * depth;
indexB += check_B.length * depth;
result.old += checkA_.length;
result.new += check_B.length;
result.data.push({
type: 'replace',
lines: [checkA_, check_B]
});
}
// что-то пропало
else if (checkB.length >= checkA.length) {
tmpA = tmpA.concat(checkA);
}
// добавлено новое
else {
tmpB = tmpB.concat(checkB);
};
};
};
// проверка равенства строки
function isEqual (startA, startB) {
for (var i = 0; i < depth; i++) {
if (a[startA + i] !== b[startB + i]) {
return false;
};
};
return true;
};
}));