Permalink
Browse files

fixing console formatted output. some restyling on demo page.

  • Loading branch information...
1 parent c7f0a00 commit 76eb5ec087d316806ab5c5afa990516cf470ee18 @beneidel beneidel committed Feb 7, 2013
Showing with 116 additions and 74 deletions.
  1. +4 −0 bin/jsondiffpatch.js
  2. +37 −42 demo/index.htm
  3. +34 −3 demo/style.css
  4. +34 −23 src/jsondiffpatch.console.js
  5. +1 −1 src/jsondiffpatch.js
  6. +6 −5 test/testdata2.json
@@ -7,6 +7,9 @@ var requireFromDir = function(filename) {
var jsondiffpatch = requireFromDir('../src/jsondiffpatch');
jsondiffpatch.config.diff_match_patch = requireFromDir('../lib/diff_match_patch_uncompressed.js');
+jsondiffpatch.config.objectHash = function(obj) {
+ return obj._id || obj.id || obj.name || JSON.stringify(obj);
+};
jsondiffpatch.console = requireFromDir('../src/jsondiffpatch.console');
var util = require('util'), fs = require('fs');
@@ -24,4 +27,5 @@ var dataOrig = JSON.parse(fs.readFileSync(file1));
var dataNew = JSON.parse(fs.readFileSync(file2));
var delta = jsondiffpatch.diff(dataOrig, dataNew);
+
console.log(jsondiffpatch.console.diffToText(dataOrig, dataNew, delta, hideUnchanged));
View
Oops, something went wrong.
View
@@ -52,19 +52,33 @@ h2 {
}
.header-options {
+ font-weight: normal;
margin-left: 30px;
display: inline-block;
}
+table {
+ padding: 20px;
+}
+
+td, th {
+ vertical-align: top;
+ text-align: left;
+}
+
#jsondiff {
- width: 95%;
- height: 200px;
+ overflow: scroll;
+ max-width: 800px;
+}
+
+.jsondiff {
+ width: 800px;
}
footer {
font-size: small;
text-align: center;
- margin: 40px;
+ margin: 40px;
}
.credits {
@@ -79,3 +93,20 @@ footer {
.credits a:hover {
text-decoration: underline;
}
+
+.results {
+ margin-top: 20px;
+}
+
+.results table {
+ width: 100%;
+}
+
+.results {
+ width: 100%;
+}
+.results > div {
+ vertical-align: top;
+ display: inline-block;
+}
+
@@ -1,4 +1,5 @@
var jsondiffpatch = require('./jsondiffpatch')
+var jdp = jsondiffpatch;
var clc = require('cli-color');
@@ -9,12 +10,15 @@ green = identity, red = identity, gray = identity, strike = identity;
green = clc.green;
red = clc.red;
+yellow = clc.yellow;
+orange = clc.orange;
strike = clc.strike;
var tremoved = function(text){
return strike(red(text));
},
tadded = green, tunchanged = gray, tnormal = identity, tdiffheader = gray,
+tmovedto = yellow, tmovedfrom = red,
indent = function(level){
return new Array(level || 0).join(' ');
};
@@ -31,7 +35,7 @@ objectToText = function(desc, o, hideUnchanged, level) {
), ': ');
}
- if (typeof o == 'object' && !jsondiffpatch.isDate(o)) {
+ if (o && typeof o == 'object' && !jsondiffpatch.isDate(o)) {
// a node (object or array)
buffer.push(o._t === 'a' ? '[\n' : '{\n');
for (var prop in o) {
@@ -64,7 +68,7 @@ var diffNodeToText = function(desc, o, n, d, hideUnchanged, level, metadata){
var positionForLabel = buffer.length;
var label = [desc];
if (n instanceof Array ) {
- label.push(' (array', n._key ? ', key=' + n._key : '', ')');
+ label.push(' (array)');
}
if (desc) {
label.push(': ');
@@ -122,19 +126,14 @@ var diffNodeToText = function(desc, o, n, d, hideUnchanged, level, metadata){
} else {
if (d[2] === 3) {
// item moved
- buffer.push(tadded('<= _' + metadata));
- buffer.push(tadded(objectToText(null, n, hideUnchanged, level)));
- labelFunc = tremoved;
-
- var melem = document.createElement('div');
if (desc == d[1]) {
- buffer.push(tadded('<= _' + metadata));
- buffer.push(tadded(objectToText(null, n, hideUnchanged, level)));
- labelFunc = tadded;
+ buffer.push(tmovedto('<= _' + metadata));
+ buffer.push(tmovedto(objectToText(null, n, hideUnchanged, level)));
+ labelFunc = tmovedto;
} else {
- buffer.push(tremoved('=> ' + d[1]));
+ buffer.push(tmovedfrom('=> ' + d[1]));
+ labelFunc = tmovedfrom;
}
- elem.appendChild(melem);
}
}
}
@@ -146,7 +145,7 @@ var diffNodeToText = function(desc, o, n, d, hideUnchanged, level, metadata){
// a node (object or array)
if (typeof metadata != 'undefined') {
- buffer.push('<= _' + metadata);
+ buffer.push(tmovedto('<= _' + metadata + ' '));
}
// only members in diff (skip unchanged members)
@@ -166,12 +165,17 @@ var diffNodeToText = function(desc, o, n, d, hideUnchanged, level, metadata){
toInsert[d[prop][1]] = { prop: prop, from: index };
}
removedIndices[index] = true;
- var li = diffNodeToText(prop, jdp.getByKey(o, index), null, hideUnchanged, level, d[prop]);
+ items.push({
+ removed: true,
+ text: diffNodeToText(prop, jdp.getByKey(o, index), null, d[prop], hideUnchanged, level + 1)
+ });
} else {
// unchanged
var prop = index.toString();
- var li = objectToText(index, o[index], hideUnchanged, level);
- items.push(li);
+ items.push({
+ unchanged: true,
+ text: objectToText(index, o[index], hideUnchanged, level + 1)
+ });
}
}
for (var prop in d) {
@@ -201,19 +205,27 @@ var diffNodeToText = function(desc, o, n, d, hideUnchanged, level, metadata){
}
}
oldIndex = parseInt(oldIndex, 10);
- var li = diffNodeToText(index, jdp.getByKey(o, oldIndex), jdp.getByKey(n, index), d[prop], hideUnchanged, level, insertion.from));
- items.splice(index + indexOffset, d[prop].length == 1 || d[prop][2] === 3 ? 0 : 1, li);
+ var item = {
+ inserted: true,
+ from: insertion.from,
+ text: diffNodeToText(index, jdp.getByKey(o, oldIndex), jdp.getByKey(n, index),
+ d[prop], hideUnchanged, level + 1, insertion.from)
+ };
+ items.splice(index + indexOffset, d[prop].length == 1 || d[prop][2] === 3 ? 0 : 1, item);
}
}
for (index = 0; index < items.length; index++) {
- buffer.push(items[index]);
+ var item = items[index];
+ if (!item.unchanged || !hideUnchanged) {
+ buffer.push(items[index].text);
+ }
}
} else {
for (var prop in d) {
if (d.hasOwnProperty(prop) && prop !== '_t') {
- buffer.push(diffNodeToText(prop, jsondiffpatch.getByKey(o, prop), jsondiffpatch.getByKey(n, prop), d[prop]
- , hideUnchanged, level+1));
+ buffer.push(diffNodeToText(prop, jsondiffpatch.getByKey(o, prop), jsondiffpatch.getByKey(n, prop),
+ d[prop], hideUnchanged, level + 1));
}
}
@@ -227,15 +239,14 @@ var diffNodeToText = function(desc, o, n, d, hideUnchanged, level, metadata){
k = o[prop][o._key];
}
if (!d || !d.hasOwnProperty(k)) {
- buffer.push(objectToText(k, o[prop], hideUnchanged, level+1));
+ buffer.push(objectToText(k, o[prop], hideUnchanged, level + 1));
}
}
}
}
}
}
-
buffer.push(indentation, d._t === 'a' ? ']\n' : '}\n');
}
@@ -420,7 +420,7 @@
};
var arrayDiff = function(o, n){
- return sequenceDiffer.diff(o, n, jdp.config.objectHash, jsondiffpatch.diff);
+ return sequenceDiffer.diff(o, n, jdp.config.objectHash, jdp.diff);
};
var objectDiff = function(o, n){
View
@@ -53,11 +53,6 @@
"unasur": true
},
{
- "name": "Colombia",
- "capital": "Bogotá",
- "independence": "1810-07-20T03:00:00.000Z"
- },
- {
"name": "Ecuador",
"capital": "Quito",
"independence": "1809-08-10T03:00:00.000Z",
@@ -76,6 +71,12 @@
"unasur": true
},
{
+ "name": "Colombia",
+ "capital": "Bogotá",
+ "independence": "1810-07-20T03:00:00.000Z",
+ "population": 42888594
+ },
+ {
"name": "Suriname",
"capital": "Paramaribo",
"independence": "1975-11-25T03:00:00.000Z",

0 comments on commit 76eb5ec

Please sign in to comment.