Friendly lightweight type checker .
MIT License
Friendly lightweight type checker 🙌.
hi5 checks values against types, that's it!
It's meant to be used for function arguments validation and avoid errors related to a lack of type checking. That's why you can guard functions to release some burden.
npm install --save hi5
function add(a, b) {
hi5(a, 'a', Number)
hi5(b, 'b', Number)
return a + b
}
add(1, 2) // => 3
add('1', 2) // Error
function display(val) {
hi5(val, 'val', [String, Number])
console.log(val)
}
display(1) // => 1
display('1') // => '1'
display({}) // Error
const add = (a, b) => hi5(a, 'a', Number) + hi5(b, 'b', Number)
add(1, 2) // => 3
add('1', 2) // Error
function display(val, options) {
hi5(val, 'val', [String, Number])
hi5.optional(options, 'options', Object)
// [...]
console.log(val)
}
display(1) // => 1
display(1, { color: 'red' }) // => 1
display(1, 2) // Error
function add(a, b) {
return a + b
}
const guardedAdd = hi5.guard(add, [['a', Number], ['b', Number]])
guardedAdd(1, 2) // => 3
guardedAdd('1', 2) // Error
const hi5 = require('hi5')
function add(a, b) {
return a + b
}
module.exports = hi5.guard(add, [['a', Number], ['b', Number]])
function add(...args) {
args.forEach((arg, i) => hi5(arg, `arg[${i}]`, [Number, String]))
// [...]
}
MIT © Nicolas Gryman