1.0.4 • Published 5 years ago

@smartorigin/smart-filter v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

@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

1.0.4

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago