defunction
v5.7.0
Published
Strict argument validation with testable errors
Downloads
19
Readme
defunction
Defensive javascript functions with run-time validation
Install
Install with npm
$ npm i defunction --save-dev
Usage
defunction takes three arguments.
defunction([validateArgument, validateArgument, ...], validateReturned, func)
And returns a defensive javascript function.
function validateNumber(label, arg) {
if (typeof arg !== 'number') {
throw new Error(`${label} type should be "number", not "${typeof arg}"`)
}
return arg
}
const add = defunction([validateNumber, validateNumber], validateNumber, function add(a, b) {
return a + b
})
add(1, '2')
>> Error: add:arguments[1] type should be "number", not "string"
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
License
Copyright © 2017 Licensed under the MIT license.
This file was generated by readme-generator on January 21, 2017.