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

arc-check

v2.0.0

Published

An ES6 class to evaluate values against multiple complex inclusion/exclusion rules

Downloads

5,294

Readme

arc-check Build Status

An ES6 class to evaluate values against multiple complex inclusion/exclusion rules.

Install

$ npm install arc-check --save

Features

  • Regular expression inclusion matching
  • Regular expression exclusion matching
  • Callback inclusion matching
  • Callback exclusion matching
  • Handles any combination of inclusion/exclusion
  • Simple API

Basic Usage

The following creates a new Check object, then evaluates variables against it. The example collection should evaluate as follows:

  • 'a' is true
  • 'b' is true
  • 'copy' is true
  • 'duck9' is false
  • 'Private' is false
  • 99 is false
  • undefined is false
//Example
var ArcCheck = require('arc-check');
let check = new ArcCheck();

//Add some logic
check.addInclude(/^[A-Za-z]*$/);
check.addExclude(/^Private$/);

//A sample array
let collection = ['a','b','copy','duck9','Private',99,undefined];

//Evaluate
for(let i=0;i<collection.length;i++){
    check.val(collection[i]);
}

Behavior & Advanced Usage

ArcCheck handles multiple evaluation states in order to either return true or false on a value. To be effectively used, it is important to understand its expected behavior and how evaluations are made.

The concept is simple and can best be described as follows: To evaluate as true a value MUST match ANY inclusion check AND MUST NOT match ANY exclusion check.

Inclusions and exclusions are always evaluated by truthiness and can be any number/combination of RegularExpression or callbacks.

By default, all states begin as passed. So in this way, if no inclusions checks are added, all values will be assumed to be wanted, and only fail if an exclusion is matched. Vice versa, if no exclusions are set, a value will only pass if an inclusion is matched. If neither inclusion, nor exclusion is set, all values will pass.

//Example
var ArcCheck = require('arc-check');
let check = new ArcCheck();

//Will be true (nothing has been set, so by default it matches all of the inclusions and has not matched any exclusions)
check.val(false);

//We add an inclusion regular expression to check for .jpg
check.addInclude(/\.jpg$/);

//Will evaluate to true (as it matches the above inclusion)
check.val('pic.jpg');

//But this will evaluate to false as it doesn't match any inclusion.
check.val('config.yml');

//If we wanted to add yml we can easily stack the inclusions by adding another
check.addInclude(/\.yml$/);

//Now this will evaluate to true
check.val('config.yml');

//But so will this
check.val('.travis.yml');

//Perhaps we don't want 'hidden' files, so we add an exclude
check.addExclude(/^\./);

//Now this will evaluate to false
check.val('.travis.yml');

//But there are times when perhaps the filter is set dynamically, say based on a user's config, so we can use a callback
check.addExclude(function(_val){
    //Callbacks are expected to return true/false, and both inclusion and exclusion callbacks are expected to return true on a match, and false on a non match
    return (_val === SOME_USER_CHECK ? true : false);
});

//Finally, if needed the Check can be reset to a default state for reuse.
check.reset();

API

new ArcCheck()

Create a new ArcCheck object. Requires new

.addInclude(inclusion:RegExp or Function)

Add an inclusion check. Valid types are a RegExp object or a callback Function that returns true/false

//Example of addInclude
var check = new ArcCheck();
check.addInclude('/^[a-z]*$/');

.addExclude(exclusion:RegExp or Function)

Add an exclusion check. Valid types are a RegExp object or a callback Function that returns true/false

//Example of addExclude
var check = new ArcCheck();
check.addExclude('/^[a-z]*$/');

.val(value:Mixed)

Evaluate a value based on the inclusion/exclusion logic checks.

//Example of val
var check = new ArcCheck();

//Set logic
check.addInclude('/^[a-zA-Z0-9\.]*$/');
check.addExclude(function(_val){
    return (_val === 'private.key' ? true : false);
});

//Evaluate
check.val('private.key'); //False
check.val('key.private'); //True

.reset()

Set Check back to default state (remove all set logic)

Testing

$ npm test