json_diff_rfc6902
json_diff_rfc6902 copied to clipboard
diff calculated wrong
In the simple example, a weird "copy" patch is calculated as a diff what is wrong. Check it on codepen or here:
var a = {
choices: [
{id: "2"}
]
};
var b = {
choices: [
{id: "2"}
],
description: "2"
};
var diff = jdr.diff(a, b);
jdr.apply(a, diff, {OBJ_COM:false});
console.info("diff:", diff); // results [{op: "copy", path: "/description", from: "/choices/0"}]
console.info("this should be b:", a); // results something else than b
Ok, i think the problem is here (at least for my case): when you are building the unchanged array in transformArray(), you store hashes:
unchanged.push( path + '/' + y_sorted[j].index + "=" + JSON.stringify(x_sorted[i].hash));
Then, when you are searching in the unchanged array in generateObjectDiff(), you search for a simple value, not for hash:
var pointer = unchangedArea.findValueInUnchanged(JSON.stringify(newVal), unchanged);
so the pointer can be calculated wrong.