npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

steplix-query-filters

v0.0.13

Published

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

Downloads

242

Readme

Steplix Query Filter

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

Index

Download & Install

NPM

$ npm install steplix-query-filters

Source code

$ git clone https://github.com/steplix/SteplixQueryFilters.git
$ cd SteplixQueryFilters
$ npm install

How is it used?

Simple usage

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 only on format method. | | mapValueParse | function | null | Mapper used to replace values only on parse method. | | mapKey | object or function | null | Mapper used to replace keys. | | mapKeyFormat | object or function | null | Mapper used to replace keys only on format method. | | mapKeyParse | object or function | null | Mapper used to replace keys only on parse method. | | separatorGroups | string | ";" | Filter group separator. Example "id in [1;2;3]" |

Configuration examples

const parser = new Parser({
  separator: '---'
});

parser.parse('active eq 1---description li %casa');
// { "active": { "eq": "1" }, "description": { "li": "%casa" } }
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

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

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

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

npm install
npm test