arc-check
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
var ArcCheck = require('arc-check');
let check = new ArcCheck();
check.addInclude(/^[A-Za-z]*$/);
check.addExclude(/^Private$/);
let collection = ['a','b','copy','duck9','Private',99,undefined];
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.
var ArcCheck = require('arc-check');
let check = new ArcCheck();
check.val(false);
check.addInclude(/\.jpg$/);
check.val('pic.jpg');
check.val('config.yml');
check.addInclude(/\.yml$/);
check.val('config.yml');
check.val('.travis.yml');
check.addExclude(/^\./);
check.val('.travis.yml');
check.addExclude(function(_val){
return (_val === SOME_USER_CHECK ? true : false);
});
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
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
var check = new ArcCheck();
check.addExclude('/^[a-z]*$/');
.val(value:Mixed)
Evaluate a value based on the inclusion/exclusion logic checks.
var check = new ArcCheck();
check.addInclude('/^[a-zA-Z0-9\.]*$/');
check.addExclude(function(_val){
return (_val === 'private.key' ? true : false);
});
check.val('private.key');
check.val('key.private');
.reset()
Set Check back to default state (remove all set logic)
Testing
$ npm test