UNPKG

pp-parachute

Version:
94 lines (80 loc) 2.7 kB
var typeAssertion = function (type) { return function (argument, message) { if (arguments.length === 0) { throw new Error('No assertion arguments!'); } if (typeof argument !== type) { throw new Error(message || 'Assertion failed! Expected a ' + type + ' but found ' + typeof argument); } } }; function Assert(condition, message) { if (!condition) { throw new Error(message || 'Assertion failed!'); } } Assert.isFunction = typeAssertion('function'); Assert.isString = typeAssertion('string'); Assert.isBoolean = typeAssertion('boolean'); Assert.isNumber = typeAssertion('number'); Assert.isUndefined = typeAssertion('undefined'); Assert.instanceOf = function(base, construct, message) { if(!(base instanceof construct)) { throw new Error(message || 'Expected base to be an instance of a given constructor'); } }; Assert.isDefined = function(something, message) { if(something === undefined) { throw new Error(message || 'Expected input to be defined.'); } }; Assert.isArray = function (array, message) { if (!Array.isArray(array)) { throw new Error(message || 'Assertion failed! Expected an array but found ' + typeof object); } }; Assert.isObject = function (object, message) { if (typeof object !== 'object') { throw new Error(message || 'Assertion failed! Expected an object but found ' + typeof object); } else if (!object) { throw new Error(message || 'Assertion failed! Expected an object but found null'); } }; Assert.isFalse = function (condition, message) { if (condition !== false) { throw new Error(message || 'Expected condition to be false!'); } }; Assert.isTrue = function (condition, message) { if (condition !== true) { if (condition !== false) { throw new Error(message || 'Expected condition to be true!'); } } }; Assert.isFalsy = function (condition, message) { Assert(!condition, message || 'Expected condition to be falsy!' ); }; Assert.isTruthy = Assert; Assert['in'] = function(input, typeArray, message) { var index = typeArray.indexOf(input); if(index === -1) { throw new Error(message || "Expected input to be in array"); } }; //Assert.any = function() { // var message = Array.prototype.pop.call(arguments); // var asserts = arguments.slice(0); // for(var i = 0; i < asserts.length; i++) { // try { // // } catch(e) { // // } // } // throw new Error(Message); //}; //if(window && window.angular) { // angular.module('Util.Assert', []).constant('Assert', Assert); //} module.exports = Assert;