Typa
A super-simple JavaScript type checker. Mainly pilfered from this blog post by Webbjocke. No dependencies. 3.6K GCC compiled, 2.0K uncompiled.
Install
npm install typa
Import
import is from 'typa'
/* or */
const is = require('typa')
Quick Start
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
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 → Serialized JSON object
- nll → Null
- noru → Null or undefined
- num → Number
- obj → Object
- prom → Promise
- regex → Regular expression
- str → String
- sym → Symbol
- undef → Undefined
Typa Method
Ternary function that checks if the supplied value matches the specified type, then returns the first function (or value) if true or the second function (or value) if false.
.typa($type, $value, $fn1, $fn2)
const myString = 'this is a string'
const myArray = 'this is also a string, not an array'
const fn1 = (() => console.log('hello'))
const fn2 = (() => console.log('goodbye'))
is.typa('str', myString, fn1, fn2)
// => 'hello'
is.typa('arr', myArray, fn1, fn2)
// => 'goodbye'
Individual Methods
.arr($value) — Array
const isArray = is.arr(['text', 12])
// => true
.bad($value) — Null, undefined, empty, or an error
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
.bool($value) — Boolean
let isBool = is.bool(true)
// => true
isBool = is.bool(false)
// => true
.date($value) — Date
const isDate = is.date(new Date())
// => true
.empty($value) — Empty string, array, or object
let isEmpty = is.empty('')
// => true
isEmpty = is.empty([])
// => true
isEmpty = is.empty({})
// => true
.err($value) — Error
const isErr = is.err(new Error('This is an error.'))
// => true
.fn($value) — Function
const isFn = is.fn(() => { console.log('Hi!') })
// => true
.int($value) — Integer
const isInt = is.int(12)
// => true
.json($value) — Serialized JSON object
const isJson = is.json('{"key": "value"}')
// => true
.nll($value) — Null
const isNll = is.nll(null)
// => true
.noru($value) — Null or Undefined
let isNoru = is.noru(null)
// => true
isNoru = is.noru(undefined)
// => true
.num($value) — Number
const isNum = is.num(28.2)
// => true
.obj($value) — Object
const isObj = is.obj({ key: 'value' })
// => true
.prom($value) — Promise
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
const isRegex = is.regex(new Regex(/\W/))
// => true
.str($value) — String
const isStr = is.str('text')
// => true
.sym($value) — Symbol
const isSym = is.sym(Symbol(42))
// => true
.undef($value) — Undefined
const isUndef = is.undef(undefined)
// => true