diff --git a/JSON-Diff.js b/JSON-Diff.js index 0e7eef1..3339d79 100644 --- a/JSON-Diff.js +++ b/JSON-Diff.js @@ -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; } @@ -57,14 +48,14 @@ 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); @@ -72,10 +63,9 @@ function generateArrayDiff(oldJson, newJson, unchanged, patches, path) { 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) { @@ -87,21 +77,21 @@ 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 ); @@ -109,7 +99,7 @@ function generateObjectDiff(oldJson, newJson, unchanged, patches, path) { } 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) }); } @@ -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)}); } diff --git a/LCS.js b/LCS.js index b3f8af6..8b8b910 100644 --- a/LCS.js +++ b/LCS.js @@ -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 = {}; @@ -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; } @@ -112,21 +114,21 @@ 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 { @@ -134,15 +136,15 @@ function printDiff(x, y, matrix, i, j, start, offset, unchanged, patches, path) //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 @@ -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]) }); } } @@ -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); diff --git a/applyPatches.js b/applyPatches.js index 8b829ff..38469c5 100644 --- a/applyPatches.js +++ b/applyPatches.js @@ -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) { @@ -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]); @@ -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) { @@ -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; @@ -113,13 +113,13 @@ 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 { @@ -127,7 +127,7 @@ function apply(all_json, patches) { } 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); } } diff --git a/bower.json b/bower.json index 783eeb2..cccc26b 100644 --- a/bower.json +++ b/bower.json @@ -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", @@ -15,7 +12,13 @@ "authors": [ "Hanyang CAO" ], - "ignore": [], + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], "license": "MIT", "dependencies": { "deep-equal": "~0.0.0" diff --git a/unchangedArea.js b/unchangedArea.js index 5b7546e..2eedc7d 100644 --- a/unchangedArea.js +++ b/unchangedArea.js @@ -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); }