Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/repalash/fexpr.js
Filter expression parser(like SQL WHERE, Elasticsearch etc) for JavaScript.
https://github.com/repalash/fexpr.js
dsl expression fexpr filter-parser javascript parser query sql typescript
Last synced: 1 day ago
JSON representation
Filter expression parser(like SQL WHERE, Elasticsearch etc) for JavaScript.
- Host: GitHub
- URL: https://github.com/repalash/fexpr.js
- Owner: repalash
- License: mit
- Created: 2024-04-21T19:03:24.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2024-04-23T12:48:50.000Z (7 months ago)
- Last Synced: 2024-10-30T08:51:44.677Z (19 days ago)
- Topics: dsl, expression, fexpr, filter-parser, javascript, parser, query, sql, typescript
- Language: TypeScript
- Homepage: http://repalash.com/fexpr.js/
- Size: 47.9 KB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
fexpr.js [![NPM Package](https://img.shields.io/npm/v/fexpr.js.svg)](https://www.npmjs.com/package/threepipe) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](./LICENSE)
===============================================================================**fexpr** is a filter query language parser that generates easy to work with AST structure so that you can safely create SQL, Elasticsearch, etc. queries from user input. **fexpr.js** is a typescript/javascript port of [fexpr](https://github.com/ganigeorgiev/fexpr).
Or in other words, transform the string `"id > 1"` into the struct `[{&& {{identifier id} > {number 1}}}]`.
Supports parenthesis and various conditional expression operators (see [Grammar](#grammar)).
This is a zero-dependency(<5KB) library that works in both node.js and browser environments.
Almost a line by line port of [fexpr](https://github.com/ganigeorgiev/fexpr) (BSD 3-Clause License) by [Gani Georgiev](https://github.com/ganigeorgiev), from golang to typescript with minor changes.
## Demo
Try the [fexpr.js playground](https://repalash.com/fexpr.js) to see how the parser works. Check the [index.html](https://github.com/repalash/fexpr.js/blob/master/index.html) file for the example source code.
Read the [package documentation](https://repalash.com/fexpr.js/docs) or the source code for API documentation.
## Usage
Installation
```
npm install fexpr.js
```
> or use a cdn link in browserESM Usage in javascript or typescript:
```javascript
import { parse } from 'fexpr.js';const result = parse('id=123 && status="active"');
// result: [{&& {{identifier id} = {number 123}}} {&& {{identifier status} = {text active}}}]
```CommonJS Usage in node.js:
```javascript
const { parse } = require('fexpr.js');const result = parse('id=123 && status="active"');
```UMD Usage in browser:
```htmlconst result = fexpr.parse('id=123 && status="active"');
```
> Note that each parsed expression statement contains a join/union operator (`&&` or `||`) so that the result can be consumed on small chunks without having to rely on the group/nesting context.
> See the [package documentation](https://repalash.com/fexpr.js/docs) for more details and examples.
## Grammar
**fexpr** grammar resembles the SQL `WHERE` expression syntax. It recognizes several token types (identifiers, numbers, quoted text, expression operators, whitespaces, etc.).
> You could find all supported tokens in [`scanner.ts`](https://github.com/repalash/fexpr.js/blob/master/src/scanner.ts).
#### Operators
- **`=`** Equal operator (eg. `a=b`)
- **`!=`** NOT Equal operator (eg. `a!=b`)
- **`>`** Greater than operator (eg. `a>b`)
- **`>=`** Greater than or equal operator (eg. `a>=b`)
- **`<`** Less than or equal operator (eg. `a`** Array/Any Greater than operator (eg. `a?>b`)
- **`?>=`** Array/Any Greater than or equal operator (eg. `a?>=b`)
- **`?<`** Array/Any Less than or equal operator (eg. `a? 123');// scan single token at a time until EOF or error is reached
while (true) {
const t = s.scan();
if (t.type === 'EOF' || t.error) {
break;
}console.log(t);
}// Output:
// {type: 'identifier', value: 'id'}
// {type: 'whitespace', value: ' '}
// {type: 'sign', value: '>'}
// {type: 'whitespace', value: ' '}
// {type: 'number', value: '123'}
```