https://github.com/patarapolw/q2filter
Advanced query string parser for MongoDB and `Array.prototype.filter`
https://github.com/patarapolw/q2filter
javascript-filter mongodb
Last synced: about 2 months ago
JSON representation
Advanced query string parser for MongoDB and `Array.prototype.filter`
- Host: GitHub
- URL: https://github.com/patarapolw/q2filter
- Owner: patarapolw
- License: mit
- Created: 2019-08-22T15:05:31.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T08:57:40.000Z (over 2 years ago)
- Last Synced: 2025-02-14T07:29:05.140Z (3 months ago)
- Topics: javascript-filter, mongodb
- Language: TypeScript
- Size: 117 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# q2filter
Advanced query string parser for MongoDB and `Array.prototype.filter`
## Usage
```typescript
import QParser from "q2filter";
const p = new QParser(parserOptions);
const cond = p.getCond(q); // cond for MongoDB find and aggregate
const filteredArray = p.filter(allItems, q); // Filtering list of items
```Example parserOptions. All options can be omitted.
```typescript
{
dialect: "filter", // or 'mongo'. Mongo is the default.
anyOf: ["a", "b", "s", "tag"],
isString: ["s", "tag"],
isDate: ["date"],
noParse: ["is:special"],
transforms: {
"is:due": () => {
return {nextReview: {$lt: new Date()}}
}
},
filters: {
"is:distinct": (items: any[]) => {
const col: Record = {};
for (const it of items) {
const k = stringify(it);
if (!col[k]) {
col[k] = it;
}
}
return Object.values(col);
},
"is:duplicate": (items: any[]) => {
const col: Record = {};
for (const it of items) {
const k = stringify(it);
col[k] = col[k] || [];
col[k].push(it);
}
return Object.values(col).filter((a) => a.length > 1).reduce((a, b) => [...a, ...b], []);
}
},
sortBy: "date",
desc: true
}
```Acceptable `q`'s and their output can be seen at [/tests/examples.yaml](/tests/examples.yaml)
## Installation
```
npm i q2filter
```