-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.filter.js
159 lines (130 loc) · 5.55 KB
/
json.filter.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
154
155
156
157
158
159
(function($){
//$.extend($.fn, {
$.filterJSON = function(_config, json) {
console.log("JSON Received: " + JSON.stringify(json));
/*****************************/
/* All Parameters */
/*****************************/
// defaults
var config = {
property: null,
value: ""
};
var ret = {result: [], err: ""};
/*****************************/
/* Inner Functions */
/*****************************/
var level = 1;
var topLevelObject = {};
jsonFilterMain = function(config, json){
console.log("in jsonFilterMain");
console.log( "type of json " + $.type(json) );
if($.type(json) === "array"){
$.each(json, function(index, value){
console.log("index: "+index+" -- value: "+value);
if($.type(value) === "object"){
topLevelObject = value;
if(filterJsonByProperty(value) == 1) {
console.log("property matched in: " + JSON.stringify(value));
ret.result.push(value);
}
}
});
}
// $.each(json, function(key, val){
// console.log(JSON.stringify(json[key]) );
//
// });
}
filterJsonByProperty = function(obj){
var response = 0;
console.log("Number of keys in obj: " + Object.keys(obj).length);
var noOfPropertiesInObj = Object.keys(obj).length;
if(noOfPropertiesInObj > 0 ){
if(obj.hasOwnProperty(config.property)){
if(config.value == "" && config.value.length <= 0){
response = 1;
//return false;
} else {
var propValue = obj[config.property];
console.log("prop value: " + propValue);
if(propValue === config.value && $.type(propValue) != "object"){
console.log("Matched");
response = 1;
//return false;
} else if($.type(propValue) === "object"){
console.log("Matched");
response = 1;
}
}
} else{
$.each(obj, function(key, value){
console.log("key: "+key+" -- value type: "+ $.type(value));
var terminate = 0;
if($.type(value) === "object"){
response = 0;
console.log("type object found: " + JSON.stringify(value));
if(filterJsonByProperty(value) == 1){
response = 1;
return false;
}
// if(config.property === key){
// console.log("key: "+key+" -- matched property: "+ config.property+ " -- for: " + JSON.stringify(obj));
// return false;
// }
}
});
}
}
return response;
}
checkConfigs = function(){
var response = -1;
if( $.type(config.property) == "string" ){
if($.trim(config.property).length > 0 ){
// cleaning the property in config
var property = config.property;
property = property.replace(/\s+/g, '');
property = property.split(',');
console.log("After clean the properties are: " + property + " --- " + $.type(property) );
response = 1;
} else {
response = 0;
}
} else {
response = -1;
}
return response;
}
/*****************************/
/* Just calling of functions */
/*****************************/
// extends default config with users config
$.extend(config, _config);
console.log("BEFORE TESTING THE CONFIGS: " + $.type(config.property) );
// check if a valid property is provided
// if($.type(config.property) == "string" && $.trim(config.property).length > 0){
//
// var prop = config.property;
// config.property = prop.split(",");
//
// console.log(config.property);
// } else {
// ret.result = json;
// return $(ret);
// }
var configCheckResult = checkConfigs();
if(configCheckResult === 1) {
resultObject = jsonFilterMain(config, json);
} else if (configCheckResult === 0) {
ret.result = json;
ret.err = "";
}else {
ret.result = {};
ret.err = "ERROR: Incorrect Configurations > check your property";
}
console.log("just before return : " + ret.result);
return ret.result;
}
//});
})(jQuery);