guard-clause

Guard clause utility functions.

npm install guard-clause
2 downloads in the last week
8 downloads in the last month

guard-clause

guard-clause is a utility to simplify writing common guard clauses.

Examples

var guard = require("guard-clause");

var concat = function(arg1, arg2){
    guard.isString(arg1, "arg1");
    guard.isString(arg2, "arg2");
    return arg1 + arg2;
};

console.log(concat("hello, ", "world!")); // writes "hello, world!"
console.log(concat(1, 2)); // throws error

Usage

var guard = require("guard-clause");

// Array guard (throw exceptions)
guard.isArray(void 0, "paramName");
guard.isArray(null, "paramName");
guard.isArray(true, "paramName");
guard.isArray(false, "paramName");
guard.isArray("", "paramName");
guard.isArray(0, "paramName");
guard.isArray(new Date(), "paramName");
guard.isArray({}, "paramName"); 

// Array guard (ok)
guard.isArray([], "paramName");

// Boolean guard (throw exceptions)
guard.isBool(void 0, "paramName");
guard.isBool(null, "paramName");
guard.isBool([], "paramName");
guard.isBool("", "paramName");
guard.isBool(0, "paramName");
guard.isBool(new Date(), "paramName");
guard.isBool({}, "paramName"); 

// Boolean guard (ok)
guard.isBool(true, "paramName");
guard.isBool(false, "paramName");

// Date guard (throw exceptions)
guard.isDate(void 0, "paramName");
guard.isDate(null, "paramName");
guard.isDate(true, "paramName");
guard.isDate(false, "paramName");
guard.isDate([], "paramName");
guard.isDate("", "paramName");
guard.isDate(0, "paramName");
guard.isDate({}, "paramName"); 

// Date guard (ok)
guard.isDate(new Date(), "paramName");

// Number guard (throw exceptions)
guard.isNumber(void 0, "paramName");
guard.isNumber(null, "paramName");
guard.isNumber(true, "paramName");
guard.isNumber(false, "paramName");
guard.isNumber([], "paramName");
guard.isNumber("", "paramName");
guard.isNumber(new Date(), "paramName");
guard.isNumber({}, "paramName"); 

// Number guard (ok)
guard.isNumber(0, "paramName");

// Object guard (throw exceptions)
guard.isObject(void 0, "paramName");
guard.isObject(null, "paramName");
guard.isObject(true, "paramName");
guard.isObject(false, "paramName");
guard.isObject([], "paramName");
guard.isObject("", "paramName");
guard.isObject(0, "paramName");
guard.isObject(new Date(), "paramName");

// Object guard (ok)
guard.isObject({}, "paramName"); 

// String guard (throw exceptions)
guard.isString(void 0, "paramName");
guard.isString(null, "paramName");
guard.isString(true, "paramName");
guard.isString(false, "paramName");
guard.isString([], "paramName");
guard.isString(0, "paramName");
guard.isString(new Date(), "paramName");
guard.isString({}, "paramName"); 

// String guard (ok)
guard.isString("", "paramName");
npm loves you