You can use FailFast to safeguard functions from bad input, and giving the caller an early warning that bad input is being passed.
// define a function, using FailFast to protect itself from bad input
function myFunction( myParam ){
FailFast.assert.isNumber( 'myParam must be a number, you passed: ' + myParam, myParam );
// your real code here
}
// call the function with bad input
myFunction( 'lorem ipsum' );
// throws Error with message property of
// "myParam must be a number, you passed: lorem ipsum"
// the execution will be halted, and the developer will immediately see
// what he's done wrong
- FailFast.assert
- FailFast.assert.isNotNull
- FailFast.assert.isBoolean
- FailFast.assert.isString
- FailFast.assert.isArray
- FailFast.assert.isFunction
- FailFast.assert.isDate
- FailFast.assert.isNumber
- FailFast.assert.isNormalNumber
- FailFast.assert.isObject
- FailFast.assert.hasProperty
- FailFast.assert.match
The tests are done using BusterJS and the excellent Sinon.JS.
- Build script to create the following wrappers
- jQuery plugin
- Ender.js wrapper
- More extensive examples of good use
FailFast uses Semantic Versioning for predictable versioning.
- v2.0
- Got rid of the mandatory msg argument, as it lead to a lot of repetition for very little value
- Grouping all functions under the assert function, should be familiar for users of Buster.JS
- Exposing the fail function, to make it easy for users to create custom assertions