odata-filter-to-ast
v2.0.0
Published
Simple OData $filter query parser.
Downloads
318
Readme
odata-filter-to-ast
Simple OData query parser with zero dependencies.
Usage
$ npm install odata-filter-to-ast
Usage in code:
import {parseFilter, parseOrderBy} from 'odata-filter-to-ast';
parseFilter(`Name gt "Milk" or Price lt -2.55 or Size ne 3`);
parseOrderBy(`age,sum(height,width) desc`);
Result:
{
type: 'OrExpr',
left: {
type: 'GtExpr',
left: {
type: 'MemberExpr',
value: 'Name',
},
right: {
type: 'Primitive',
value: 'Milk'
},
},
right: {
type: 'OrExpr',
left: {
type: 'LtExpr',
left: {
type: 'MemberExpr',
value: 'Price',
},
right: {
type: 'Primitive',
value: -2.55
}
},
right: {
type: 'NeExpr',
left: {
type: 'MemberExpr',
value: 'Size',
},
right: {
type: 'Primitive',
value: 3
}
}
}
}
[
{
type: 'OrderByItem',
expr: {
type: 'MemberExpr',
value: 'age',
},
dir: 'asc',
},
{
type: 'OrderByItem',
expr: {
type: 'FunctionExpr',
name: 'sum',
arguments: [
{
type: 'MemberExpr',
value: 'height',
},
{
type: 'MemberExpr',
value: 'width',
},
],
},
dir: 'desc',
},
]
Supported constructs
The following construct from OData specification are supported:
- JS primitives
-42
,3.14
,6.022e23
,'string'
,true
,false
,null
- field identifiers
iDenT_iFi3r
- heterogeneous arrays
['a','r','r','a','y',7,false,null]
- primitive relations
eq
,ne
,gt
,gt
,lt
,le
- array relation
in
- boolean conjunctions
and
,or
- operator priority grouping
( ... )
- function calls
includes(Name,'Joe')
- sort directions
asc
,desc
Filters should be compatible with odata-filter-builder.
Did you find a string which could not be parsed? File an issue, please.
Related
- Parser built using Peggy and TS PEG.js
- Grammar inspired by the OData specification and the OData ABNF Construction Rules