This implementation is based heavily on the original python2 version:
see http://www.phil-roberts.name/json-delta/ for further
documentation. */
-JSON_delta = {
+const JSON_delta = {
isStrictlyEqual: function (left, right) {
if (this.isTerminal(left) && this.isTerminal(right)) {
return (left === right);
if (left.length != right.length) {
return false;
}
- for (idx in left) {
+ for (let idx in left) {
if ( ! this.isStrictlyEqual(left[idx], right[idx])) {
return false;
}
if (ks[1].length != 0 || ks[2].length != 0) {
return false;
}
- for (key in ks[0]) {
+ for (let key in ks[0]) {
key = ks[0][key];
if ( ! this.isStrictlyEqual(left[key], right[key])) {
return false
splitDeletions: function (diff) {
if (diff.length == 0) {return [[], diff]}
diff.sort(function (a,b) {return b.length-a.length});
- for (idx in diff) {
- if (diff[idx] > 1) {break}
+ for (var idx in diff) {
+ if (diff[idx].length === 1) {break}
}
return [diff.slice(0,idx), diff.slice(idx)]
},
var target = overlap;
var targ_num = (left instanceof Array);
- for (key in left) {
+ for (let key in left) {
if (targ_num) {
key = Number(key)
}
}
target.push(key);
}
- for (key in right) {
+ for (let key in right) {
if (targ_num) {
key = Number(key)
}
commonality: function (left, right) {
var com = 0;
var tot = 0;
+ var elem;
if (this.isTerminal(left) || this.isTerminal(right)) {
return 0;
}
if ((left instanceof Array) && (right instanceof Array)) {
- for (idx in left) {
+ for (let idx in left) {
elem = left[idx];
if (right.indexOf(elem) != -1) {
com += 1;
}
else {
var ks = this.computeKeysets(left, right);
- o = ks[0]; l = ks[1]; r = ks[2];
+ let o = ks[0]; let l = ks[1]; let r = ks[2];
com = o.length;
tot = o.length + l.length + r.length;
- for (idx in r) {
+ for (let idx in r) {
elem = r[idx];
if (l.indexOf(elem) == -1) {
tot += 1
if (common) {
var ks = this.computeKeysets(left, right);
- for (idx in ks[0]) {
- okey = ks[0][idx];
+ for (let idx in ks[0]) {
+ let okey = ks[0][idx];
if (left[okey] != right[okey]) {
out.push([key.concat([okey]), right[okey]]);
}
}
- for (idx in ks[1]) {
- okey = ks[1][idx];
+ for (let idx in ks[1]) {
+ let okey = ks[1][idx];
out.push([key.concat([okey])]);
}
- for (idx in ks[2]) {
- okey = ks[2][idx];
+ for (let idx in ks[2]) {
+ let okey = ks[2][idx];
out.push([key.concat([okey]), right[okey]]);
}
return out
keysetDiff: function (left, right, key) {
var out = [];
var ks = this.computeKeysets(left, right);
- for (k in ks[1]) {
+ for (let k in ks[1]) {
out.push([key.concat(ks[1][k])]);
}
- for (k in ks[2]) {
+ for (let k in ks[2]) {
out.push([key.concat(ks[2][k]), right[ks[2][k]]]);
}
- for (k in ks[0]) {
+ for (let k in ks[0]) {
out = out.concat(this.diff(left[ks[0][k]], right[ks[0][k]],
key.concat([ks[0][k]])))
}
patchStanza: function (struc, diff) {
// Applies the diff stanza diff to the structure struc. Returns
// the modified structure.
- key = diff[0];
+ let key = diff[0];
switch (key.length) {
case 0:
struc = diff[1];
}
break;
default:
- pass_key = key.slice(1);
- pass_struc = struc[key[0]];
- pass_diff = [pass_key].concat(diff.slice(1));
+ let pass_key = key.slice(1);
+ let pass_struc = struc[key[0]];
+ let pass_diff = [pass_key].concat(diff.slice(1));
struc[key[0]] = this.patchStanza(pass_struc, pass_diff);
}
return struc;
patch: function (struc, diff) {
// Applies the sequence of diff stanzas diff to the structure
// struc, and returns the patched structure.
- for (stan_key in diff) {
+ for (let stan_key in diff) {
struc = this.patchStanza(struc, diff[stan_key]);
}
return struc
var dumbdiff = [[key, right]]
var my_diff = [];
- common = this.commonality(left, right);
+ let common = this.commonality(left, right);
if (common < 0.5) {
my_diff = this.thisLevelDiff(left, right, key, common);
}