underscore.assert
Plugin for underscore - add method _.assert
- This is not for tests files in project...
- This is not instead of Jasmine or any test framework...
- This is inline code for testing production code!
_
| |
__ _ ___ ___ ___ _ __| |_
/ _` / __/ __|/ _ \ '__| __|
| (_| \__ \__ \ __/ | | |_
\__,_|___/___/\___|_| \__|
Install
npm install underscore.assert
Usage
_.assert(typeof fn === 'function', '*fn* should be function');
// throws AssertionError
_.assert(list.length, '*list* should contains any value');
// - for non-empty list nothing to do
// - for empty list throws AssertionError
_.assert(false); // => throw `AssertionError`
_.assert.ErrorConstructor = MyCustomError;
_.assert(false); // => throw `MyCustomError`Example
// instead of this:
function isUrl(url) {
if (typeof url === 'string') {
throw new Error('URL should be a *string* value');
}
// ... URL validation
}
// ... You can that:
function isUrl(url) {
_.assert(typeof url === 'string', 'URL should be a *string* value');
// ... URL validation
}
There is a shorter, nicer and better practice.