Simple function to create filter with simple JSON syntax. Arrow functions cannot be dynmically composed and serialized, but JSON based query can be easily composed dynamically and serialized as needed.
var filtered = array.filter({ category: 'Action' });
You can also pass function that will be forwarded to default filter function of array.
To create filter function you can also use $f
var f = $f({ category: 'Action' });
var filtered = array.filter(f);
{ name: 'akash' }
equivalent of
function(item){
return item.name == 'akash';
}
{
category:'Action',
'price <': 200
}
equivalent of
function(item){
return
item.category == 'Action' &&
item.price < 200;
}
{
category:'Action',
$or:{
'yearlyPrice <': 200,
'monthlyPrice <': 20
}
}
equivalent of
function(item){
return
item.category == 'Action' &&
(item.yearlyPrice < 200 ||
item.monthlyPrice < 20)
}
{ 'category in': ['Action','Comedy'] }
{ 'category !in': ['Drama','Documentory'] }
For string values only, this does not raise error if any property chain is null. If any object in property chain returns null, function returns false.
{ 'broker.name contains': 'ash' }
equivalent of
function(item){
return ((broker || {}).name || '').indexOf('ash') !== -1;
}
{ 'children any': { name: 'Akash' } }
equivalent of
function(item){
return item.children.filter(
function(c){
c.name == 'Akash'
} ) ? true : false;
}
or in short...
function (item){
return item.children.filter($f({ name: 'Akash' }$) ? true : false;
}
{ 'first ~': /^A/ }
equivalent of
function (item){
return /^A/.test(item.name);
}
array.sort('Gender DESC,FirstName');
equivalent of
array.sort(function(a,b){
if(!a){
return !b ? 0 : -1;
}
if(!b){
return 1;
}
var ag = (a.Gender || '').toLowerCase();
var bg = (b.Gender || '').toLowerCase();
var n = bg.localeCompare(ag);
if(n!=0){
return n;
}
var af = (a.FirstName || '').toLowerCase();
var bf = (b.FirstName || '').toLowerCase();
return af.localeCompare(bf);
});
Sorting case insensitive, to perform case sensitive, you should use operator CSASC for case sensitive ascending and CSDESC for case sensitive descending sorting.
The method is null safe, it does not throw null exceptions.
ASC - Case insensitive ascending order (default)
DESC - Case insensitive descending order
CSASC - Case sensitive ascending order
CSDESC - Case sensitive descending order