Skip to content
Composable rule matching
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.editorconfig
.eslintrc.json
.gitignore
.npmignore
.travis.yml
Gruntfile.js
LICENSE
README.md
package.json

README.md

Build Status Join the chat at https://gitter.im/MiguelCastillo/roolio

roolio

Composable rule matcher

Create rules to check if data matches a particular criteria. Rules are built with one or more matchers so that you can compose sequences of matchers to test data against. There are a few built in matchers to get you started with, but you can create your own as well.

API

addMatchers

Function that takes a single or array of matching rules that we refer to as matchers. matchers added with this method are aggregated.

var rule = new Rule();

// Register a matcher to verify if the input is a string or matches the pattern in the regexp
rule
  .addMatchers(Rule.matcher.string)
  .addMatchers(/hello world/);

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 = new Rule();

// Match true
rule
  .addMatchers(Rule.matcher.string)
  .addMatchers(/hello world/);
  .match('test');

matchAll

Just like match, but it checks if the input matches all the matching rules.

var rule = new Rule();

// Match is false
rule
  .addMatchers(Rule.matcher.string)
  .addMatchers(/hello world/);
  .matchAll('test');

// Match is true
rule
  .matchAll('hello world');

getLength

Function that returns the number of matchers in a particular rule.

var rule = new Rule();

// Length returns 2
rule
  .addMatchers(Rule.matcher.string)
  .addMatchers(/hello world/)
  .getLength();

You can also access rule.matchers.length

matchers

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.

This is what matchers looks like:

function strict_equality_matcher(match) {
  return function(criteria) {
    return match === criteria;
  }
}

function all_caps_matcher() {
  return function(criteria) {
    return /[A-Z]/.test(criteria);
  }
}

roolio comes with a few default matchers that are detailed below.

default

The default matcher will do a regexp test if a regexp is provided, otherwise it will do a strict equality comparison.

var rule = new Rule();

rule.addMatchers(['hello', /hola/]);

rule.match('hello');        // Will test true
rule.match('hola world');   // Will test true
rule.match('hello world');  // Will test false
rule.match('hi world');     // 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 = new Rule();

rule.addMatchers(Rule.matcher.extension('js|jsx|json'));

rule.match('test.js');   // Will test true
rule.match('test.jsx');  // Will test true
rule.match('test.json'); // Will test true
rule.match('testjs');    // Will test false
rule.match('test.js.a'); // Will test false

string

string matcher, which verifies if the input string matches one of the matching rules.

var rule = new Rule();

rule.addMatchers(Rule.matcher.string('test.js'));

rule.match('test.js');   // Will test true
rule.match('test.jsx');  // Will test false
rule.match('test.json'); // Will test false
rule.match('testjs');    // Will test false
rule.match('test.js.a'); // Will test false

You can actually just check if the input is a string.

var rule = new Rule();

rule.addMatchers(Rule.matcher.string);

rule.match('some randome string');   // Will test true
rule.match('');                      // Will test true
rule.match(true);                    // Will test false
rule.match(null);                    // Will test false

custom 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:

function customMatcher(criteria) {
  return function(match) {
    return criteria[0] === match[0];
  };
}

var rule = new Rule();

rule.addMatchers([customMatcher('Standing up'), customMatcher('Dont stand up')]);

rule.match('Some randome string');   // Will test true
rule.match('some randome string');   // Will test false
rule.match('Dance party');           // Will test true
rule.match('dance party');           // Will test false

License

Licensed under MIT

You can’t perform that action at this time.