Skip to content

Commit

Permalink
- added .gitignore not to track any temp/generated files (included pa…
Browse files Browse the repository at this point in the history
…ckage-lock.json)

- removed packaging itself as an external node module
- fixed async warning dropping by fs
- fixed issue caohanyang#3 (diff calculated wrong)
- modified expected test results where the calculated patch is equivalent (now only 1 fails)
- added comments to almost every change
- the code formatting is a mess, but i've left it as it is to compare more easy - proper tabulating required
  • Loading branch information
nlac committed Jan 31, 2018
1 parent cb23e4a commit 41e1abc
Show file tree
Hide file tree
Showing 123 changed files with 104 additions and 1,584 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*_new.json
*_patch.json
*.csv
node_modules/
package-lock.json
29 changes: 20 additions & 9 deletions JSON-Diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ function generateArrayDiff(oldJson, newJson, unchanged, patches, path) {

// Use LCS
var tmpPatches = [];
var tmpPatchHashes = [];

if (oldJson.length === 0) {
patches.push({ op: "add", path: path, value: newJson});
} else {
// Use sortBack
tmpPatches = transformArray(oldJson, newJson, unchanged, tmpPatches, tmpPatchHashes, path);
// deleted unused parameters from the call
tmpPatches = transformArray(oldJson, newJson, unchanged, path);
for (var l = 0; l < tmpPatches.length; l++) {
patches.push(tmpPatches[l]);
}
Expand Down Expand Up @@ -146,7 +146,9 @@ function generateObjectDiff(oldJson, newJson, unchanged, patches, path) {
if (!oldJson.hasOwnProperty(newKey)) {
//Try to find the value in the unchanged area
// change JSON.stringify()
var pointer = unchangedArea.findValueInUnchanged(JSON.stringify(newVal), unchanged);

// pass the value here, apply JSON.stringify in the method
var pointer = unchangedArea.findValueInUnchanged(newVal, unchanged);
if (pointer) {
//COPY
patches.push({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer});
Expand Down Expand Up @@ -294,7 +296,8 @@ function findCopyInArray(element, m, array, arrUnchanged) {
return copyIndex;
}

function transformArray(oldJson, newJson, unchanged, patches, patchHashes, path, jsondiff) {
// deleted unused parameters
function transformArray(oldJson, newJson, unchanged, path) {
//When is the Array, stop to find leaf node
// (hash, value, index)
var x = hashObject.mapArray(hashObject.hash, oldJson, HASH_ID);
Expand All @@ -315,6 +318,7 @@ function transformArray(oldJson, newJson, unchanged, patches, patchHashes, path,

while (i < x_sorted.length) {
while( j < y_sorted.length) {

if(x_sorted[i] !== void 0) {

if (x_sorted[i].hash > y_sorted[j].hash) {
Expand All @@ -323,7 +327,11 @@ function transformArray(oldJson, newJson, unchanged, patches, patchHashes, path,

} else if (x_sorted[i].hash === y_sorted[j].hash) {
// Unchanged push
unchanged.push( path + '/' + y_sorted[j].index + "=" + JSON.stringify(x_sorted[i].hash));
//no point to insert if already there
var el = path + '/' + y_sorted[j].index + "=" + JSON.stringify(x_sorted[i].value);
if (unchanged.indexOf(el)<0)
unchanged.push(el);

arrPatch.push({op: "move", value: y_sorted[j].value, valueOld: x_sorted[i].value, from: x_sorted[i].index , index: y_sorted[j].index, hash: y_sorted[j].hash });
i++;
j++;
Expand All @@ -338,14 +346,15 @@ function transformArray(oldJson, newJson, unchanged, patches, patchHashes, path,
j++;
}

}
} //j

if (i < x_sorted.length) {
// Remove the rest elements of the x_sorted
arrPatch.push({op: "remove", index: x_sorted[i].index, value: x_sorted[i].value });
// for being consistent, added hash as well like for other operators
arrPatch.push({op: "remove", index: x_sorted[i].index, value: x_sorted[i].value, hash: x_sorted[i].hash });
i++;
}
}
} //i

//Get the patch to make all the elements are the same, but index is random
arrPatch = arrPatch.sort(compare);
Expand Down Expand Up @@ -462,6 +471,8 @@ function transformArray(oldJson, newJson, unchanged, patches, patchHashes, path,

}

/*
// no reason to do anything with arrPatch local variable at this point
arrPatch = arrPatch.map(function(obj) {
obj.path = path + '/' + obj.index;
delete obj.hash;
Expand All @@ -473,7 +484,7 @@ function transformArray(oldJson, newJson, unchanged, patches, patchHashes, path,
return obj;
});
*/

return arrtmp;

}
61 changes: 0 additions & 61 deletions dataset/Stackoverflow/Stackoverflow.csv

This file was deleted.

15 changes: 0 additions & 15 deletions dataset/Twitter/Twitter.csv

This file was deleted.

61 changes: 0 additions & 61 deletions dataset/Xignite/Xignite.csv

This file was deleted.

9 changes: 5 additions & 4 deletions dataset/generate_patch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var jsonpatch = require('fast-json-patch'),
jiff = require('jiff'),
rfc6902 = require('rfc6902'),
jdr = require('json-diff-rfc6902'),
jdr = require('../json-diff-rfc6902'),
json8 = require('json8-patch'),
fs = require('fs'),
now = require("performance-now");
Expand All @@ -18,7 +18,8 @@ for (var i = 0; i < data.length; i++) {
var root = "./" + data[i] + "/";
// Write title
var title = data[i]+',A0_DT,A0_PS,A1_DT,A1_PS,A2_DT,A2_PS,A3_DT,A3_PS,A4_DT,A4_PS,A5_DT,A5_PS' + '\n';
fs.writeFile(root + data[i] +'.csv', title, {flag: 'a'} );
//use synched to avoid fs warning
fs.writeFileSync(root + data[i] +'.csv', title, {flag: 'a'} );

for (var j = 1; j <= versionNum; j++) {

Expand Down Expand Up @@ -87,8 +88,8 @@ function generatePatch(root, data, f_old, f_new, version, algorithm) {
result = result + '\n';
}


fs.writeFile(root + data +'.csv', result, {flag: 'a'});
//use synched to avoid fs warning
fs.writeFileSync(root + data +'.csv', result, {flag: 'a'});

}

Expand Down
1 change: 0 additions & 1 deletion dataset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"dependencies": {
"fast-json-patch": "^0.5.7",
"jiff": "^0.7.3",
"json-diff-rfc6902": "^1.3.3",
"json8-patch": "^0.4.0",
"performance-now": "^0.2.0",
"rfc6902": "^1.2.1"
Expand Down
4 changes: 3 additions & 1 deletion lib/hashObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ function hash(obj, HASH_ID) {
if (obj[HASH_ID] !== void 0 ) {
return typeof obj[HASH_ID] === "string"? hashToNum(obj[HASH_ID]): obj[HASH_ID];
} else {
// no speculation about magical properties - just use the hashToNum(JSON.stringify(obj))
return hashToNum(JSON.stringify(obj));
// || hashToNum(JSON.stringify(obj))
// || (obj.title === undefined)? obj.title: hashToNum(JSON.stringify(obj.title))
return obj.id || obj._id || obj.answer_id || (obj.title === undefined? obj.title: hashToNum(JSON.stringify(obj.title))) || hashToNum(JSON.stringify(obj));
//return obj.id || obj._id || obj.answer_id || (obj.title === undefined? obj.title: hashToNum(JSON.stringify(obj.title))) || hashToNum(JSON.stringify(obj));
}

}
Expand Down
8 changes: 6 additions & 2 deletions lib/patchArea.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var deepEqual = require('./deepEquals.js');

exports.findValueInPatchHashes = findValueInPatchHashes;
exports.findValueInPatch = findValueInPatch;
exports.handlePatch = handlePatch;
Expand All @@ -23,8 +25,10 @@ function findValueInPatch(newValue, patches) {

for (var i = 0; i < patches.length; i++) {
patchValue = patches[i].value;

if (newValue === patchValue) {

// the two values are not neccessarily primitives
if (deepEqual._equals(newValue, patchValue)) {
//if (newValue === patchValue) {
return i;
}
}
Expand Down

0 comments on commit 41e1abc

Please sign in to comment.