5.3.0 • Published 9 years ago

conditional v5.3.0

Weekly downloads
84
License
-
Repository
github
Last release
9 years ago

conditional

Build Status Dependency Status Coverage Status NPM version

NPM

NPM Downloads

Table of Contents

Summary

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:

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:

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:

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:

findMax([]);  // throws "IllegalArgumentError: array is empty"

Installation

NPM

$ npm install conditional

Bower

This will install the bower component without the debug dependency. You must to add this yourself if it is needed in your app.

$ bower install preconditions

Browser version

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 called preconditions. This will export a global variable called preconditions which will work exactly as defined in following usage examples.

Usage

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
checkArgument(typeof myVar === 'string')

If this check fails, a error will be thrown with the default message.

  • a custom message but no callback
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
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
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.

Argument check

Checks whether argument satisfies certain condition.

checkArgument(condition:*, [message:string], [callback:function])
    throws IllegalArgumentError

This will throw IllegalArgumentError with message equal to the supplied string if condition is false or undefined. If message is not provided, a default value of "invalid argument" is assumed.

var checkArgument = require('conditional').checkArgument;

function demo(arg) {
  checkArgument(arg === 'test', "argument must be equal to 'test'");

  continueWithNormalOperation();
}

State check

This has a similar signature and usage as argument check defined above. The only difference is in the error type and default error message.

checkState(condition:*, [message:string], [callback:function])
    throws IllegalStateError

The default value for error message is "illegal state".

Number type check

Check for making sure that a variable contains numerical value.

checkNumberType(value:*, [message:string], [callback:function])
    throws InvalidTypeError
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:

var checkNumberType = require('conditional').checkNumberType;

function square(x) {
  checkNumberType(x, 'only numerical values can be squared');

  return Math.pow(x, 2);
}

Contains check

Check if a value is contained in another.

checkContains(value:*, object:*, [message:string], [callback:function])
    throws UnknownValueError
checkDoesNotContain(value:*, object:*, [message:string], [callback:function])
    throws UnknownValueError

As expected checkDoesNotContain behave exactly opposite as its counterpart checkContains.

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).
var checkContains = require('conditional').checkContains;

function installPackage(userInput) {
  checkContains(userInput, ['yes', 'y', 'no', 'n'], 'invalid input');

  if (userInput.indexOf('y') === 0) {
    // do install package
  }
}

Equals check

Check if two values are equal.

checkEquals(actual:*, expected:*, [message:string], [callback:function])
    throws UnknownValueError
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 equal
  • string and number types are not equal in any condition
  • undefined values can not be checked against (will throw a IllegalArgumentError)
  • order of key/value pair in a map is not relevant. This means {val1 : 1, val2: 2} is same as {val2: 2, val1: 1}
var checkEquals = require('conditional').checkEquals;

function login(password) {
  checkEquals(password, 'expected-password', 'invalid password');

  // password successfully validated
}

Defined check

Check if a value is defined (or in other words, not undefined).

checkDefined(value:*, [message:string], [callback:function])
    throws UndefinedValueError
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
var checkDefined = require('conditional').checkDefined;

function sendMessage(message) {
  checkDefined(message, 'a valid message required')

  // proceed to send the message
}

Empty check

Check if a value is empty or not.

checkEmpty(value:*, [message:string], [callback:function])
    throws IllegalValueError
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 empty
  • empty string is obviously considered empty
  • value 0 (zero) or false are not considered empty
var checkNotEmpty = require('conditional').checkNotEmpty

function sendMessage(message) {
  checkNotEmpty(message, 'message must not be empty');

  // proceed to send the message
}

Null check

Check if value is null or undefined.

checkNull(value:*, [message:string], [callback:function])
    throws IllegalValueError
checkNotNull(value:*, [message:string], [callback:function])
    throws IllegalValueError

In most cases, you'd be more interested in the checkNotNull precondition than the other. This checkDefined precondition is slightly different than this one as it does not check for nulls. Here is an example:

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));
        }
      });
    }
  });
}

Building

To get the js source generated form coffee script:

$ grunt coffee

This will put all js files in lib folder.

Testing

To execute tests, make sure grunt is installed. Then run:

$ 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, just typing grunt and pressing RET is also sufficient to run tests.

Debugging

The debug module is integrated into this library. To enable it, run your app like this:

$ DEBUG=conditional npm start

Documentation

Documentation is generated using docco and placed in docs folder. To build documentation:

$ grunt docs

Build + Test + Document

The build task of grunt will check linting, test everything, generate docs and build javascript source. So, to execute:

$ grunt build

Contributing

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.

Author

Anshul Verma :: anshulverma :: @anshulverma

License

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.

5.3.0

9 years ago

5.2.5

9 years ago

5.2.4

9 years ago

5.2.2

9 years ago

5.2.0

9 years ago

5.1.1

10 years ago

5.1.0

10 years ago

5.0.0

10 years ago

4.2.0

10 years ago

4.1.0

10 years ago

3.1.1

10 years ago

3.1.0

10 years ago

3.0.1

10 years ago

3.0.0

10 years ago