Skip to content

Commit

Permalink
Remove log
Browse files Browse the repository at this point in the history
  • Loading branch information
caohanyang committed Mar 16, 2016
1 parent a3e513b commit dc45a78
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 55 deletions.
38 changes: 14 additions & 24 deletions JSON-Diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,19 @@ exports = module.exports.diff = diff;
exports = module.exports.apply = apply;

function apply(app_old, jpn_patch) {
console.log("===========Begin to apply patches==============");
applyPatches.apply(app_old, jpn_patch);
}

function diff(oldJson, newJson) {
console.log("=========== Data ======================");
console.log(JSON.stringify(oldJson));
console.log(JSON.stringify(newJson));

// Get the unchanged area
var unchanged = [];
unchangedArea.generateUnchanged(oldJson, newJson, unchanged, '');
console.log("=========== Unchanged =================");
console.log(unchanged);
console.log("=========================================");

// Generate the diff
var patches = [];
generateDiff(oldJson, newJson, unchanged, patches, '');
patchArea.handlePatch(patches);
console.log("===========Final Patches=================");
console.log(patches);

return patches;
}
Expand All @@ -57,25 +48,24 @@ function generateDiff(oldJson, newJson, unchanged, patches, path) {
function generateValueDiff(oldJson, newJson, unchanged, patches, path) {
// the endpoint
if (newJson !== oldJson) {
console.log({ op: "replace", path: path, value: copy.clone(newJson)});
// console.log({ op: "replace", path: path, value: copy.clone(newJson)});
patches.push({ op: "replace", path: path, value: copy.clone(newJson)});
}

}

function generateArrayDiff(oldJson, newJson, unchanged, patches, path) {
console.log("--------This is Array-------------");
// console.log("--------This is Array-------------");
// Hash array
var x = oldJson.map(hashArray);
var y = newJson.map(hashArray);
// Use LCS
var tmpPatches = [];
lcs.LCS(x, y, unchanged, tmpPatches, path);
for (var l = 0; l < tmpPatches.length; l++) {
console.log(tmpPatches[l]);
patches.push(tmpPatches[l]);
}
console.log("--------Array complete-------");
// console.log("--------Array complete-------");
}

function hashArray(obj) {
Expand All @@ -87,29 +77,29 @@ function generateObjectDiff(oldJson, newJson, unchanged, patches, path) {
var newKeys = Object.keys(newJson);
var removed = false;

console.log("oldKeys: " + oldKeys);
console.log("newKeys: " + newKeys);
// console.log("oldKeys: " + oldKeys);
// console.log("newKeys: " + newKeys);

// Loop from the old; from lengths -1 to 0
for (var i = oldKeys.length -1; i >= 0; i--) {
var oldKey = oldKeys[i];
var oldValue = oldJson[oldKey];

console.log("oldKey: " + oldKey);
console.log("oldValue: " + JSON.stringify(oldValue));
// console.log("oldKey: " + oldKey);
// console.log("oldValue: " + JSON.stringify(oldValue));

if (newJson.hasOwnProperty(oldKey)) {
var newValue = newJson[oldKey];

console.log("newValue: " + JSON.stringify(newValue));
// console.log("newValue: " + JSON.stringify(newValue));

// go deeper
generateDiff(oldJson[oldKey], newJson[oldKey], unchanged, patches, path + "/" + oldKey );
// ???? patchPointString(oldKey)

} else {
// Remove
console.log({ op: "remove", path: path + "/" + patchPointString(oldKey), value: copy.clone(oldValue) });
// console.log({ op: "remove", path: path + "/" + patchPointString(oldKey), value: copy.clone(oldValue) });
removed = true;
patches.push({ op: "remove", path: path + "/" + patchPointString(oldKey), value: copy.clone(oldValue) });
}
Expand All @@ -131,25 +121,25 @@ function generateObjectDiff(oldJson, newJson, unchanged, patches, path) {
//Try to find the value in the unchanged area
// change JSON.stringify()
var pointer = unchangedArea.findValueInUnchanged(JSON.stringify(newVal), unchanged);
console.log("pointer: " + pointer);
// console.log("pointer: " + pointer);
if (pointer) {
//COPY
console.log({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer});
// console.log({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer});
patches.push({ op: "copy", path: path + "/" + patchPointString(newKey), from: pointer});
} else {
// no json.stringnify
var previousIndex = patchArea.findValueInPatch(newVal, patches);
console.log("previousIndex: " + previousIndex);
// console.log("previousIndex: " + previousIndex);

if (previousIndex !== -1) {
// MOVE
var oldPath = patches[previousIndex].path;
patches.splice(previousIndex, 1);
console.log({ op: "move", from: oldPath, path: path + "/" + patchPointString(newKey)});
// console.log({ op: "move", from: oldPath, path: path + "/" + patchPointString(newKey)});
patches.push({ op: "move", from: oldPath, path: path + "/" + patchPointString(newKey)});
} else {
//ADD
console.log({ op: "add", path: path + "/" + patchPointString(newKey), value: copy.clone(newVal)});
// console.log({ op: "add", path: path + "/" + patchPointString(newKey), value: copy.clone(newVal)});
patches.push({ op: "add", path: path + "/" + patchPointString(newKey), value: copy.clone(newVal)});
}

Expand Down
34 changes: 18 additions & 16 deletions LCS.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ function LCS (x, y, unchanged, patches, path) {
var newY = y.slice(start, y_end + 1);

var matrix = lcsMatrix(newX, newY);
var result = lcsResult(newX, newY, matrix);
var finalResult = x.slice(0, start) + result + x.slice(x_end + 1, x.length);

//backtrack
// var result = lcsResult(newX, newY, matrix);
// var finalResult = x.slice(0, start) + result + x.slice(x_end + 1, x.length);
// For Array
console.log("Result: " + finalResult);
// console.log("Result: " + finalResult);

// Set offset = 1, offset is the array index adjuster for JSON format patch
var offset = {};
Expand Down Expand Up @@ -67,7 +69,7 @@ function lcsMatrix(x, y) {
}
}

console.log("LCSLength = " + matrix[x_length][y_length]);
// console.log("LCSLength = " + matrix[x_length][y_length]);
return matrix;
}

Expand Down Expand Up @@ -112,37 +114,37 @@ function printDiff(x, y, matrix, i, j, start, offset, unchanged, patches, path)
var tmpPath = path + "/" + (i + start + offset.value);
if (lastElement !== void 0 && lastElement.op === "remove" && lastElement.path === tmpPath) {
//First Replace
console.log({ op: "replace", path: tmpPath, value: y[j] });
// console.log({ op: "replace", path: tmpPath, value: y[j] });
patches[patches.length - 1].op = "replace";
patches[patches.length - 1].value = JSON.parse(y[j]);
} else {

// First MOVE or ADD or COPY
var previousIndex = patchArea.findValueInPatch(y[j], patches);
console.log("previousIndex: " + previousIndex);
// console.log("previousIndex: " + previousIndex);
// ********Need to be fiexed*****************
// only move when the previousIndex is 0 and patchLength is 1
// if (previousIndex !== -1) {
if (previousIndex === 0 && patches.length === 1) {
// MOVE
var oldPath = patches[previousIndex].path;
console.log({ op: "move", from: oldPath, path: tmpPath});
// console.log({ op: "move", from: oldPath, path: tmpPath});
patches.splice(previousIndex, 1);
patches.push({ op: "move", from: oldPath, path: tmpPath});
} else {
// ADD OR COPY
//Try to find the value in the unchanged area
// var pointer = findValueInUnchanged(JSON.stringify(y[j]), unchanged);
var pointer = unchangedArea.findValueInUnchanged(y[j], unchanged);
console.log("pointer: " + pointer);
// console.log("pointer: " + pointer);
if (pointer) {
// COPY
// Adjust the index in the unchanged area
var newPointerArr = pointer.split('/');
var initIndex = parseInt(newPointerArr[newPointerArr.length - 1]);
console.log("offset: " + offset.value);
console.log("start: " + start);
console.log("initIndex: " + initIndex);
// console.log("offset: " + offset.value);
// console.log("start: " + start);
// console.log("initIndex: " + initIndex);

var newIndex;
// change index
Expand All @@ -151,23 +153,23 @@ function printDiff(x, y, matrix, i, j, start, offset, unchanged, patches, path)
} else {
newIndex = initIndex + offset.value - 1;
}
console.log("newIndex: " + newIndex);
// console.log("newIndex: " + newIndex);
if (newIndex >= 0) {
// newIndex >= 0, hence the element exists in the array, copy
var index = pointer.lastIndexOf('/');
if (index !== -1) {
var newPointer = pointer.slice(0, index + 1) + newIndex;
console.log({ op: "copy", path: tmpPath, from: newPointer });
// console.log({ op: "copy", path: tmpPath, from: newPointer });
patches.push({ op: "copy", path: tmpPath, from: newPointer });
}
} else {
// newIndex < 0, hence the element doesn't exist in the array, add
console.log({ op: "add", path: tmpPath, value: y[j] });
// console.log({ op: "add", path: tmpPath, value: y[j] });
patches.push({ op: "add", path: tmpPath, value: JSON.parse(y[j]) });
}
} else {
// ADD
console.log({ op: "add", path: tmpPath, value: y[j] });
// console.log({ op: "add", path: tmpPath, value: y[j] });
patches.push({ op: "add", path: tmpPath, value: JSON.parse(y[j]) });
}
}
Expand All @@ -184,7 +186,7 @@ function printDiff(x, y, matrix, i, j, start, offset, unchanged, patches, path)
//First change offset
offset.value--;
//Then remove
console.log({ op: "remove", path: path + "/" + (i + start + offset.value), value: x[i] });
// console.log({ op: "remove", path: path + "/" + (i + start + offset.value), value: x[i] });
patches.push({ op: "remove", path: path + "/" + (i + start + offset.value), value: JSON.parse(x[i]) });
} else {
// console.log("reach the end i = " + i);
Expand Down
20 changes: 10 additions & 10 deletions applyPatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ var objectOps = {
// console.log(this);
// console.log(child_json);
child_json[key] = this.value;
console.log("Add operation = " + this.value);
// console.log("Add operation = " + this.value);
return true;
},
remove: function(child_json, key, all_json) {
delete child_json[key];
console.log("Remove operation = " + child_json);
// console.log("Remove operation = " + child_json);
return true;
},
replace: function(child_json, key, all_json) {
child_json[key] = this.value;
console.log("replace operation = " + this.value);
// console.log("replace operation = " + this.value);
return true;
},
copy: function(child_json, key, all_json) {
Expand All @@ -32,7 +32,7 @@ var objectOps = {
return true;
},
move: function(child_json, key, all_json) {
console.log("move operation = " + JSON.stringify(child_json));
// console.log("move operation = " + JSON.stringify(child_json));
var tmpOp = {"op": "val_get", "path": this.from};
//Get the tmp value
apply(all_json, [tmpOp]);
Expand All @@ -47,12 +47,12 @@ var objectOps = {
var arrayOps = {
add: function(arr, key, all_json) {
arr.splice(key, 0, this.value);
console.log("Add operation = " + this.value);
// console.log("Add operation = " + this.value);
return true;
},
remove: function(arr, key, all_json) {
arr.splice(key, 1);
console.log("Remove operation = " + key);
// console.log("Remove operation = " + key);
return true;
},
replace: function(arr, key, all_json) {
Expand Down Expand Up @@ -97,7 +97,7 @@ function apply(all_json, patches) {
if (patch !== void 0) {
//when patch = "", it's the root
var path = patch.path || "";
console.log(path);
// console.log(path);
var keys = path.split("/");
var child_json = all_json;

Expand All @@ -113,21 +113,21 @@ function apply(all_json, patches) {

//This is the root operations
if (key === "") {
console.log("The key is undefined");
// console.log("The key is undefined");
rootOps[patch.op].call(patch, child_json, stringToPoint(key), all_json);
break;
}

if (Array.isArray(child_json)) {
console.log("***********Array operations****************");
// console.log("***********Array operations****************");
if (key === '-') {
key = child_json.length;
} else {
key = parseInt(key);
}
arrayOps[patch.op].call(patch, child_json, key, all_json);
} else {
console.log("***********Object operations***************");
// console.log("***********Object operations***************");
objectOps[patch.op].call(patch, child_json, stringToPoint(key), all_json);
}
}
Expand Down
11 changes: 7 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
"version": "1.0.1",
"homepage": "https://github.com/caohanyang/JSON-Diff",
"description": "This framework is to compare two JSON data and generate the Patch",
"moduleType": [
"node"
],
"keywords": [
"JSON",
"Diff",
Expand All @@ -15,7 +12,13 @@
"authors": [
"Hanyang CAO"
],
"ignore": [],
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"license": "MIT",
"dependencies": {
"deep-equal": "~0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion unchangedArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function generateUnchanged(oldJson, newJson, unchanged, path) {
//********************Need to be changed ********************
function generateUnchangedArray(oldJson, newJson, unchanged, path) {
var miniLength = Math.min(oldJson.length, newJson.length);
console.log("miniLength is " + miniLength);
// console.log("miniLength is " + miniLength);
for (var i = 0; i < miniLength; i++) {
generateUnchanged(oldJson[i], newJson[i], unchanged, path + "/" + i);
}
Expand Down

0 comments on commit dc45a78

Please sign in to comment.