Skip to content

Commit 539ac6c

Browse files
committed
1. Added sort functionality.
- Sorting in ascending or descending order. - Sort based on a property/key provided by the user or should fall back onto the default property used for filtering. - Both text and numeric sorting. - Robust sort comparison function. - Custom comparison function can be provided. 2. Updated readme file.
1 parent a6fe50f commit 539ac6c

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ This options takes precedence over checkContains if it is set to true.
6262

6363
* avoidDuplicates (optional, default is false): If set to true will avoid duplicate objects.
6464

65+
* sort (optional, default is false): If set to true will return sorted values.
66+
67+
* sortOrder (optional, default is 'asc'): If sorting is set to true then the default sort order is ascending described by 'asc' value. If you want your sort order to be descending change this value to 'desc' or
68+
for the matter of fact anything else other than 'asc'.
69+
70+
* sortProperty (optional, default is null): The key to be used to sort the objects if sorting is set to true. If not provided then the required property 'property' would be used. In case multiple properties are
71+
given, the first one will be used.
72+
73+
* comparator (optional, default is compare function): A comparator function used for sorting. This is completely optional but can be provided by the user.
74+
6575
### Known Issues
6676

6777
For this plugin to work in IE7 and below, you will have to include JSON2.

src/filterJSON.plugin.js

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,42 @@
1212
(function($) {
1313
$.extend($.fn, {
1414
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+
}
2351
},
2452
filterJSONLoop = function(json, config) {
2553
var errorMsg = null;
@@ -39,7 +67,7 @@
3967
if(json && typeof json == "object") {
4068
// iterating through each property in the JSON Object
4169
$.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
4371
// in the property array passed in the config.
4472
if($.inArray(key, config.property) != -1) {
4573
var constructReturnObject = function(wrapperValue, plainValue) {
@@ -59,9 +87,9 @@
5987
if(config.value) {
6088
var valueArray = $.isArray(config.value) ? config.value : [config.value],
6189
internalConfig = {},
62-
type = Object.prototype.toString.call( j ),
90+
type = getObjectType( j ),
6391
innerComparo = function(type, originalValue, valueToCompare) {
64-
if(type === "[object String]") {
92+
if(type === typeMap.STRING) {
6593
var originalValueLowerCase = originalValue.toLowerCase(),
6694
valueToCompareLowerCase = valueToCompare.toLowerCase();
6795

@@ -80,20 +108,20 @@
80108
}
81109
}
82110
}
83-
else if(type === "[object Number]") {
111+
else if(type === typeMap.NUMBER) {
84112
if(originalValue === parseInt( valueToCompare ) || originalValue === parseFloat( valueToCompare )) {
85113
constructReturnObject(json, originalValue);
86114
}
87115
}
88-
else if(type === "[object Boolean]") {
116+
else if(type === typeMap.BOOLEAN) {
89117
/*
90118
Note:
91119
Boolean(anyString other than blankString) is true
92120
Boolean(anyNumber, even negative number but other than 0) is true
93121
Boolean(true) is true abd Boolean(false) is false
94122
*/
95123
var booleanvalueToCompare = false;
96-
if(Object.prototype.toString.call( valueToCompare ) === "[object String]") {
124+
if(getObjectType( valueToCompare ) === typeMap.STRING) {
97125
if(valueToCompare.toLowerCase() == "true") {
98126
booleanvalueToCompare = true;
99127
}
@@ -102,14 +130,14 @@
102130
constructReturnObject(json, originalValue);
103131
}
104132
}
105-
else if(type === "[object Array]") {
133+
else if(type === typeMap.ARRAY) {
106134
if(originalValue.length > 0) {
107135
for(k in originalValue) {
108-
innerComparo(Object.prototype.toString.call( originalValue[k] ), originalValue[k], valueToCompare);
136+
innerComparo(getObjectType( originalValue[k] ), originalValue[k], valueToCompare);
109137
}
110138
}
111139
}
112-
else if(type === "[object Object]") {
140+
else if(type === typeMap.OBJECT) {
113141
$.extend(internalConfig, config, {value: valueToCompare});
114142
filterJSONLoop(originalValue, internalConfig);
115143
}
@@ -142,14 +170,15 @@
142170
ret = {filteredJSON: []},
143171
returnObject = null,
144172
filteredJSON = null,
145-
prop = null;
173+
prop = null,
174+
sorted = false;
146175

147176
// extend the default config with the ones passed in by the user.
148177
$.extend(config, _config);
149178

150179
prop = config.property;
151180
// 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) {
153182
// remove extra spaces if any.
154183
prop = prop.replace(/\s/g, "");
155184
// convert the input property string into an array.
@@ -172,6 +201,15 @@
172201
filteredJSON = returnObject.filteredJSON.length > 0 ? returnObject.filteredJSON : returnObject.filteredJSON[0];
173202
}
174203

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+
}
175213
return $(filteredJSON);
176214
}
177215
});

src/filterJSON.plugin.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)