roolio
Composable rule matcher
Create rules to check if data matches a particular criteria. Rules are built with one or more matcher
s so that you can compose sequences of matcher
s to test data against. There are a few built in matcher
s to get you started with, but you can create your own as well.
matchers
Are functions that check if input data matches a criteria. For example, you can check if input data is a string, or if the string is all CAPS.
There are a few of these default matchers.
default
The default matcher will do a regex test if a regex is provided, otherwise it will do a strict equality comparison.
var rule = ;rule;rule; // Will test truerule; // Will test truerule; // Will test falserule; // Will test false
extension
File extension matcher, which verifies if an input string has particular file extensions. You can specify one or more file extensions in a single matcher.
var rule = ;rule;rule; // Will test truerule; // Will test truerule; // Will test truerule; // Will test falserule; // Will test false
string
string matcher, which verifies if the input string matches one of the matching rules.
var rule = ;rule;rule; // Will test truerule; // Will test falserule; // Will test falserule; // Will test falserule; // Will test false
You can actually just check if the input is a string.
var rule = ;rule;rule; // Will test truerule; // Will test truerule; // Will test falserule; // Will test false
custom rule matchers
Rule matchers are just functions. So you can pass in your own functions if you want custom behavior. For example, you can create your own custom matcher to check objects properties.
Here is an arbitrary custom matcher that checks if the first letter of the input string is the same as the first letter of one of the matching rules:
{return {return criteria0 === match0;};}var rule = ;rule;rule; // Will test truerule; // Will test falserule; // Will test truerule; // Will test false
Rules API
addMatcher
Function that takes a single matching rule, or an array of them. Matching rules added with this method are aggregated, and returns itself to enable chaining.
var rule = ;rule;
match || matchAny
Function that takes in data to be matched against the configured matching rules. Returns whether or not the input matches one of the matching rules.
var rule = ;// Match is truerule;;
matchAll
Just like match
, but it checks if the input matches all the matching rules.
var rule = ;// Match is falserule;;// Match is truerule;
getLength
Function that returns the number of matchers in a particular rule.
var rule = ;// Length returns 2rule;
License
Licensed under MIT