2.2.0 • Published 4 years ago

filter-query-parser v2.2.0

Weekly downloads
34
License
ISC
Repository
github
Last release
4 years ago

filter-query-parser

Library for parsing the query string to an Object form and stringify Object to query string.

Try it in console by typing FQP.

This library is inspired from the open source library artista-jql.

The parsed output object of filter-query-parser and input for stringify is build based on the output of the Angular library angular2-query-builder.

Installation

$ npm install filter-query-parser

Browser

<script src="..../FQP/dist/FQP.js"></script>

node.js

const FQP = require('..../FQP/dist/FQP.js').FQP;

Quick Start

Parse the query string to JavaScript Object

const query = `Age <= 25 AND (Gender = "Male" OR School contains "School")`;
const Obj = FQP.parser(query);

Returns

{
  "condition":"AND",
  "rules": [
      {"field":"Age","operator":"<=","value":25},
      {
          "condition":"OR",
          "rules": [
              {"field":"Gender","operator":"=","value":"Male"},
              {"field":"School","operator":"CONTAINS","value":"School"}
          ],
          "not":false
      }
  ],
  "not":false
}

Stringify JavaScript Object to query string

const Obj = {
        "condition":"AND",
        "rules": [
            {"field":"Age","operator":"<=","value":25},
            {
                "condition":"OR",
                "rules": [
                    {"field":"Gender","operator":"=","value":"Male"},
                    {"field":"School","operator":"CONTAINS","value":"School"}
                ],
                "not":false
            }
        ],
        "not":false
    };
const query = FQP.stringify(Obj);

Returns

Age <= 25 AND (Gender = "Male" OR School contains "School")

Grammar

Supported data types for right value

typeexample
String"foo", "bar", ...
Number1, 200, -15, 14.5, ...
Booleantrue false

Logical Operators (case insensitive)

operatordescription
ANDlogical conjunction
ORlogical sum
!logical negation

Comparison Operators (case insensitive)

operatordescription
=equal
!=not equal
>=greater than or equal to
<=less than or equal to
>greater than
<less than
CONTAINSCheck if right value contains left value when right value is StringOr check if right value is in left array when left value is Array
STARTS WITHCheck the value is starts with the right value
ENDS WITHCheck the value is ends with the right value
LIKECheck the value is LIKE the right value
DOES NOT CONTAINCheck the value DOES NOT CONTAIN the right value
EXACTLY MATCHESCheck the value EXACTLY MATCHES with the right value
BETWEENCheck the value are BETWEEN the right values
NOT BETWEENCheck the value are NOT BETWEEN the right values
INCheck the value are IN the right values
NOT INCheck the value are NOT IN the right values
NULLCheck the value is NULL
NOT NULLCheck the value is NOT NULL

Query examples

sample queriesdescription
Goodname = "hoge"compare String
Goodname contains "eorg"partial match with String
Goodage = 37, age < 30, age >= 3compare Number
Goodflag = truecompare Boolean
Goodname != "George"not equal
Goodperson.age > 40JSON dot notation
Goodk1 = "v1" AND k2 = "v2"AND operator
Goodk1 = "v1" OR k2 = "v2"OR operator
Goodk1 = "v1" AND k2 = "v2" ... AND kx= "vx"multiple AND/OR operator
BADk1 = "v1" AND k2 = "v2" OR k3 = "v3"mixed logical operators
Good! (name contains "eorg")! operator
Good(a = 1 AND b = 'foo') OR c = falsewith brackets
Good(a = 1 OR b = 'foo') AND c = falsewith brackets
Good(a = 1 OR b = 'foo') AND (c = false AND d CONTAINS 'bar')with brackets
BAD((a = 1 AND b = 'foo') OR c = false) AND d CONTAINS 'bar'nested brackets