npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expression-match

v0.0.17

Published

expression comparison

Downloads

3,708

Readme

Match Objects with Expressions

Use expression patterns to compare object values

Table of Contents

Installation

npm install expression-match

Expressions

  • add
  • any
  • eq
  • gt
  • gte
  • in
  • lt
  • lte
  • ne
  • not
  • or
  • regex

Configuration

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);

Add Patterns

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' } );

Match

Run all expression searches in the queue

Match.match()

Select Expression 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();

Custom Selector and Comparer

in progress

###Usage

Keystone dependsOn

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();

Examples

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
   

Testing

Test file located in test dir

npm i -g mocha

npm test

License

MIT License