A module that allows smart filtering over js array using SQL-Like where clause.
npm install @smartorigin/smart-filter
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.
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
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.
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 passingnpm run prepublish
is passing
Created by MarcAlx for Smart/Origin
https://github.com/flexdinesh/npm-module-boilerplate
MIT License