Super-simple, zero-dependency JavaScript type checker utility.
   
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.
``sh`
yarn add typa || npm install typa
`sh`
yarn test
`js
import is from 'typa' || const is = require('typa')
const hello = 'Hello!'
const goodbye = ['Goodbye!', 'Adios!', 'Au revoir!']
if (is.str(hello)) console.log(hello)
// => 'Hello!'
if (is.str(goodbye)) console.log(hello)
// => no result
`
- _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
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)
`js
const isStr = () => console.log('I am a string')
const aintStr = () => console.log('I am not a string')
is.typa('str', 'Am I a string?', isStr, aintStr)
// => 'I am a string'
is.typa('str', ['Am', 'I', 'a', 'string', '?'], isStr, aintStr)
// => 'I am not a string'
`
Returns a string or an array of strings matching the type of the supplied value.
.what(\$value)
`js
is.what('This is a string')
// => 'string'
is.what(['This', 'is', 'an', 'array'])
// => ['array', 'object']
`
`js`
const isArray = is.arr(['text', 12])
// => true
.bad(\$value) ā Null, undefined, empty, or an error
`js
let isBad = is.bad(null)
// => true
isBad = is.bad(undefined)
// => true
isBad = is.bad({})
// => true
isBad = is.bad(new Error('This is an error'))
// => true
`
`js
let isBool = is.bool(true)
// => true
isBool = is.bool(false)
// => true
`
`js`
const isDate = is.date(new Date())
// => true
.empty(\$value) ā Empty string, array, or object
`js
let isEmpty = is.empty('')
// => true
isEmpty = is.empty([])
// => true
isEmpty = is.empty({})
// => true
`
`js`
const isErr = is.err(new Error('This is an error.'))
// => true
`js`
const isFn = is.fn(() => {
console.log('Hi!')
})
// => true
`js`
const isInt = is.int(12)
// => true
.json(\$value, \$type ['str'|'obj']) ā JSON string (default) or object
`js
let isJson = is.json('{"key": "value"}')
// => true
isJson = is.json({ key: 'value' }, 'obj')
// => true
`
`js`
const isNil = is.nil(null)
// => true
.nullish(\$value) ā Null or Undefined
`js
let isNullish = is.nullish(null)
// => true
isNullish = is.nullish(undefined)
// => true
`
`js`
const isNum = is.num(28.2)
// => true
`js`
const isObj = is.obj({ key: 'value' })
// => true
`js
const myPromise = new Promise((resolve, reject) => {
try {
console.log('I make a promise to you')
resolve()
} catch (err) {
reject(err)
}
})
const isProm = is.prom(myPromise)
// => true
`
.regex(\$value) ā Regular Expression
`js`
const isRegex = is.regex(new Regex(/\W/))
// => true
`js`
const isStr = is.str('text')
// => true
`js`
const isSym = is.sym(Symbol(42))
// => true
`js``
const isUndef = is.undef(undefined)
// => true
š¤ Daniel Sieradski
- Website: self.agency
- Twitter: @selfagency_llc
- GitLab: @selfagency
Most of the checks comprising this library were pilfered from this blog post by Webbjocke.
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Give a āļø if this project helped you!