# filtering-query

> A simple query language (DSL) for filtering out objects with type assist support

Latest version **0.1.0-alpha.1** (published 2021-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install filtering-query
pnpm add filtering-query
yarn add filtering-query
bun add filtering-query
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0-alpha.1 |
| Published | 2021-05-10 |
| First published | 2021-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 159.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | marchmiel |
| Maintainers | marchmiel |

## Links

- npm: https://www.npmjs.com/package/filtering-query
- Repository: https://github.com/marchmiel/filtering-query
- Homepage: https://github.com/marchmiel/filtering-query#readme
- Issues: https://github.com/marchmiel/filtering-query/issues
- npm.io page: https://npm.io/package/filtering-query

## Dependencies (5)

- [g](https://npm.io/package/g.md) ^2.0.1
- [tslib](https://npm.io/package/tslib.md) ^2.2.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.14.0
- [antlr4ts-browser](https://npm.io/package/antlr4ts-browser.md) ^0.5.0-alpha.4

## Recent versions

- 0.1.0-alpha.1 (latest) — 2021-05-10

## README

Simple expression language for objects filtering
=======

A simple, safe, JavaScript expression engine, allowing end-users to enter arbitrary expressions without p0wning you.

Installation
------------------

```
npm i --save filtering-query
```

Quick start
------------------

```javascript
import {compile} from 'filtering-query'

// Input from a user (e.g. a search filter)
const expression = 'transactions <= 5 and customer.name = "My customer"';

// Compile expression to an executable function
const fn = compile(expression);

// Execute function
fn({transactions: 3, customer: { name: "My customer" }); // returns true
fn({transactions: 3, customer: { name: "Other customer" }); // returns false
```

Under the hood, the above expression gets compiled to a clean and fast JavaScript function, looking something like this:

```javascript
// Resulting function
function(item) {
  return item.transactions <= 5 && customer.name === "My customer";
}
```

Why?
----

There are many cases where you want a user to be able enter an arbitrary expression through a user interface
to filter/search across items using multiple fields.

Sure, you could do that with JavaScript and `eval()`, but this would increase chances of p0wning you.

This library takes a different approach. It defines a grammar for a really simple expression language, produces a lexer and a parser for it,
and uses it to compile down the expression into a fast JavaScript filtering function, which can be used runtime.

The expression laguage is very simple so that 
anyone who's ever used a spreadsheet
should be familiar with it. 

Moreover, the library provides a type assist functionality based on JSON schema,
so writting an expression is even simplier.

Syntax
-----------

Values | Description
--- | ---
43, -1.234 | Numbers
"hello" | String
foo, a.b.c | External data variable defined by application (may be numbers, strings or arrays)

Comparisons | Description
--- | ---
x = y | Equals
x != y | Does not equal
x < y | Less than
x <= y | Less than or equal to
x > y | Greater than
x >= y | Greater than or equal to
x ~= y | Regular expression match (case insensitive)

Boolean logic | Description
--- | ---
x or y | Boolean or
x and y | Boolean and
not x | Boolean not
( x ) | Explicity operator precedence

Operator precedence follows that of any sane language.

### Examples

```
product.name = "Some product name"
product.price.value >= 100.5 and product.price.value <= 300
product.name ~= "some"
product.name = "Some product" and (product.categories = "Appliances" or product.categories = "Electronics")
product.name = "Some product" and not product.availability
```

See [the grammar definition](./FilterQuery.g4) for more details.

---
_Source: https://npm.io/package/filtering-query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
