Skip to content

Commit a61fa0b

Browse files
committed
Renamed the filterJson files to filterJSON
1 parent b5ab0e3 commit a61fa0b

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

src/filterJSON.plugin.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* jQuery Filter JSON Plugin
3+
* Copyright (c) 2012 Kapil Kashyap
4+
*
5+
* Depends:
6+
* - jQuery 1.6+
7+
*
8+
* Dual licensed under the MIT and GPL licenses:
9+
* - http://www.opensource.org/licenses/mit-license.php
10+
* - http://www.gnu.org/licenses/gpl.html
11+
*/
12+
(function($) {
13+
$.extend($.fn, {
14+
filterJSON: function( json, _config ) {
15+
var config = {
16+
property: null,
17+
value: "",
18+
wrapper: false,
19+
checkContains: false,
20+
startsWith: false,
21+
matchCase: false,
22+
avoidDuplicates: false
23+
},
24+
filterJSONLoop = function(json, config) {
25+
var errorMsg = null;
26+
if($.isEmptyObject( config.property ) || !config.property[0]) {
27+
errorMsg = "config.property is required to filter the JSON object.";
28+
}
29+
if(!$.isEmptyObject(errorMsg)) {
30+
if(window.console) {
31+
console.error ? console.error( errorMsg ) : console.log( errorMsg );
32+
}
33+
else {
34+
alert( errorMsg );
35+
}
36+
return;
37+
}
38+
39+
if(json && typeof json == "object") {
40+
// iterating through each property in the JSON Object
41+
$.each(json, function(key, j) {
42+
// checking to see if current 'key' is one of the properties
43+
// in the property array passed in the config.
44+
if($.inArray(key, config.property) != -1) {
45+
var constructReturnObject = function(wrapperValue, plainValue) {
46+
var _value = config.wrapper ? wrapperValue : plainValue;
47+
48+
if(config.avoidDuplicates) {
49+
// avoiding duplicate objects in case of multiple property check.
50+
if($.inArray(_value, ret.filteredJSON) == -1) {
51+
ret.filteredJSON.push( _value );
52+
}
53+
}
54+
else {
55+
ret.filteredJSON.push( _value );
56+
}
57+
};
58+
59+
if(config.value) {
60+
var internalConfig = {},
61+
type = Object.prototype.toString.call( j ),
62+
innerComparo = function(type, originalValue, valueToCompare) {
63+
if(type === "[object String]") {
64+
var originalValueLowerCase = originalValue.toLowerCase(),
65+
valueToCompareLowerCase = valueToCompare.toLowerCase();
66+
67+
if(!config.matchCase) {
68+
if(originalValueLowerCase == valueToCompareLowerCase ||
69+
(!config.startsWith && config.checkContains && originalValueLowerCase.indexOf(valueToCompareLowerCase) != -1) ||
70+
(config.startsWith && originalValueLowerCase.indexOf(valueToCompareLowerCase) == 0)) {
71+
constructReturnObject(json, originalValue);
72+
}
73+
}
74+
else {
75+
if(originalValue == valueToCompare ||
76+
(!config.startsWith && config.checkContains && originalValue.indexOf(valueToCompare) != -1) ||
77+
(config.startsWith && originalValue.indexOf(valueToCompare) == 0)) {
78+
constructReturnObject(json, originalValue);
79+
}
80+
}
81+
}
82+
else if(type === "[object Number]") {
83+
if(originalValue === parseInt( valueToCompare ) || originalValue === parseFloat( valueToCompare )) {
84+
constructReturnObject(json, originalValue);
85+
}
86+
}
87+
else if(type === "[object Boolean]") {
88+
/*
89+
Note:
90+
Boolean(anyString other than blankString) is true
91+
Boolean(anyNumber, even negative number but other than 0) is true
92+
Boolean(true) is true abd Boolean(false) is false
93+
*/
94+
var booleanvalueToCompare = false;
95+
if(Object.prototype.toString.call( valueToCompare ) === "[object String]") {
96+
if(valueToCompare.toLowerCase() == "true") {
97+
booleanvalueToCompare = true;
98+
}
99+
}
100+
if(originalValue === booleanvalueToCompare) {
101+
constructReturnObject(json, originalValue);
102+
}
103+
}
104+
else if(type === "[object Array]") {
105+
if(originalValue.length > 0) {
106+
for(k in originalValue) {
107+
innerComparo(Object.prototype.toString.call( originalValue[k] ), originalValue[k], valueToCompare);
108+
}
109+
}
110+
}
111+
else if(type === "[object Object]") {
112+
$.extend(internalConfig, config, {value: valueToCompare});
113+
filterJSONLoop(originalValue, internalConfig);
114+
}
115+
};
116+
117+
innerComparo(type, j, config.value);
118+
}
119+
else {
120+
constructReturnObject(json, j);
121+
}
122+
}
123+
filterJSONLoop(j, config);
124+
});
125+
}
126+
return ret;
127+
},
128+
ret = {filteredJSON: []},
129+
returnObject = null,
130+
filteredJSON = null,
131+
prop = null;
132+
133+
// extend the default config with the ones passed in by the user.
134+
$.extend(config, _config);
135+
136+
prop = config.property;
137+
// check to see if the property has been passed as a string.
138+
if(Object.prototype.toString.call( prop ) === "[object String]") {
139+
// remove extra spaces if any.
140+
prop = prop.replace(/\s/g, "");
141+
// convert the input property string into an array.
142+
// note: even if it is a single property, this will convert it into an array.
143+
config.property = prop.split(",");
144+
}
145+
prop = null;
146+
147+
// Setting avoidDuplicates to true in case
148+
// - config.wrapper is set to true and
149+
// - multiple properties in config.property and
150+
// - config.avoidDuplicates property is not set by the user.
151+
if(config.wrapper && (config.property && config.property.length > 1)
152+
&& _config.avoidDuplicates == undefined) {
153+
config.avoidDuplicates = true;
154+
}
155+
returnObject = filterJSONLoop(json, config);
156+
157+
if(returnObject && returnObject.filteredJSON) {
158+
filteredJSON = returnObject.filteredJSON.length > 0 ? returnObject.filteredJSON : returnObject.filteredJSON[0];
159+
}
160+
161+
return $(filteredJSON);
162+
}
163+
});
164+
})(jQuery);

src/filterJSON.plugin.min.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)