Comparing version 1.0.0 to 1.0.1
83
index.js
@@ -9,2 +9,6 @@ "use strict"; | ||
constructor(){ | ||
this.reset(); | ||
} | ||
reset(){ | ||
this.includes = new ArcObject; | ||
@@ -19,3 +23,3 @@ this.excludes = new ArcObject; | ||
switch(is(_includeCheck)){ | ||
case 'regExp': | ||
case 'regexp': | ||
this.includes[_includeCheck.toString()] = _includeCheck; | ||
@@ -28,5 +32,3 @@ break; | ||
default: | ||
throw new TypeError('Check.addInclude expects valid RegExp or Function'); | ||
break; | ||
default: throw new TypeError('Check.addInclude expects valid regexp or function: received '+is(_includeCheck)); | ||
} | ||
@@ -39,3 +41,3 @@ return this; | ||
switch(is(_excludeCheck)){ | ||
case 'regExp': | ||
case 'regexp': | ||
this.excludes[_excludeCheck.toString()] = _excludeCheck; | ||
@@ -48,5 +50,3 @@ break; | ||
default: | ||
throw new TypeError('Filter.addInclude expects valid RegExp object'); | ||
break; | ||
default: throw new TypeError('Check.addExclude expects valid regexp object or function: received '+is(_excludeCheck)); | ||
} | ||
@@ -63,28 +63,24 @@ return this; | ||
//If we have inclusion regX we set it to false and check it first | ||
if(this.includes.count()){ | ||
includeCheck = false; | ||
this.includes.each(function(_key,_RX,_break){ | ||
if(_RX.exec(_string)[0] !== ''){ | ||
includeCheck = true; | ||
_break(); | ||
} | ||
}); | ||
if(is(_string) === 'string'){ | ||
this.includes.each(function(_key,_RX){ | ||
let rxResult = _RX.exec(_string); | ||
if(is(rxResult) === 'array' && rxResult[0] !== ''){ | ||
includeCheck = true; | ||
return false; | ||
} | ||
}); | ||
} | ||
} | ||
if(this.excludes.count()){ | ||
excludeCheck = false; | ||
this.excludes.each(function(_key,_RX,_break){ | ||
if(_RX.exec(_string)[0] !== ''){ | ||
excludeCheck = true; | ||
_break(); | ||
} | ||
}); | ||
} | ||
if(this.iCallbacks.length){ | ||
//We will be in 2 states at this point, we will have inclusionCallbacks and we will have not done RegX inclusion checks, or the RegX inclusion checks will have failed. If so, check | ||
if(this.iCallbacks.length && !this.includes.count() || this.iCallbacks.length && !includeCheck){ | ||
iCallbackCheck = false; | ||
this.iCallbacks.each(function(_callback,_key,_break){ | ||
this.iCallbacks.each(function(_callback){ | ||
if(_callback(_string)){ | ||
iCallbackCheck = true; | ||
_break(); | ||
includeCheck = true; | ||
return false; | ||
} | ||
@@ -94,11 +90,30 @@ }); | ||
if(this.xCallbacks.length){ | ||
xCallbackCheck = true; | ||
this.xCallbacks.each(function(_callback,_key,_break){ | ||
if(_callback(_string)){ | ||
xCallbackCheck = false; | ||
_break(); | ||
//At this point, either we had no inclusion checks, so by default everything is included and we're just checking exclusion, or we passed our inclusion checks | ||
if(includeCheck && iCallbackCheck){ | ||
if(this.excludes.count()){ | ||
//Our exclusion check is set to true initially (by default) | ||
if(is(_string) === 'string'){ | ||
this.excludes.each(function(_key,_RX){ | ||
let rxResult = _RX.exec(_string); | ||
if(is(rxResult) === 'array' && rxResult[0] !== ''){ | ||
//If it matches, we want to fail the check (return false) | ||
excludeCheck = false; | ||
return false; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
//Again we have two states, we have exclusionCallbacks and have not done any RegX exclusion checks, or the RegX exclusion checks will have passed and we need to also check against these | ||
if(this.xCallbacks.length && !this.excludes.count() || this.xCallbacks.length && excludeCheck){ | ||
//Default xCallback check is true | ||
this.xCallbacks.each(function(_callback){ | ||
if(_callback(_string)){ | ||
xCallbackCheck = false; | ||
return false; | ||
} | ||
}); | ||
} | ||
} | ||
return (excludeCheck && includeCheck && iCallbackCheck && xCallbackCheck ? true : false); | ||
@@ -105,0 +120,0 @@ } |
{ | ||
"name": "arc-check", | ||
"version": "1.0.0", | ||
"description": "An ES6 class that accepts include/exclude regular expression and callbacks to check a variable", | ||
"version": "1.0.1", | ||
"description": "An ES6 class to evaluate values against multiple complex inclusion/exclusion rules", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "tap test/*.js" | ||
}, | ||
@@ -23,6 +23,9 @@ "repository": { | ||
"dependencies": { | ||
"arc-is": "^1.0.0", | ||
"arc-array": "^1.0.0", | ||
"arc-object": "^1.0.0" | ||
"arc-array": "^2.0.0", | ||
"arc-is": "^1.0.2", | ||
"arc-object": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"tap": "^5.7.2" | ||
} | ||
} |
147
README.md
@@ -1,2 +0,145 @@ | ||
# arc-check | ||
An ES6 class that accepts include/exclude regular expression and callbacks to test whether a variable passes | ||
# arc-check [![Build Status](https://travis-ci.org/anyuzer/arc-check.svg?branch=master)](https://travis-ci.org/anyuzer/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` | ||
```js | ||
//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. | ||
```js | ||
//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` | ||
```js | ||
//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` | ||
```js | ||
//Example of addExclude | ||
var check = new ArcCheck(); | ||
check.addExclude('/^[a-z]*$/'); | ||
``` | ||
### .val(value:Mixed) | ||
Evaluate a value based on the inclusion/exclusion logic checks. | ||
```js | ||
//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 | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
11501
6
149
0
145
1
+ Addedarc-array@2.1.0(transitive)
+ Addedarc-check@1.2.1(transitive)
- Removedarc-array@1.1.0(transitive)
Updatedarc-array@^2.0.0
Updatedarc-is@^1.0.2
Updatedarc-object@^1.0.1