Skip to content

A module that allows smart filtering over js array using SQL-Like where clause.

License

Notifications You must be signed in to change notification settings

MarcAlx/smart-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@smartorigin/smart-filter

npm version License: MIT

A module that allows smart filtering over js array using SQL-Like where clause.

Getting started

npm install @smartorigin/smart-filter

Usage

Capabilities of the mobule.

With smart-filter you can test if an object match an SLQ-Like where clause.

It could be used to expose to end user an high level language and to easily process end user expressions in JS.

You can filter/test object using the following syntax : (assuming propA and propB are properties of a js object)

propA = 1

propA = "2"

propA = true

propA = 'Hello'

propA = "Hello" OR propA = "Hi"

propA = "Hello" AND propA = "Hi"

!(propA = "Hello" AND propA = "Hi")

propA IN ["Hello","Hi"]

propA NOTIN ["Hello","Hi"]

propA <> propB

propA > 2

propA < 4

propA <= 4

propA >= 4

1=1 //means keep all

1!=1 //means keep none

You can also write operator like this :

= -> ==
AND -> && 
OR -> ||
<> -> !=

This module is build using JSEP.

ES6

see also /test/index.js

First import exposed functions :

import {filter,test} from '@smartorigin/smart-filter'

function test(obj,expr)

let input = {
    "propA":2,
    "propB":"OK",
    "propC":true,
    "propD":4,
    "propE":false,
    "propF":"KO"
}

//simple equality
test(input,'propA == propA') //return true

//simple comparison
test(input,'propA > 1') //returns true

//boolean expr
test(input,'propB == "OK" && propA == 2') //return true

function filter(arr,expr)

let input = [
    {
        "name":"obj1",
        "propA":2,
        "propB":"OK",
        "propC":true
    },
    {
        "name":"obj2",
        "propA":34,
        "propB":"KO",
        "propC":false
    }
];


filter(input,'name="obj1" OR name="obj2"') // Returns two results

filter(input,'name="test"')// Returns empty results

Non ES6

For non ES6 env just require module

var smartfilter = require("@smartorigin/smart-filter");

let input = {"a":"ok"};

smartfilter.test(input,'a="ok"'); // returns true

let arr = [{"a":"1"},{"a":"2"}];

smartfilter.filter(a,'a="1"'); // return an array that contains one object.

Contributing

Before pushing any pull request you must ensure the following points :

  • you have new test to your feature / fix and npm run test pass.
  • npm run build is passing
  • npm run prepublish is passing

About

Created by MarcAlx for Smart/Origin

Usefull links

https://github.com/flexdinesh/npm-module-boilerplate

License

MIT License

About

A module that allows smart filtering over js array using SQL-Like where clause.

Resources

License

Stars

Watchers

Forks

Packages

No packages published