Simple argument invariant checker for JavaScript.
var checkArguments = function(errors) {
var gotError = false;
for(var argName in errors) {
var checks = errors[argName];
for(var i in checks) {
var check = checks[i];
if(check.invariant) {
console.warn(argName + ' ' + check.error + '.');
gotError = true;
}
})
}
return gotError;
}
var getAmount = function() {
return 42; // just something to test out our doStuff
}
var doStuff = function(from, to) {
var amount = getAmount();
var gotError = checkArguments({
from: [
{invariant: from < 0, error: 'too small'},
{invariant: from >= amount, error: 'too big'}
],
to: [
{invariant: to < 0, error: 'too small'},
{invariant: to >= amount, error: 'too big'}
]
});
if(gotError) {
return; // raise an Exception, return or something
}
// ok, we're fine. let's rock
// ...
// still rocking :)
}