q2filter
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

q2filter

Advanced query string parser for MongoDB and Array.prototype.filter

Usage

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.

{
  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<string, any> = {};
      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<string, any[]> = {};
      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",
  desctrue
}

Acceptable q's and their output can be seen at /tests/examples.yaml

Installation

npm i q2filter

Dependencies (4)

Dev Dependencies (8)

Package Sidebar

Install

npm i q2filter

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

61.9 kB

Total Files

18

Last publish

Collaborators

  • patarapolw