text-filter-parser

🌪️ Parse query filter rules given in plain text

View the Project on GitHub AnandChowdhary/text-filter-parser

🌪️ Text Filter Parser

NPM Build Coveralls Type definitions GitHub Vulnerabilities

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:

[
  {
    "key": "hello",
    "condition": "is",
    "value": "world"
  }
]

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 = require("text-filter-parser");

Create an object and use a method to get your desired output.

const result = new Parser("id > 2, name ew nand")
const json = result.toJSON();
console.log(json);
/*
[
  {
    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 = new Parser("id > 2, name ew nand")
const sql = result.toSQL();
console.log("SELECT * FROM users WHERE " + sql);
/*
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