myData Filter
Small filter library for working with immutable AST(abstract syntax trees) and queries
Installation
npm install mydata-filter
Usage
Compile a JSON-AST code that can be used later by a transformer:
import { Parse } from "mydata-filter";
const code = `(user.username == "Ana") or (username == "Mari")`;
Parse(code).then(ast => {
console.log(ast); // { type: "...", ... }
}).catch(error => {
console.error(error);
});
Compile a mysql where clause from a string using a transformer:
import { Parse } from "mydata-filter";
import MySqlTransformer from "mydata-filter-mysql";
const code = `(user.username == "Ana") or (username == "Ana")`;
Parse(code, {
transformer: new MySqlTransformer({
clause: "WHERE"
})
}).then(query => {
console.log(query); // (`user`.`username` == 'Ana') OR (`username` == 'Ana')
}).catch(error => {
console.error(error);
});
Supported Functions
Parse(code, {
cache?: boolean,
transformer?: ITransformer
}): Promise<any>;
ParseSync(code, {
cache?: boolean,
transformer?: ITransformer
}): any;
Available Common Transformers
Functions Syntax
FunctionName(arg1)
FUNCTION_NAME(arg1, arg2, ...)
Supported Operators
Name | Operator | Alias |
---|---|---|
Equals | == | |
Not Equals | != | |
Less than or equal | <= | |
Greater Than or Equal | >= | |
Less Than | < | |
Greater Than | > | |
Logical And | && | AND |
Logical Or | || | OR |
Contains | *= | |
Contains Word | ~= | |
Starts With | ^= | |
Ends With | =$ | |
Arithmetic Add | + | |
Arithmetic Subtraction | - | |
Arithmetic Multiplication | * | |
Arithmetic Division | / | |
Sorter ascending | ASC | |
Sorter descending | DESC |
Supported Features Identifiers Name
Name | Description |
---|---|
OR | Logical Or |
AND | Logical And |
LT | Less Than |
GT | Greater Than |
LE | Less than or equal |
GE | Greater Than or Equal |
NEQ | Not Equals |
EQ | Equals |
CONTAINS | Contains Operator |
CONTAINS_WORD | Contains Word Operator |
STARTS_WITH | Starts With Operator |
ENDS_WITH | Ends With Operator |
ADDITION | Arithmetic Addiction |
SUBTRACTION | Arithmetic Subtraction |
MULTIPLICATION | Arithmetic Multiplication |
DIVISION | Arithmetic Division |
PARENTHESES | Parentheses |
IDENTIFIER_PATH | Deep Identifier separated by dot |
IDENTIFIER | Simple identifier |
BOOLEAN | Boolean Type |
NUMBER | Number Type |
STRING | String Type |
FUNCTION_CALL | Call Function |
ASC | Sort ascending |
DESC | Sort descending |
SORTING_LIST | Multiple sorters separated by comma |