-
Notifications
You must be signed in to change notification settings - Fork 11
/
jsondiff.js
238 lines (237 loc) · 6.52 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
// Generated by CoffeeScript 1.6.3
(function() {
(function(root, factory) {
if (typeof exports !== 'undefined') {
return factory(root, exports);
} else if (typeof define === 'function' && define.amd) {
return define(['exports'], function(exports) {
return root.jsondiff = factory(root, exports);
});
} else {
return root.jsondiff = factory(root, {});
}
})(this, function(root) {
var diff, eq, flattenObject, getParent, has, hasOwnProperty, isArray, isContainer, isEqual, isFunction, isObject, isSameContainer, isString, toString;
toString = Object.prototype.toString;
hasOwnProperty = Object.prototype.hasOwnProperty;
isArray = function(obj) {
return toString.call(obj) === '[object Array]';
};
isObject = function(obj) {
return toString.call(obj) === '[object Object]';
};
isString = function(obj) {
return toString.call(obj) === '[object String]';
};
isFunction = function(obj) {
return toString.call(obj) === '[object Function]';
};
has = function(obj, key) {
return hasOwnProperty.call(obj, key);
};
isEqual = function(a, b) {
return eq(a, b, [], []);
};
eq = function(a, b, aStack, bStack) {
var aCtor, bCtor, className, key, length, result, size;
if (a === b) {
return a !== 0 || 1 / a === 1 / b;
}
if ((a == null) || (b == null)) {
return a === b;
}
className = toString.call(a);
if (className !== toString.call(b)) {
return false;
}
switch (className) {
case "[object String]":
return a === String(b);
case "[object Number]":
return (a !== +a ? b !== +b : (a === 0 ? 1 / a === 1 / b : a === +b));
case "[object Date]":
case "[object Boolean]":
return +a === +b;
case "[object RegExp]":
return a.source === b.source && a.global === b.global && a.multiline === b.multiline && a.ignoreCase === b.ignoreCase;
}
if (typeof a !== "object" || typeof b !== "object") {
return false;
}
length = aStack.length;
if ((function() {
var _results;
_results = [];
while (length--) {
_results.push(aStack[length] === a);
}
return _results;
})()) {
return bStack[length] === b;
}
aStack.push(a);
bStack.push(b);
size = 0;
result = true;
if (className === "[object Array]") {
size = a.length;
result = size === b.length;
if (result) {
while (size--) {
if (!(result = eq(a[size], b[size], aStack, bStack))) {
break;
}
}
}
} else {
aCtor = a.constructor;
bCtor = b.constructor;
if (aCtor !== bCtor && !(isFunction(aCtor) && (aCtor instanceof aCtor) && isFunction(bCtor) && (bCtor instanceof bCtor))) {
return false;
}
for (key in a) {
if (has(a, key)) {
size++;
if (!(result = has(b, key) && eq(a[key], b[key], aStack, bStack))) {
break;
}
}
}
if (result) {
for (key in b) {
if (has(b, key) && !(size--)) {
break;
}
}
result = !size;
}
}
aStack.pop();
bStack.pop();
return result;
};
getParent = function(paths, path) {
var idx;
idx = path.lastIndexOf("/");
if (idx === -1) {
return null;
} else if (idx === 0) {
return paths[path];
} else {
return paths[path.substring(0, idx)];
}
};
isContainer = function(obj) {
return isArray(obj) || isObject(obj);
};
isSameContainer = function(obj1, obj2) {
return (isArray(obj1) && isArray(obj2)) || (isObject(obj1) && isObject(obj2));
};
flattenObject = function(obj, prefix, paths) {
var i, key, o, _i, _len;
if (prefix == null) {
prefix = "/";
}
if (paths == null) {
paths = {};
}
paths[prefix] = {
path: prefix,
value: obj
};
if (prefix !== '/') {
prefix = prefix + '/';
}
if (isArray(obj)) {
for (i = _i = 0, _len = obj.length; _i < _len; i = ++_i) {
o = obj[i];
flattenObject(o, prefix + i, paths);
}
} else if (isObject(obj)) {
for (key in obj) {
o = obj[key];
flattenObject(o, prefix + key, paths);
}
}
return paths;
};
diff = function(obj1, obj2) {
var add, doc, doc1, doc2, key, key1, key2, keyfrom, keyto, move, patch, paths1, paths2, remove, replace;
if (!isSameContainer(obj1, obj2)) {
throw new Error('Patches can only be derived from objects or arrays');
}
paths1 = flattenObject(obj1);
paths2 = flattenObject(obj2);
add = {};
remove = {};
replace = {};
move = {};
for (key in paths1) {
doc1 = paths1[key];
doc2 = paths2[key];
if (!getParent(paths2, key)) {
continue;
} else if (!doc2) {
remove[key] = doc1;
} else if (isSameContainer(doc1.value, doc2.value)) {
continue;
} else if (!isEqual(doc1.value, doc2.value)) {
replace[key] = doc2;
}
}
for (key in paths2) {
doc1 = paths1[key];
doc2 = paths2[key];
if (!doc1 && isSameContainer(getParent(paths1, key), getParent(paths2, key))) {
add[key] = doc2;
}
}
for (key1 in remove) {
doc1 = remove[key1];
for (key2 in add) {
doc2 = add[key2];
if (isEqual(doc2.value, doc1.value)) {
delete remove[key1];
delete add[key2];
move[key2] = key1;
break;
}
}
}
patch = [];
for (key in add) {
doc = add[key];
patch.push({
op: 'add',
path: key,
value: doc.value
});
}
for (key in remove) {
patch.push({
op: 'remove',
path: key
});
}
for (key in replace) {
doc = replace[key];
patch.push({
op: 'replace',
path: key,
value: doc.value
});
}
for (keyto in move) {
keyfrom = move[keyto];
patch.push({
op: 'move',
from: keyfrom,
path: keyto
});
}
return patch;
};
root.diff = diff;
return root;
});
}).call(this);