Motivation
Provide a minimal way to represent a behavior, whether it's allowed and the exceptions to the allowance.
Installation
npm install behavior-js
Roadmap
- Actions functions
- Actions functions async and sequential evaluations
Usage
Instantiation
new Behavior(allowed : boolean, exceptions : string[] | string)
Single exception
Let
var Behavior = ;// Whether the behavior is allowedvar politenessAllowed = true;// The exception to the behaviorvar politenessException = 'crude honesty';
Instantiation
var politeness = politenessAllowedpolitenessException;
Multiple exceptions
Let
var Behavior = ;// Whether the behavior is allowedvar leadershipAllowed = true;// The exceptions to the behaviorvar leadershipExceptions ='ego''superiority';
Instantiation
var leadership = leadershipAllowedleadershipExceptions;
Evaluation
behavior.isAllowed(action : string) : boolean
Let
var Behavior = ;// Whether the behavior is allowedvar agressionAllowed = false;// The exceptions to the behaviorvar agressionExceptions =// These are good to have, will be allowed.'competitiveness''power desire';// The initializationvar agression = agressionAllowedagressionExceptions;
Evaluation with an exempted value
// Returns trueagression;
Evaluation with a non-exempted value
// Returns falseagression;
Sample use case
Problem
I have a library that parses url query parameters but I would like to prevent some parameters from getting parsed
Solution
Instantiation
var Behavior = ; // These are the query parameters we want to prevent from getting parsedvar parsingExceptions = 'onSuccess' 'isNewUser'; // We instantiate the behavior, allow it and pass it's exceptionsvar parsing = trueparsingExceptions;
Usage
// .. somewhere in your awesome query parsing libraryqueryParser{ ifparsing // Do your parsing with complete peace of mind }
Tests
All of the tests are written in Jasmine with the BDD process
Requirements
npm install gulp typescript tslint tsd -g
Run
npm test