forked from mr54ndm4n/JSON-Comparator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (109 loc) · 2.86 KB
/
app.js
File metadata and controls
122 lines (109 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
onClick = () => {
let jsona = document.getElementById("json_a").value;
let jsonb = document.getElementById("json_b").value;
try{
let differ = comparejson(JSON.parse(jsona), JSON.parse(jsonb));
if(Object.keys(differ).length === 0){
shownoti('primary', 'All Match!!');
}
else{
shownoti('none', 'none');
}
let table = document.getElementById("table");
clearTable(table);
for(let d in differ){
insert(table, d, differ[d].nodea, differ[d].nodeb);
}
}
catch(e){
shownoti('danger', 'Error: ' + e);
}
}
onClickMerge = () => {
let jsona = document.getElementById("json_a").value;
let jsonb = document.getElementById("json_b").value;
try{
let differ = mergejson(JSON.parse(jsona), JSON.parse(jsonb));
if(Object.keys(differ).length === 0){
shownoti('primary', 'All Match!!');
}
else{
shownoti('none', 'none');
}
document.getElementById("json_merge").value = JSON.stringify(differ);
}
catch(e){
shownoti('danger', 'Error: ' + e);
}
}
shownoti = (status, text) => {
let noti = document.getElementById("noti");
noti.classList.remove("is-primary");
noti.classList.remove("is-danger");
if(status == 'none'){
noti.style.display = 'none';
}
else{
noti.style.display = 'block';
console.log("is-" + status);
noti.classList.add("is-" + status);
noti.innerHTML = text;
}
}
insert = (t, path, a, b) => {
let r = t.insertRow(1);
r.insertCell(0).innerHTML = path;
r.insertCell(1).innerHTML = a? a: '-';
r.insertCell(2).innerHTML = b? b: '-';
}
clearTable = (t) => {
for(let i = t.rows.length-1; i > 0; i--){
t.deleteRow(i);
}
}
comparejson = (jsona, jsonb) => {
var diff = {};
compareIter = (nodea, nodeb, path, pos) => {
if(typeof(nodea)=='object'){
for(let child in nodea){
compareIter(getChild(nodea, child),getChild(nodeb, child), path + child + '/', pos);
}
}
else{
if(nodea != nodeb){
if(pos){ diff[path] = {'nodea' : nodea, 'nodeb': nodeb} }
else{ diff[path] = {'nodea': nodeb, 'nodeb' : nodea} }
}
}
}
getChild = (p, k) => {return typeof(p)=='object'? p[k]: undefined}
compareIter(jsona, jsonb, '', true);
compareIter(jsonb, jsona, '', false);
return diff;
}
mergejson = (jsona, jsonb) => {
var diff = {};
combineObjs = (obj, target) => {
for (let key of Object.keys(obj)) {
console.log(obj[key].toString(), typeof(obj[key]));
if (typeof obj[key] === 'object') {
console.log("thinks object")
if (!target[key]) {
target[key] = {};
}
combineObjs(obj[key], target[key]);
}
else {
if (target[key]) {
target[key + "_1"] = obj[key];
}
else {
target[key] = obj[key];
}
}
}
}
combineObjs(jsona, diff);
combineObjs(jsonb, diff);
return diff;
}