|
12 | 12 | (function($) {
|
13 | 13 | $.extend($.fn, {
|
14 | 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 |
| 15 | + var typeMap = { |
| 16 | + "STRING": "[object String]", |
| 17 | + "NUMBER": "[object Number]", |
| 18 | + "BOOLEAN": "[object Boolean]", |
| 19 | + "ARRAY": "[object Array]", |
| 20 | + "OBJECT": "[object Object]", |
| 21 | + "FUNCTION": "[object Function]" |
| 22 | + }, |
| 23 | + getObjectType = function(o) { |
| 24 | + return Object.prototype.toString.call( o ); |
| 25 | + }, |
| 26 | + config = { |
| 27 | + property: null, // property used to filter the objects, required. |
| 28 | + value: "", // optional, provide this if we want to search for a particular value |
| 29 | + wrapper: false, // returning the parent object is turned off by default |
| 30 | + checkContains: false, // is required only when we are matching against a value, will match value with in the string |
| 31 | + startsWith: false, // is required only when we are matching against a value, will match value at the beginning of the string |
| 32 | + matchCase: false, // case insensitive matching is on by default |
| 33 | + avoidDuplicates: false, // avoid duplicates is off by default |
| 34 | + sort: false, // sorting is off by default |
| 35 | + sortOrder: "asc", // "desc" is the other value |
| 36 | + sortProperty: null, // optional, if this isn't provided then the property provided in the config would be used. |
| 37 | + comparator: function(a, b) { // optional, default comparator function for sorting |
| 38 | + var order = (config.sortOrder !== "asc") ? -1 : 1, |
| 39 | + p = config.sortProperty || config.property[0] || config.property, |
| 40 | + a = a[p] || a, |
| 41 | + b = b[p] || b; |
| 42 | + |
| 43 | + if(a < b) { |
| 44 | + return -1 * order; |
| 45 | + } |
| 46 | + if(a > b) { |
| 47 | + return 1 * order; |
| 48 | + } |
| 49 | + return 0; |
| 50 | + } |
23 | 51 | },
|
24 | 52 | filterJSONLoop = function(json, config) {
|
25 | 53 | var errorMsg = null;
|
|
39 | 67 | if(json && typeof json == "object") {
|
40 | 68 | // iterating through each property in the JSON Object
|
41 | 69 | $.each(json, function(key, j) {
|
42 |
| - // checking to see if current 'key' is one of the properties |
| 70 | + // checking to see if current "key" is one of the properties |
43 | 71 | // in the property array passed in the config.
|
44 | 72 | if($.inArray(key, config.property) != -1) {
|
45 | 73 | var constructReturnObject = function(wrapperValue, plainValue) {
|
|
59 | 87 | if(config.value) {
|
60 | 88 | var valueArray = $.isArray(config.value) ? config.value : [config.value],
|
61 | 89 | internalConfig = {},
|
62 |
| - type = Object.prototype.toString.call( j ), |
| 90 | + type = getObjectType( j ), |
63 | 91 | innerComparo = function(type, originalValue, valueToCompare) {
|
64 |
| - if(type === "[object String]") { |
| 92 | + if(type === typeMap.STRING) { |
65 | 93 | var originalValueLowerCase = originalValue.toLowerCase(),
|
66 | 94 | valueToCompareLowerCase = valueToCompare.toLowerCase();
|
67 | 95 |
|
|
80 | 108 | }
|
81 | 109 | }
|
82 | 110 | }
|
83 |
| - else if(type === "[object Number]") { |
| 111 | + else if(type === typeMap.NUMBER) { |
84 | 112 | if(originalValue === parseInt( valueToCompare ) || originalValue === parseFloat( valueToCompare )) {
|
85 | 113 | constructReturnObject(json, originalValue);
|
86 | 114 | }
|
87 | 115 | }
|
88 |
| - else if(type === "[object Boolean]") { |
| 116 | + else if(type === typeMap.BOOLEAN) { |
89 | 117 | /*
|
90 | 118 | Note:
|
91 | 119 | Boolean(anyString other than blankString) is true
|
92 | 120 | Boolean(anyNumber, even negative number but other than 0) is true
|
93 | 121 | Boolean(true) is true abd Boolean(false) is false
|
94 | 122 | */
|
95 | 123 | var booleanvalueToCompare = false;
|
96 |
| - if(Object.prototype.toString.call( valueToCompare ) === "[object String]") { |
| 124 | + if(getObjectType( valueToCompare ) === typeMap.STRING) { |
97 | 125 | if(valueToCompare.toLowerCase() == "true") {
|
98 | 126 | booleanvalueToCompare = true;
|
99 | 127 | }
|
|
102 | 130 | constructReturnObject(json, originalValue);
|
103 | 131 | }
|
104 | 132 | }
|
105 |
| - else if(type === "[object Array]") { |
| 133 | + else if(type === typeMap.ARRAY) { |
106 | 134 | if(originalValue.length > 0) {
|
107 | 135 | for(k in originalValue) {
|
108 |
| - innerComparo(Object.prototype.toString.call( originalValue[k] ), originalValue[k], valueToCompare); |
| 136 | + innerComparo(getObjectType( originalValue[k] ), originalValue[k], valueToCompare); |
109 | 137 | }
|
110 | 138 | }
|
111 | 139 | }
|
112 |
| - else if(type === "[object Object]") { |
| 140 | + else if(type === typeMap.OBJECT) { |
113 | 141 | $.extend(internalConfig, config, {value: valueToCompare});
|
114 | 142 | filterJSONLoop(originalValue, internalConfig);
|
115 | 143 | }
|
|
142 | 170 | ret = {filteredJSON: []},
|
143 | 171 | returnObject = null,
|
144 | 172 | filteredJSON = null,
|
145 |
| - prop = null; |
| 173 | + prop = null, |
| 174 | + sorted = false; |
146 | 175 |
|
147 | 176 | // extend the default config with the ones passed in by the user.
|
148 | 177 | $.extend(config, _config);
|
149 | 178 |
|
150 | 179 | prop = config.property;
|
151 | 180 | // check to see if the property has been passed as a string.
|
152 |
| - if(Object.prototype.toString.call( prop ) === "[object String]") { |
| 181 | + if(getObjectType( prop ) === typeMap.STRING) { |
153 | 182 | // remove extra spaces if any.
|
154 | 183 | prop = prop.replace(/\s/g, "");
|
155 | 184 | // convert the input property string into an array.
|
|
172 | 201 | filteredJSON = returnObject.filteredJSON.length > 0 ? returnObject.filteredJSON : returnObject.filteredJSON[0];
|
173 | 202 | }
|
174 | 203 |
|
| 204 | + if(config.sort && getObjectType( filteredJSON ) === typeMap.ARRAY) { |
| 205 | + if(config.comparator && getObjectType( config.comparator ) === typeMap.FUNCTION) { |
| 206 | + if(getObjectType( filteredJSON[0] ) === typeMap.OBJECT) { |
| 207 | + filteredJSON.sort(config.comparator); |
| 208 | + sorted = true; |
| 209 | + } |
| 210 | + } |
| 211 | + !sorted && filteredJSON.sort(config.comparator); |
| 212 | + } |
175 | 213 | return $(filteredJSON);
|
176 | 214 | }
|
177 | 215 | });
|
|
0 commit comments