Mavis
Reliable, light-weight, highly-specific type checking for javascript. Like a thorough typeof
.
I found myself writing this code over and over, so I figured I'd make a package out of it and send it to the internet.
Usage
Mavis works both in node and the browser. Simply include it as a <script>
tag or
$ npm install mavis
and require('mavis')
from your app.
examples
// nodevar type =
<!-- browser -->
// objects // 'object' // 'array' {} // 'object' // functions // 'function' // 'function' // 'generator' // numbers // 'number' // 'number' // 'infinity' // 'NaN' // strings // 'string' // 'string' // errors // 'error' // 'error' // 'error' // 'error' // 'error' // 'error' // html // 'html' // 'html' // 'html' { return document;} // 'html' // 'html' // 'html' // 'html' // special // 'global' // 'storage' // 'date' // 'map' // 'set' // 'promise' // 'arguments' // 'RegExp' // 'uint16array'
type.is(actual, expected)
Mavis comes with type comparison. The result of a comparison is a boolean, unless you pass an array to match against, in which case the matching type is returned.
type // truetype // falsetype // truetype // 'number' type // truetype // falsetype // 'RegExp'type // false
advantages
Mavis is intentionally string-based. Some libraries check by constructor, although this forces you to ensure the type is defined before using it. By using strings, type-checking is completely backwards/forwards compatible. If a type you need to check against isn't supported in your browser, you can check for it without worrying. It's just a string!
final words
If you have any issues, problems, or questions, please submit an issue on GitHub.
Thanks for using Mavis!