🌪️ Text Filter Parser
A utility to parse query filter rules given in plain text and get a structured array.
⭐ How it works
If you have a database view in your app, you might want to give users the option to filter based on a text input. For example, hello=world
becomes:
You can then translate this into a query or use one of built-in functions like toSQL()
.
💻 Usage
Add fraud to your project with NPM:
npm install text-filter-parser
Add it to your project:
const Parser = ;
Create an object and use a method to get your desired output.
const result = "id > 2, name ew nand"const json = result;console;/*[ { key": "id", condition": "is greater than", value: 2 }, { key": "name", condition": "ends with", value: "nand" }]*/
You can also generate the WHERE
clause of an SQL query:
const result = "id > 2, name ew nand"const sql = result;console;/*SELECT * FROM users WHERE `id` > 2 AND `name` = "%nand"*/
Operators
Operator | Condition |
---|---|
= | "is" |
!= | "is not" |
> | "is greater than" |
< | "is less than" |
>= | "is greater than or equal to" |
<= | "is less than or equal to" |
sw | "starts with" |
ew | "ends with" |
* | "includes" |
📝 License
MIT