Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
expression-match
Advanced tools
Readme
Table of Contents
npm install expression-match
var ExMatch = require('expression-match');
var searchFields = {
str1: 'string',
str2: 'hello',
str3: 'plug',
str4: ['1','2'],
num1: 1,
num2: '2',
num3: '3',
check1:'true',
check2: false,
check3: undefined,
check4: 0,
check5: true,
check6: 'false'
}
var patterns = {
// true
e1: { $or:[ { str1:['first','third'] } , {check1: 'true'} ] },
e2: { $gt: { num3:1 }, $and:[{num1:'1'}, {str3: { or: ['cork','clog','plug'] } }, {num2:{$lt:4}}] },
e3: { str2: ['hi', 'hello'], str3: { $regex: 'plu.*/i' } },
e4: { num3: { $lte: 3 } },
e5: { str1: [{ or: 'strings' }, { or: 'string'}] },
}
/* debug can be any truthy or 2 for compare only
* @param {object} matchPatterns
* @param {object} matchAgainst
* @param {object/boolean} options - debug only when boolean
* */
// `options.expression` changes the default expression which is `$and`.
var Match = new ExMatch( patterns.e2, searchFields, { debug: true, expression: '$and' });
// debig messages only.
var Match = new ExMatch( patterns.e3, searchFields, true);
Each method returns this
for chainability
Match.
addSearchParams( patterns.e3 ).
and( patterns.e1 ).
any( patterns.e3 ).
eq( { num1: 2, str2: 'hello' } )
gt( [ { num1: 2 } ] ).
gte( [ { num1: 2 }, { num2: 5 } ] ).
in( { num1: ['2', '1'] } )
lt( { num1: 2 } ).
lte( { num1: 2 } ).
ne( { num1: 2 } ).
not( { str1: 'test' } ).
regex( { str1: 'fir.*/i' } );
Run all expression searches in the queue
Match.match()
Run a single Expression search from the queue
Match.regex({str2: { $regex: 'hel.*/i'}});
var ret = Match.$regex();
/* All return boolean true/false */
Match.$and();
Match.$any();
Match.$eq();
Match.$gt();
Match.$gte();
Match.$in();
Match.$lt();
Match.$lte();
Match.$ne();
Match.$not();
Match.$or();
Match.$regex();
in progress
###Usage
var Tester = new keystone.List('Tester');
var patterns = {
earth: { $and:[ {str1:'earth'} , {check2:true} ] },
and: { $lte:[ {num1:1} , {num3:4} ] },
ice: { $gt:[{num3:1}],$and:[{num1:'1'},{sel1:['first','third']},{num2:{$lt:4}}] },
water: { $or:[ {num1:{$gte:6}} , {num2:{$lte:4}} ] },
fire: { sel1:'second',str3: {$regex: 'fir.*/i'} }
}
Tester.add({
}, 'Depends On', {
isIce: { type: Boolean, label: 'Ice', dependsOn: patterns.ice},
isFire: { type: Boolean, label: 'Fire' ,dependsOn: patterns.fire},
isEarth: { type: Boolean, label: 'Earth', dependsOn: patterns.earth},
isWater: { type: Boolean, label: 'Water', dependsOn: patterns.water},
isAnd: { type: Boolean, label: 'And', dependsOn: patterns.and }
}, 'Show Dependants When', {
sel1: { type: Types.Select, many: true, emptyOption: true },
check1: { type: Boolean, label: 'Check 1' , note:'earth ' },
check2: { type: Boolean, label: 'Check 2' },
num1: {type: Types.Number, note:'show `Water` when >= `6` -- -- `<=1` == `And` with num3' },
num2: {type: Types.Number, note:'show `Water` when <= `4`'},
num3: {type: Types.Number, note: '`<=4` show `And` with num1' },
str1: {type:String, note:'type earth with check2 to show `Earth`'},
str2: String,
str3: {type:String, note:'regex `fir*` with `sel1`==`second` to show `Fire`'},
});
Tester.register();
var ExMatch = require('exmatch');
// Create a searchfield:value object and some test search objects
searchFields = {
str1: 'string', str2: 'hello', str3: 'plug',
num1: 1, num2: '2', num3: '3',
check1:'true', check2: false
}
var Match = new ExMatch({
$or:[ {str1:['string1']}, {check1:'true'} ],
num2:'2',
num1:{ $lt:2 }
},searchFields);
var ret = Match.match();
==> true
var m7 = new ExMatch({}, searchFields, 2); //dubug on for compare only
m7.
and({check2:false}).
any({check1:false}).
any({ str2: { $regex: 'hel.*/i'} });
var ret = m7.match();
==> true
var Match = new ExMatch({ num2:{$eq:2} , $eq:{num3:'3'}, $eq:[{num1:'1'}] }, searchFields);
var ret = Match.match();
==> false
var Match = new ExMatch({ num2:{$eq:2} ,$eq:{num2:2}, $eq:[{num2:1}] } , searchFields);
var ret = Match.match();
==> false
var regex = new ExMatch({}, searchFields, true); //dubug on
regex.regex({str2: { $regex: 'hel.*/i'}});
var ret = regex.$regex();
==> true
Test file located in test
dir
npm i -g mocha
npm test
MIT License
FAQs
expression comparison
The npm package expression-match receives a total of 910 weekly downloads. As such, expression-match popularity was classified as not popular.
We found that expression-match demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.