Skip to content

Instantly share code, notes, and snippets.

@arian
Created February 24, 2013 23:33
Show Gist options
  • Save arian/5026246 to your computer and use it in GitHub Desktop.
Save arian/5026246 to your computer and use it in GitHub Desktop.
better asserts, that can be switched of with a DEV variable.
"use strict";
if (typeof DEV == 'undefined') return;
function assert(condition, message){
if (!condition) throw new Error(message || "Assertion fault");
}
exports.assert = assert;
exports.assertFunction = function(fn, message){
assert(typeof fn == 'function', message || 'expected a function');
};
exports.assertNumber = function(num, message){
assert(typeof num == 'number', message || 'expected a number');
};
exports.assertNumber = function(str, message){
assert(typeof str == 'string', message || 'expected a string');
};
exports.assertLength = function(obj, len, message){
assert(obj && obj.length === len, message || 'expect object to have a length of ' + len);
};
exports.assertMinLength = function(obj, len, message){
assert(obj && obj.length >= len, message || 'expect object to have a minimum length of ' + len);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment