0.1.0 • Published 8 years ago

hi5 v0.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

hi5

Lightweight type checker for bros :raised_hands:.

travis codecov

hi5 checks values against types, that's it, yup.

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.

Install

npm install --save hi5

Usage

Simple check

function add(a, b) {
  hi5(a, 'a', Number)
  hi5(b, 'b', Number)

  return a + b
}

add(1, 2)   // => 3
add('1', 2) // Error

Multiple types check

function display(val) {
  hi5(val, 'val', [String, Number])
  console.log(val)
}

display(1)    // => 1
display('1')  // => '1'
display({})   // Error

Optional arguments

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

Guard function

function add(a, b) {
  return a + b
}

const guardedAdd = hi5.guard(add, [
  [ 'a', Number ],
  [ 'b', Number ]
])

guardedAdd(1, 2)   // => 3
guardedAdd('1', 2) // Error

Tips

Guard on export

const hi5 = require('hi5')

function add(a, b) {
  return a + b
}

module.exports = hi5.guard(add, [
  [ 'a', Number ],
  [ 'b', Number ]
])

Rest parameters

function add(...args) {
  args.forEach((arg, i) => hi5(arg, `arg[${i}]`, [Number, String]))

  // [...]
}

License

MIT © Nicolas Gryman