-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsondiff.js
293 lines (272 loc) · 9.99 KB
/
jsondiff.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Important links
// https://blog.jcoglan.com/2017/04/25/myers-diff-in-linear-space-implementation/
export default class JSONDiff {
constructor(oldObj, newObj) {
this.oldObj = oldObj;
this.newObj = newObj;
this.jsonPatch = [];
this.deletes = {};
}
isEmpty = (obj) => {
if (typeof obj != 'number' && obj == null || obj == undefined) return true;
else if(Array.isArray(obj) && obj.length == 0) return true;
else if(typeof obj == 'object') {
for(let i in obj) return false;
return true;
}
return false;
}
getKeys(obj) {
if (Array.isArray(obj)) {
const keys = new Array(obj.length);
for (let k = 0; k < obj.length; k++) {
keys[k] = `_${k}`;
}
return keys;
}
return Object.keys(obj);
}
generateArrDiff(oldArr, newArr, path) {
this.walkMiddleSnake(oldArr, newArr, path);
}
setDelete(path) {
// make sure inserted to jsonPatch first else will be error
this.deletes[path] = this.jsonPatch.length - 1;
}
generateObjDiff(oldObj=this.oldObj, newObj=this.newObj, path='', pushToArr=this.jsonPatch) {
if (this.isEmpty(oldObj)) {
pushToArr.push({op: "add", path: `${path ? path : '/'}`, value: newObj});
} else if (this.isEmpty(newObj)) {
pushToArr.push({op: "delete", path: `${path ? path : '/'}`});
this.setDelete(`${path ? path : '/'}`);
} else {
const oldKeys = this.getKeys(oldObj);
const newKeys = this.getKeys(newObj);
for (let t = 0; t < oldKeys.length; t++) {
// 1. if the value of key is not an array/object/function
if (typeof oldObj[oldKeys[t]] != 'object' && typeof oldObj[oldKeys[t]] != 'function') {
// 1.2. check if the key is not present in new, then delete key
// 1.3. else if key is present, check if value is different replace the value
// 1.4. else same, then skip
if (!newObj.hasOwnProperty(oldKeys[t])) {
const _p = `${path}/${oldKeys[t]}`;
pushToArr.push({op: "delete", path: _p});
this.setDelete(_p);
}
else if (newObj[oldKeys[t]] != oldObj[oldKeys[t]]) {
pushToArr.push({op: "replace", path: `${path}/${oldKeys[t]}`, value: newObj[oldKeys[t]]});
}
}
// 2. if it is an object
else if (typeof oldObj[oldKeys[t]] == 'object' && !Array.isArray(oldObj[oldKeys[t]]) && !Array.isArray(newObj[oldKeys[t]])) {
// 2.2 if newObj does not has that property then delete
// 2.3 else recursive call
if (!newObj.hasOwnProperty(oldKeys[t])) {
const _p = `${path}/${oldKeys[t]}`;
pushToArr.push({op: "delete", path: _p});
this.setDelete(_p);
} else {
this.generateObjDiff(oldObj[oldKeys[t]], newObj[oldKeys[t]], `${path}/${oldKeys[t]}`);
}
}
// 3. if it is an array, use myer's algo
else if (Array.isArray(oldObj[oldKeys[t]]) && Array.isArray(newObj[oldKeys[t]])) {
this.generateArrDiff(oldObj[oldKeys[t]], newObj[oldKeys[t]], `${path}/${oldKeys[t]}`);
}
}
// 4. add all the remaining new properties to patch
for (let t = 0; t < newKeys.length; t++) {
if (!this.isEmpty(newObj[newKeys[t]]) && !oldObj.hasOwnProperty(newKeys[t])) {
pushToArr.push({op: "add", path: `${path}/${newKeys[t]}`, value: newObj[newKeys[t]]});
}
}
}
this.oldObj = {};
this.newObj = {};
}
comparisonOperation(a, b) {
if (!a || !b) {
return;
}
// if string / number
if ((typeof a == 'string' && typeof b =='string') || (typeof a == 'number' && typeof b == 'number')) {
return a == b;
} else if(!Array.isArray(a) && !Array.isArray(b)){
// if object, deep check
if (a.hasOwnProperty('type') && a.type != b.type) return false;
const akeys = Object.keys(a);
const bkeys = Object.keys(b);
for(let i = 0; i < akeys.length; i++) {
if (akeys[i] == 'type') continue;
if (!b.hasOwnProperty(akeys[i])) return false;
const state = this.comparisonOperation(a[akeys[i]], b[akeys[i]]);
if (state == false) return false;
}
for (let i = 0; i < bkeys.length; i++) {
if (!a.hasOwnProperty(bkeys[i])) return false;
}
return true;
} else if (Array.isArray(a) && Array.isArray(b)) { // if array, checking by index
if (a.length != b.length) return false;
else{
for(let i = 0; i < a.length; i++) {
const state = this.comparisonOperation(a[i], b[i]);
if (state == false) return false;
}
return true;
}
}
}
modCalc(x, y) {
return (x + y) % y;
}
forwardShortestEdit(box, vForward, vBackward, i, oldArr, newArr) {
for (var k = i; k >= -i; k-=2) {
let c = k - box.delta;
let px, x;
if (k == -i || (k != i && vForward[this.modCalc(k - 1, vForward.length)] < vForward[this.modCalc(k + 1, vForward.length)])) {
px = x = vForward[this.modCalc(k + 1, vForward.length)]
} else {
px = vForward[this.modCalc(k - 1, vForward.length)];
x = px + 1;
}
let y = box.top + (x - box.left) - k;
let py = (i == 0 || x != px) ? y : y - 1;
while (x < box.right && y < box.bottom && this.comparisonOperation(oldArr[x], newArr[y])) {
x = x + 1;
y = y + 1;
}
vForward[this.modCalc(k, vForward.length)] = x;
if (box.delta % 2 != 0 && c >= -(i - 1) && c <= (i - 1) && y >= vBackward[this.modCalc(c, vBackward.length)]) {
return [
[px, py],
[x, y]
]
}
}
}
backwardShortestEdit(box, vForward, vBackward, i, oldArr, newArr) {
for (var c = i; c >= -i; c-=2) {
let k = c + box.delta;
let py, y;
if (c == -i || (c != i && vBackward[this.modCalc(c - 1, vBackward.length)] > vBackward[this.modCalc(c + 1, vBackward.length)])) {
py = y = vBackward[this.modCalc(c + 1, vBackward.length)]
} else {
py = vBackward[this.modCalc(c - 1, vBackward.length)];
y = py - 1;
}
let x = box.left + (y - box.top) + k;
let px = (i == 0 || y != py) ? x : x + 1;
while (x > box.left && y > box.top && this.comparisonOperation(oldArr[x - 1], newArr[y - 1])) {
x = x - 1;
y = y - 1;
}
vBackward[this.modCalc(c, vBackward.length)] = y;
if (box.delta % 2 == 0 && k >= -i && k <= i && x <= vForward[this.modCalc(k, vForward.length)]) {
return [
[x, y],
[px, py]
]
}
}
}
findMiddleSnake(box, oldArr, newArr) {
if (box.size == 0) return;
const max = Math.ceil(box.size / 2);
const vForward = new Array(2 * max + 1);
vForward[1] = box.left;
const vBackward = new Array(2 * max + 1);
vBackward[1] = box.bottom;
for (var i = 0; i <= max; i++) {
const forward = this.forwardShortestEdit(box, vForward, vBackward, i, oldArr, newArr);
const backward = this.backwardShortestEdit(box, vForward, vBackward, i, oldArr, newArr);
if (forward || backward) {
return forward || backward;
}
}
}
findPath(left, top, right, bottom, oldArr, newArr) {
const box = new Box(left, top, right, bottom);
const snake = this.findMiddleSnake(box, oldArr, newArr);
if (!snake) return;
const start = snake[0];
const finish = snake[1];
const head = this.findPath(box.left, box.top, start[0], start[1], oldArr, newArr);
const tail = this.findPath(finish[0], finish[1], box.right, box.bottom, oldArr, newArr);
return [
...head || [start],
...tail || [finish]
];
}
callbackFunc (x1, y1, x2, y2, oldArr, newArr, path_) {
if (x1 == x2) {
const _p = `${path_}/${y1}`;
if (typeof newArr[y1] == 'object' && !Array.isArray(newArr[y1]) && this.deletes.hasOwnProperty(_p)) {
// if there is a delete at same index and the type is object check for partial update
const arr = [];
this.generateObjDiff(oldArr[y1], newArr[y1], `${path_}/${y1}`, arr);
this.jsonPatch.splice(this.deletes[_p], 1, ...arr);
} else {
if (this.deletes.hasOwnProperty(_p)) {
this.jsonPatch.splice(this.deletes[_p], 1, {
op: 'replace',
path: `${path_}/${y1}`,
value: newArr[y1]
});
} else {
this.jsonPatch.push({
op: 'add',
path: `${path_}/${y1}`,
value: newArr[y1]
});
}
}
} else if(y1 == y2) {
const _p = `${path_}/${x1}`;
this.jsonPatch.push({
op: 'delete',
path: _p
});
this.setDelete(_p);
}
}
walkMiddleSnake(oldArr, newArr, path_) {
const path = this.findPath(0, 0, oldArr.length, newArr.length, oldArr, newArr);
if (!path || path.length == 0) return;
for(var i = 0; i < path.length - 1; i++) {
let pair1 = path[i];
const pair2 = path[i + 1];
pair1 = this.walkDiagonal(pair1, pair2, oldArr, newArr);
const val1 = (pair2[0] - pair1[0]);
const val2 = (pair2[1] - pair1[1]);
if (val1 < val2) {
this.callbackFunc(pair1[0], pair1[1], pair1[0], pair1[1] + 1, oldArr, newArr, path_);
pair1[1]++;
} else if (val1 > val2) {
this.callbackFunc(pair1[0], pair1[1], pair1[0] + 1, pair1[1], oldArr, newArr, path_);
pair1[0]++;
}
this.walkDiagonal(pair1, pair2, oldArr, newArr);
}
}
walkDiagonal (pair1, pair2, oldArr, newArr) {
while (pair1[0] < pair2[0] && pair1[1] < pair2[1] && this.comparisonOperation(oldArr[pair1[0]], newArr[pair1[1]])) {
// this.callbackFunc(pair1[0], pair1[1], pair1[0] + 1, pair1[1] + 1, oldArr, newArr, path_);
pair1[0]++;
pair1[1]++;
}
return [pair1[0], pair1[1]];
}
}
class Box {
constructor(left, top, right, bottom) {
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
this.width = right - left;
this.height = bottom - top;
this.size = this.width + this.height;
this.delta = this.width - this.height;
}
}