-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-diff.js
153 lines (141 loc) · 4.63 KB
/
json-diff.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
angular.module('jsondiff', ['ngMaterial', 'ngAnimate', 'ngMessages', 'ngAria']);
angular.module('jsondiff').
controller('DiffController', ['$scope', 'JsonValidatorService', function($scope, JsonValidatorService) {
var sourceJson = {};
var destJson = {};
var difference;
$scope.showDifference = function() {
$scope.difference = null
if (!proceedToCompare()) {
return;
}
difference = null;
if($scope.dataMode){
difference = differJsonInDataMode(sourceJson, destJson);
//removeEmptyObjects(difference);
}else{
difference = differJsonInSchemaMode(sourceJson, destJson);
}
$scope.difference = JSON.stringify(difference, null, 2);
}
function removeEmptyObjects(object){
for(key in object){
if(angular.equals(object[key], {})){
delete object[key];
}else {
arguments.callee(object[key])
}
// arguments.callee(object[key])
}
}
$scope.clearAll = function() {
$scope.dataMode = true;
$scope.sourceFile = null;
$scope.sourceFileName = null;
$scope.destFile = null;
$scope.destFileName = null;
$scope.sourceError = null;
$scope.destError = null;
$scope.difference = null;
difference = null;
}
$scope.clearAll();
function differJsonInDataMode(obj1, obj2) {
var result = {};
for (key in obj1) {
if (!angular.equals(obj2[key], obj1[key])) {
result[key] = obj1[key];
}
if (Array.isArray(obj2[key]) && Array.isArray(obj1[key])) {
if (obj1[key].length !== obj2[key].length) {
result[key] = obj1[key];
}
}
if (typeof obj2[key] == 'object' && typeof obj1[key] == 'object' && !Array.isArray(obj2[key])) {
result[key] = arguments.callee(obj1[key], obj2[key]);
}
}
return result;
}
function differJsonInSchemaMode(obj1, obj2) {
var result = {};
for (key in obj1) {
if (angular.isUndefined(obj2[key])) {
result[key] = obj1[key]
}
if (Array.isArray(obj2[key]) && Array.isArray(obj1[key])) {
if (obj1[key].length !== obj2[key].length) {
result[key] = obj1[key];
}
}
if (typeof obj2[key] == 'object' && typeof obj1[key] == 'object' && !Array.isArray(obj2[key])) {
result[key] = arguments.callee(obj1[key], obj2[key]);
}
}
return result;
}
function proceedToCompare() {
if (!$scope.sourceFile || !$scope.destFile) {
return false;
}
if (JsonValidatorService.isJSON($scope.sourceFile)) {
sourceJson = JSON.parse($scope.sourceFile);
$scope.sourceError = null;
} else {
console.error('Invalid json : Source');
$scope.sourceError = 'Invalid JSON File'
}
if (JsonValidatorService.isJSON($scope.destFile)) {
destJson = JSON.parse($scope.destFile);
$scope.destError = null;
} else {
console.error('Invalid json : Destination');
$scope.destError = 'Invalid JSON File'
}
if ($scope.sourceError || $scope.destError) {
return false;
}
return true;
}
$scope.isDifferenceExixts = function(){
try{
if(Object.keys(difference).length > 0){
return 'dif'
}
return 'eq';
}catch(e){
return false;
}
}
}]).directive('triggerFileInput', function() {
return {
restrict: 'A',
scope: {
target: '@'
},
link: function(scope, element, attr) {
element.on('click', function() {
document.getElementById(scope.target).click();
});
}
};
}).directive("fileread", [function() {
return {
scope: {
fileread: "=",
filename: "="
},
link: function(scope, element, attributes) {
element.bind("change", function(changeEvent) {
var reader = new FileReader();
reader.onload = function(loadEvent) {
scope.$apply(function() {
scope.fileread = loadEvent.target.result;
});
}
reader.readAsText(changeEvent.target.files[0]);
scope.filename = changeEvent.target.files[0].name;
});
}
}
}]);