A preconditions package based on Google's Preconditions library
npm install conditionalconditional
==================





Table of Contents
- conditional
- Summary
- Installation
- NPM
- Bower
- Browser version
- Usage
- Argument check
- State check
- Number type check
- Contains check
- Equals check
- Defined check
- Empty check
- Null check
- Building
- Testing
- Documentation
- Build + Test + Document
- Contributing
- Author
- License
This is a preconditions package for node modules based on Google's
Preconditions library. We all make certain assumptions when writing
code. Won't it be nice to assert that your assumptions about data is
correct? And to gracefully fail with a meaningful error message in case
the data is bad or assumption was not valid in the first place?
Consider a method called findMax:
`` js`
function findMax(arr) {
return Math.max.apply(Math, arr);
}
There is nothing wrong with this method, but, this will misbehave if you
pass an empty array:
` js`
findMax([4, 2, 1]); // returns 4
findMax([]); // returns -Infinity
This is just how Math.max works as described
here.
So, to avoid this scenario lets assert our assumption that the caller is
not going to supply an empty array:
` js
function findMax(arr) {
preconditions.checkArgument(arr.length > 0, 'array is empty');
return Math.max.apply(Math, arr);
}
`
Now, whenever a user sends in an empty array, a nice and meaningful
message can be printed:
` js`
findMax([]); // throws "IllegalArgumentError: array is empty"
` bash`
$ npm install conditional
This will install the bower component without the debug
dependency. You must to add this yourself if it is needed in your app.
` bash`
$ bower install preconditions
This library can be used in the browser. You can either copy the file
you need from dist folder, or, you can get it from cdnjs calledpreconditions. This will export a global variable calledpreconditions which will work exactly as defined in following usage
examples.
The usage of various checks differs slightly as explained.
Each check accepts a callback function as the last parameter. If passed,
and if the check fails, the callback will be invoked with the
error. This means that message and callback parameters are optional for
each precondition check. For example, checkArgument can be invoked in
any of these ways:
- no message or callback
` js`
checkArgument(typeof myVar === 'string')
If this check fails, a error will be thrown with the default message.
- a custom message but no callback
` js`
checkArgument(typeof myVar === 'string', 'expecting string value')
Upon failure, the above check will throw a error with the message
'expecting string value'.
- a custom callback but no message
` js`
checkArgument(typeof myVar === 'string', function(err) {
if (err != null) {
console.error('Something went wrong: ' + err.message);
}
});
So, if a callback is passed to a check, it will be invoked with the
error argument. Please note that the callback is invoked even if there
was no error (in which case the error is null).
- a custom message and a custom callback
` js`
checkArgument(arg > 0, 'expecting positive value', function(err) {
if (err != null) {
console.error('I was expecting a positive number');
}
});
This works in a similar fashion as the one above except that the error's
message will be the one we specified.
With this in mind, lets look at all the available precondition checks
below.
Checks whether argument satisfies certain condition.
` js`
checkArgument(condition:*, [message:string], [callback:function])
throws IllegalArgumentError
This will throw IllegalArgumentError with message equal to thecondition
supplied string if is false or undefined. If message"invalid argument"
is not provided, a default value of is assumed.
` js
var checkArgument = require('conditional').checkArgument;
function demo(arg) {
checkArgument(arg === 'test', "argument must be equal to 'test'");
continueWithNormalOperation();
}
`
This has a similar signature and usage as argument check defined
above. The only difference is in the error type and default error
message.
` js`
checkState(condition:*, [message:string], [callback:function])
throws IllegalStateError
The default value for error message is "illegal state".
Check for making sure that a variable contains numerical value.
` js`
checkNumberType(value:*, [message:string], [callback:function])
throws InvalidTypeError
` js`
checkNotNumberType(value:*, [message:string], [callback:function])
throws InvalidTypeError
In some cases you want to make sure that only numerical value are sent
to a method. For example, a method called square(x) which takes a
numerical value x and returns its squared value. This method expects
that the user will be sending a numerical value only. As we already know
by now, it is always better to put our assumptions in code:
` js
var checkNumberType = require('conditional').checkNumberType;
function square(x) {
checkNumberType(x, 'only numerical values can be squared');
return Math.pow(x, 2);
}
`
Check if a value is contained in another.
` js`
checkContains(value:, object:, [message:string], [callback:function])
throws UnknownValueError
` js`
checkDoesNotContain(value:, object:, [message:string], [callback:function])
throws UnknownValueError
As expected checkDoesNotContain behave exactly opposite as itscheckContains
counterpart .
This is a very flexible check since it can allow contains check with
numbers, strings, arrays or regular objects. Here are some of the rules
it follows:
- empty strings are equal
- null is not same as 0 (zero) or empty string
- 'number' can contain 'string' and vice versa (except for array objects
as explained below)
- array objects (second parameter) enforce strict types (for example
numbers and string are considered different in this case).
` js
var checkContains = require('conditional').checkContains;
function installPackage(userInput) {
checkContains(userInput, ['yes', 'y', 'no', 'n'], 'invalid input');
if (userInput.indexOf('y') === 0) {
// do install package
}
}
`
Check if two values are equal.
` js`
checkEquals(actual:, expected:, [message:string], [callback:function])
throws UnknownValueError
` js`
checkDoesNotEqual(actual:, expected:, [message:string], [callback:function])
throws UnknownValueError
Similar to contains check, this check also allows you to check against
any data type. It follows these rules:
- empty strings are equal
- null values are equalstring
- and number types are not equal in any conditionundefined
- values can not be checked against (will throw aIllegalArgumentError
)map
- order of key/value pair in a is not relevant. This means{val1 : 1, val2: 2} is same as {val2: 2, val1: 1}
` js
var checkEquals = require('conditional').checkEquals;
function login(password) {
checkEquals(password, 'expected-password', 'invalid password');
// password successfully validated
}
`
Check if a value is defined (or in other words, not undefined).
` js`
checkDefined(value:*, [message:string], [callback:function])
throws UndefinedValueError
` js`
checkUndefined(value:*, [message:string], [callback:function])
throws UndefinedValueError
This check follows these rules:
- null is a defined value
- an empty string is not undefined
- 0 (zero) is not undefined
- an empty array is not undefined
- an empty object is not undefined
` js
var checkDefined = require('conditional').checkDefined;
function sendMessage(message) {
checkDefined(message, 'a valid message required')
// proceed to send the message
}
`
Check if a value is empty or not.
` js`
checkEmpty(value:*, [message:string], [callback:function])
throws IllegalValueError
` js`
checkNotEmpty(value:*, [message:string], [callback:function])
throws IllegalValueError
notEmpty check follows these rules:null
- value is empty{}
- empty array or object (i.e. ) is also considered emptyfalse
- empty string is obviously considered empty
- value 0 (zero) or are not considered empty
` js
var checkNotEmpty = require('conditional').checkNotEmpty
function sendMessage(message) {
checkNotEmpty(message, 'message must not be empty');
// proceed to send the message
}
`
Check if value is null or undefined.
` js`
checkNull(value:*, [message:string], [callback:function])
throws IllegalValueError
` js`
checkNotNull(value:*, [message:string], [callback:function])
throws IllegalValueError
In most cases, you'd be more interested in the checkNotNullcheckDefined
precondition than the other. This precondition isnull
slightly different than this one as it does not check for s. Here
is an example:
` js
var checkNotNull = require('conditional').checkNotNull;
function parse(str) {
// do some string manipulation
}
function getUserInput(callback) {
readFromInput(function(err, str) {
if(err != null) {
callback(err);
} else {
checkNotNull(str, function (err) {
if(err != null) {
callback(null, null);
} else {
callback(null, parse(str));
}
});
}
});
}
`
To get the js source generated form coffee script:
` bash`
$ grunt coffee
This will put all js files in lib folder.
To execute tests, make sure
grunt is installed. Then run:
` bash`
$ grunt test
Before testing, this task will perform a lint check using
coffeelint. Tests will be executed if and
only if linting succeeds.
The default task of grunt will run this command as well. So, justgrunt
typing and pressing RET is also sufficient to run tests.
The debug module is integrated into this library. To enable it, run your
app like this:
` bash`
$ DEBUG=conditional npm start
Documentation is generated using
docco and placed in docs
folder. To build documentation:
` bash`
$ grunt docs
The build task of grunt will check linting, test everything,
generate docs and build javascript source. So, to execute:
` bash``
$ grunt build
Feel free to make a change and issue a pull request if you have a patch.
If you have a feature request or if you find a bug, please open a issue.
Anshul Verma ::
anshulverma ::
@anshulverma
The MIT License (MIT)
Copyright (c) 2014 Anshul Verma
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.