Giter Site home page Giter Site logo

qp's Introduction

qp

Build Status Filter arrays using SQL like querystring

Installation

$ npm install @rusintez/qp --save

Usage

var filter = require('@rusintez/qp');
var assert = require('assert');

var arr = [{
  bool_field: true,
  another_bool_field: false,
  a: 1,
  b: 2,
  c: 'blah',
  d: [1, "2", true, false]
}];

var query = 'bool_field AND NOT another_bool_field AND a = 1 AND (b >= 2 OR c = "blah") AND d INCLUDES [1, "2", true]';
var result = filter(query, arr);

assert.deepEqual(arr, result);

API

filter({String} query, {Array} list[, {String} adapter]):Array

var result = filter(query, list);

Adapters

By default adapter is set to 'native' that uses tree-like structure to filter objects in an array. It's possible to use (included) sift.js filtering mechanism

var siftFilteredList = filter(query, list, 'sift');

Query Syntax

AND

a AND b — true when both are true

OR

a OR b - true when either one is true

NOT

NOT c — true if c is falsy

EQUALS

a EQUALS 5 — true when a is number 5 a = 5 — shorthand, same as above

GT

a > 4 — true when a is greater than 4

GTE

a >= 4 — true when a is greater than or equals 4

LT

a < 4 — true when a is less than 4

LTE

a <= 4 — true when a is less than or equals 4

INCLUDES

tags INCLUDES ["russia", "china"] — true when tags include both russia and china tags IN ["us", "uk"] — shorthand, same as above

Flags

Flags are shorthand expressions by them selves, eg.

has_active_license AND no_principal will become has_active_license = true AND no_principal = true

Brackets

Brackets are supported to ensure correct order or comparison

a = 5 OR (b = 10 AND c > 0) AND NOT tags IN ["blacklisted"]

Supported datatypes

String

"hello world" — strings must be surrounded by double quotes

Number

5 - for now only non-negative integers are supported, more coming soon

Boolean

true, false

Null

null

Array

[1,"2", false, [true, true], null] — all of the above + nesting

Advanced

It's possible to get back parsed query tree using excellent PEG generated parser.

var parser = require('@rusintez/qp/parser');
var assert = require('assert');

var tree = parser.parse('license_types INCLUDES 7');

assert.deepEqual(tree, {
  token: 'INCLUDES',
  key: 'license_types',
  value: {
    token: 'NUMBER',
    value: 7
  }
});

Author

Vladimir Popov

License

MIT

qp's People

Contributors

rusintez avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

toshsan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.