Typa: Zero-dep JS type checker 🧐
The easy-peasy zero-dependency JavaScript type checker that asks, "What typa input is that?" Minified packaged version is 2 entire KB.
Notice: Breaking changes in v0.3.0. New bundler, plus nll
is now nil
and noru
is now nullish
.
Install
yarn add typa || npm install typa
Run tests
yarn test
Basic Usage
const hello = 'Hello!'const goodbye = 'Goodbye!' 'Adios!' 'Au revoir!' if is console // => 'Hello!' if is console // => no result
API
- arr → Array
- bad → Null, undefined, empty, or an error
- bool → Boolean
- date → Date
- empty → Empty string, array, or object
- err → Error
- fn → Function
- int → Integer
- json → JSON string or object
- nil → Null
- nullish → Null or undefined
- num → Number
- obj → Object
- prom → Promise
- regex → Regular expression
- str → String
- sym → Symbol
- undef → Undefined
Typa Method
Ternary operator that checks if the supplied value matches the specified type, then returns the first callback function or value if true or the second callback function or value if false.
.typa($type, $value, $fn1, $fn2)
const isStr = consoleconst aintStr = console is// => 'I am a string' is// => 'I am not a string'
What Method
Returns a string or an array of strings matching the type of the supplied value.
.what($value)
is// => 'string' is// => ['array', 'object']
Individual Type Methods
const isArray = is// => true
.bad($value) — Null, undefined, empty, or an error
let isBad = is// => true isBad = is// => true isBad = is// => true isBad = is// => true
let isBool = is// => true isBool = is// => true
const isDate = is// => true
.empty($value) — Empty string, array, or object
let isEmpty = is// => true isEmpty = is// => true isEmpty = is// => true
const isErr = is// => true
const isFn = is// => true
const isInt = is// => true
.json($value, $type ['str'|'obj']) — JSON string (default) or object
let isJson = is// => true isJson = is// => true
const isNil = is// => true
.nullish($value) — Null or Undefined
let isNullish = is// => true isNullish = is// => true
const isNum = is// => true
const isObj = is// => true
const myPromise = { try console catch err } const isProm = is// => true
.regex($value) — Regular Expression
const isRegex = is// => true
const isStr = is// => true
const isSym = is// => true
const isUndef = is// => true
Author
👤 Daniel Sieradski hello@self.agency
- Website: self.agency
- Twitter: @selfagency_llc
- GitLab: @selfagency
Acknowledgements
Most of the checks comprising this library were pilfered from this blog post by Webbjocke.
Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a ⭐️ if this project helped you!