@comodinx/query-filters
v1.0.1
Published
@comodinx/query-filters is a module for parsing filters in string to object.
Maintainers
Readme
Query Filters
@comodinx/query-filters is a module for parsing filters in string to object.
Index
Download & Install
NPM
$ npm install @comodinx/query-filtersSource code
$ git clone https://github.com/comodinx/query-filters.git
$ cd query-filters
$ npm installHow is it used?
Simple usage
const { Parser } = require('@comodinx/query-filters');
const parser = new Parser();
parser.parse('active eq 1,description li %casa');
// { "active": { "eq": "1" }, "description": { "li": "%casa" } }
parser.parse('active eq 1,description li %casa,description eq depto');
// { "active": { "eq": "1" }, "description": { "li": "%casa", "eq": "depto" } }Logical operators and groups
AND
- Default operator
- Represented by
,
active eq 1,description li %casaOR
- Represented by
|
active eq 1|description li %casaGroups
- Represented by
(and)
active eq 1,(description li %casa|description eq depto)Precedence
ANDhas higher precedence thanOR- Use groups to control evaluation order
active eq 1,description li %casa|age ge 18
active eq 1,(description li %casa|age ge 18)Parsed result example
parser.parse('active eq 1,(deleted_at is null|age ge 18)');
/*
{
active: { eq: 1 },
or: [
{ deleted_at: { is: 'null' } },
{ age: { ge: 18 } }
]
}
*/Operators
| Name | Example | Description | |:-----|:-----------------------|:--------------------------------------------------------------| | eq | id eq 1 | Check equality. id = 1 | | ne | name ne nico | Check inequality. name != 'nico' | | gt | id gt 1 | Check greater than. id > 1 | | ge | id ge 10 | Check greater than or equal. id >= 10 | | lt | id lt 1 | Check less than. id < 1 | | le | id le 10 | Check less than or equal. id <= 10 | | li | name li nico% | Check matches with nico*. name like nico% | | nl | name nl nico% | Check not matches with nico*. name not like nico% | | in | id in [1;2;3] | Check if included on [1,2,3]. id in (1,2,3) | | ni | id ni [1;2;3] | Check if not included on [1,2,3]. id not in (1,2,3) | | be | id be [1;10] | Check if it is between a and b. id between (1 and 10) | | nb | id nb [1;10] | Check if it is not between a and b. id not between (1 and 10) | | is | deleted_at is null | Check if it is null. | | no | deleted_at is not null | Check if it is not null. |
Configurations
| Name | Type | Default | Description |
|:----------------|:-------|:--------------------------------------------------|:-------------------------------------------------|
| logicals | object | { or: '|', and: ',' } | Logical operators configuration. |
| key | string | "[A-Za-z0-9_]+" | RegExp string for matching keys. |
| value | string | ".+" | RegExp string for matching values. |
| operators | array | ['eq','ne','gt','ge','lt','le','li','nl','in','ni','be','nb','is','no'] |
| operatorPrefix | string | " " | Operator prefix. |
| operatorSuffix | string | " " | Operator suffix. |
| operatorFlags | string | "i" | Operator regexp flag. |
| separatorGroups | string | ";" | List values separator. |
| groups | object | { start: '(', end: ')' } | Conditions group delimiters. |
Format
const parser = new Parser();
parser.format({
active: { eq: 1 },
description: { li: '%casa' },
or: [
{ deleted_at: { is: 'null' } },
{ age: { ge: 18 } }
]
});
// "active eq 1,description li %casa,(deleted_at is null|age ge 18)"Tests
In order to see more concrete examples, I INVITE YOU TO LOOK AT THE TESTS :)
Run the unit tests
npm install
npm test