# steplix-query-filters

> Steplix Query Filter is a module for parsing filters in string to object.

Latest version **0.0.13** (published 2020-09-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install steplix-query-filters
pnpm add steplix-query-filters
yarn add steplix-query-filters
bun add steplix-query-filters
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.13 |
| Published | 2020-09-10 |
| First published | 2020-02-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Steplix Developers |
| Maintainers | dev-steplix |
| Keywords | query, filter, params, parameters, steplix |

## Links

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

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.0.13 (latest) — 2020-09-10
- 0.0.12 — 2020-07-28
- 0.0.11 — 2020-04-15
- 0.0.10 — 2020-02-27
- 0.0.9 — 2020-02-19
- 0.0.8 — 2020-02-19
- 0.0.7 — 2020-02-19
- 0.0.6 — 2020-02-18
- 0.0.5 — 2020-02-14
- 0.0.3 — 2020-02-14
- 0.0.2 — 2020-02-13
- 0.0.1 — 2020-02-13

## README

# Steplix Query Filter

Steplix Query Filters is a module for parse filters from string to object.

## Index

* [Download & Install][install].
* [How is it used?][how_is_it_used].
* [Tests][tests].

## Download & Install

### NPM
```bash
$ npm install steplix-query-filters
```

### Source code
```bash
$ git clone https://github.com/steplix/SteplixQueryFilters.git
$ cd SteplixQueryFilters
$ npm install
```

## How is it used?

### Simple usage
```js
const { Parser } = require('steplix-query-filters');
const parser = new Parser();

parser.parse('active eq 1,description li %casa');
// { "active": { "eq": "1" }, "description": { "li": "%casa" } }

parser.parse('active eq 1,description li %casa,description eq depto');
// { "active": { "eq": "1" }, "description": { "li": "%casa", "eq": "depto" } }
```

### Operators
| Name | Example         | Description                                                   |
|:-----|:----------------|:--------------------------------------------------------------|
| eq   | id eq 1         | Check equality. id = 1                                        |
| ne   | name ne nico    | Check inequality. name != 'nico'                              |
| gt   | id gt 1         | Check greater than. id > 1                                    |
| ge   | id ge 10        | Check greater than or equal. id >= 10                         |
| lt   | id lt 1         | Check less than. id < 1                                       |
| le   | id le 10        | Check less than or equal. id <= 10                            |
| li   | name li nico%   | Check matches with nico*. name like nico%                     |
| nl   | name nl nico%   | Check not matches with nico*. name not like nico%             |
| in   | id in [1;2;3]   | Check if included on [1,2,3]. id in (1,2,3)                   |
| ni   | id ni [1;2;3]   | Check if not included on [1,2,3]. id not in (1,2,3)           |
| be   | id be [1;10]    | Check if it is between a and b. id between (1 and 10)         |
| nb   | id nb [1;10]    | Check if it is not between a and b. id not between (1 and 10) |

### Configurations
| Name            | Type               | Default                                                       | Description                                                        |
|:----------------|:-------------------|:--------------------------------------------------------------|:-------------------------------------------------------------------|
| separator       | string             | ","                                                           | Filter separator.                                                  |
| key             | string             | "[A-Za-z0-9_]+"                                               | String with RegExp format for match key on filters.                |
| value           | string             | ".+"                                                          | String with RegExp format for match value on filters.              |
| operators       | array              | ['eq','ne','gt','ge','lt','le','li','nl','in','ni','be','nb'] | Operators known to the parser.                                     |
| operatorPrefix  | string             | " "                                                           | Operator prefix in the string filter.                              |
| operatorSuffix  | string             | " "                                                           | Operator suffix in the string filter.                              |
| operatorFlags   | string             | "i"                                                           | Operator regexp flag.                                              |
| mapOperator     | object or function | null                                                          | Mapper used to replace operators.                                  |
| mapValue        | function           | null                                                          | Mapper used to replace values.                                     |
| mapValueFormat  | function           | null                                                          | Mapper used to replace values <i>only on <b>format</b> method.</i> |
| mapValueParse   | function           | null                                                          | Mapper used to replace values <i>only on <b>parse</b> method.</i>  |
| mapKey          | object or function | null                                                          | Mapper used to replace keys.                                     |
| mapKeyFormat    | object or function | null                                                          | Mapper used to replace keys <i>only on <b>format</b> method.</i> |
| mapKeyParse     | object or function | null                                                          | Mapper used to replace keys <i>only on <b>parse</b> method.</i>  |
| separatorGroups | string             | ";"                                                           | Filter group separator. Example "id in [1;2;3]"                    |

#### Configuration examples
```js
const parser = new Parser({
  separator: '---'
});

parser.parse('active eq 1---description li %casa');
// { "active": { "eq": "1" }, "description": { "li": "%casa" } }
```

```js
const parser = new Parser({
  operators: Parser.defaults.operators.concat(['my-operator'])
});

parser.parse('active eq 1,description my-operator casa');
// { "active": { "eq": "1" }, "description": { "my-operator": "casa" } }
```

#### Configuration mapper

Inspired to use in combination with **steplix-database**

```js
const { Parser, Mappers } = require('steplix-query-filters');
const parser = new Parser({
  mapper: Mappers.SQL
});

parser.parse('active eq 1,description li %casa');
// { "active": { "=": "1" }, "description": { "LIKE": "%casa" } }
```

Complete example with **steplix-database**
```js
const { Database, Query } = require('steplix-database');
const { Parser, Mappers } = require('steplix-query-filters');
const db = new Database({
  host: 'localhost',
  user: 'myuser',
  password: 'mypass',
  database: 'mydbname'
});
const parser = new Parser({
  mapper: Mappers.SQL
});

const where = parser.parse('active eq 1,description li %nicolas');
const query = Query.select('users', { where });
// "SELECT * FROM users WHERE active = '1' AND description LIKE '%nicolas'"

db.query(query);
// array<user models>
```

### Format
```js
const parser = new Parser();

parser.format({
  active: {
    eq: "1"
  },
  description: {
    li: "%casa"
  }
});
// "active eq 1,description li %casa"
```

## Tests

In order to see more concrete examples, **I INVITE YOU TO LOOK AT THE TESTS :)**

### Run the unit tests
```bash
npm install
npm test
```

<!-- deep links -->
[install]: #download--install
[how_is_it_used]: #how-is-it-used
[tests]: #tests

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