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

roolio

v0.1.1

Published

Composable rule matcher

Downloads

58

Readme

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.

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

rule.addMatcher(['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.addMatcher(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.addMatcher(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.addMatcher(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 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:

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

var rule = new Rule();

rule.addMatcher([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

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

rule
  .addMatcher(Rule.matcher.string)
  .addMatcher(/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 is true
rule
  .addMatcher(Rule.matcher.string)
  .addMatcher(/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
  .addMatcher(Rule.matcher.string)
  .addMatcher(/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
  .addMatcher(Rule.matcher.string)
  .addMatcher(/hello world/)
  .getLength();

License

Licensed under MIT