deep-condition
v1.0.0
Published
Simple and lightweight library for filtering arrays with predefined human-friendly conditions.
Downloads
1
Maintainers
Readme
deep-condition
Simple and lightweight library for filtering arrays with predefined human-friendly conditions
For usage in browser use file source/index.js
Install
Node
npm install deep-condition --save
Usage
let filterFunction = require('deep-condition');
let sampleData = [
{"rank": 1, "rating": 9.2, "year": null, "title": "The Shawshank Redemption"},
{"rank": 2, "rating": 9.2, "year": 1972, "title": "The Godfather"},
{"rank": 3, "rating": 9, "year": 1974, "title": "The Godfather: Part II"},
{"rank": 4, "rating": 8.9, "year": 1966, "title": "Il buono, il brutto, il cattivo."},
{"rank": 5, "rating": 8.9, "year": 1994, "title": "Pulp Fiction"},
{"rank": 6, "rating": 8.9, "year": 1957, "title": "12 Angry Men"},
{"rank": 7, "rating": 8.9, "year": 1993, "title": "Schindler's List"},
{"rank": 8, "rating": 8.8, "year": 1975, "title": "One Flew Over the Cuckoo's Nest"},
{"rank": 9, "rating": 8.8, "year": 2010, "title": "Inception"},
{"rank": 0, "rating": 8.8, "year": 2008, "title": "The Dark Knight"}
];
let filter = {
logic: 'or',
filters: [
{
logic: 'and',
filters: [
{field: 'rating', operator: 'eq', value: 8.8},
{field: 'year', operator: 'gt', value: 2000}
]
},
{field: 'title', operator: 'contains', value: 'Godfather'}
]
};
let result = filterFunction(sampleData, filter);
console.dir(result);
/** OUTPUT:
[ { rank: 2, rating: 9.2, year: 1972, title: 'The Godfather' },
{ rank: 3, rating: 9, year: 1974, title: 'The Godfather: Part II' },
{ rank: 9, rating: 8.8, year: 2010, title: 'Inception' },
{ rank: 0, rating: 8.8, year: 2008, title: 'The Dark Knight' } ]
**/
Description
Types of filter conditions (EXAMPLES)
Filter group
let filterExample = {
logic: 'or',
filters: [
{
logic: 'and',
filters: [
{field: 'rating', operator: 'eq', value: 8.8},
{field: 'year', operator: 'gt', value: 2000}
]
},
{field: 'title', operator: 'contains', value: 'Godfather'}
]
};
Simple condition
let filterExample = {field: 'rating', operator: 'eq', value: 8.8};
Array of simple conditions
By default uses logic: AND
let filterExample = [{field: 'rating', operator: 'eq', value: 8.8}, {field: 'year', operator: 'eq', value: 2010}];
let resultFiltering = filterFunction(sampleData, filterExample);
Supported types of conditions
- eq - equal
let filter = {field: 'rating', operator: 'eq', value: 8.8};
- neq - not equal
let filter = {field: 'rating', operator: 'neq', value: 8.8};
- isnull - is null
let filter = {field: 'year', operator: 'isnull'};
- isnotnull - is not null
let filter = {field: 'year', operator: 'isnotnull'};
- lt - less than
let filter = {field: 'rating', operator: 'lt', value: 8.9};
- lte - less than or equal
let filter = {field: 'rating', operator: 'lte', value: 8.9};
- gt - greater than
let filter = {field: 'rating', operator: 'gt', value: 8.9};
- gte - greater than or equal
let filter = {field: 'rating', operator: 'gte', value: 8.9};
- startswith - starts with
let filter = {field: 'title', operator: 'startswith', value: 'The'};
- endswith - ends with
let filter = {field: 'title', operator: 'endswith', value: 'tion'};
- contains - contains
let filter = {field: 'title', operator: 'contains', value: 'Godfather'};
- doesnotcontain - does not contain
let filter = {field: 'title', operator: 'doesnotcontain', value: 'Godfather'};
- isempty - is empty
let filter = {field: 'rank', operator: 'isempty'};
- isnotempty - is not empty
let filter = {field: 'rank', operator: 'isnotempty'};
FOR MORE EXAMPLES SEE test/filtering.js
file in package
FOR JSDOC DESCRIPTIONS SEE source/index.js
file in package
Test
Test
npm install
npm run test
Change list
Version 1.0.0
- Initial commit