Type Utilities
A tiny tree shakable utility library for runtime type checking.
The entire package weighs in at 1.07kb
.
See live demo.
Why this package?
This package exposes re-usable runtime type checking functions. This is useful for shrinking bundle sizes.
How to install
Install @analytics/type-utils
from npm.
npm install @analytics/type-utils
API
Below is the api for @analytics/type-utils
.
isBrowser
Check if currently in browser context
import { isBrowser } from '@analytics/type-utils'
if (isBrowser) {
console.log('do things in browser env')
}
isNode
Check if currently in Node.js context
import { isNode } from '@analytics/type-utils'
if (isNode) {
console.log('do things in node env')
}
isDeno
Check if currently in Deno context
import { isDeno } from '@analytics/type-utils'
if (isDeno) {
console.log('do things in deno env')
}
isWebWorker
Check if currently in WebWorker context
import { isWebWorker } from '@analytics/type-utils'
if (isWebWorker) {
console.log('do things in webworker env')
}
isJsDom
Check if currently in JSDOM context
import { isJsDom } from '@analytics/type-utils'
if (isJsDom) {
console.log('do things in JSDOM env')
}
isString
Check if value is string
import { isString } from '@analytics/type-utils'
const xyz = 'hi'
console.log(isString(xyz))
isNumber
Check if value is number
import { isNumber } from '@analytics/type-utils'
const xyz = 123
console.log(isNumber(xyz))
isBoolean
Check if value is boolean
import { isBoolean } from '@analytics/type-utils'
const myBool = true
console.log(isBoolean(myBool))
isPrimitive
Check if value is primitive JS value.
import { isPrimitive } from '@analytics/type-utils'
isPrimitive(true)
isPrimitive({})
isPrimitive(0)
isPrimitive('1')
isPrimitive(1.1)
isPrimitive(NaN)
isPrimitive(Infinity)
isPrimitive(function() {})
isPrimitive(Date),
isPrimitive(null)
isPrimitive(undefined)
isArray
Check if value is array
import { isArray } from '@analytics/type-utils'
const myArr = ['x', 'y']
console.log(isArray(myArr))
isObject
Check if value is object
import { isObject } from '@analytics/type-utils'
const myObj = { cool: 'hello' }
console.log(isObject(myObj))
isUndefined
Check if value is undefined
import { isUndefined } from '@analytics/type-utils'
let myval
console.log(isUndefined(myval))
isFunction
Check if value is function
import { isFunction } from '@analytics/type-utils'
function xyz() {}
console.log(isFunction(xyz))
isClass
Check if value is javascript class
import { isClass } from '@analytics/type-utils'
class MyClass {}
console.log(isClass(MyClass))
isPromise
Check if value is javascript promise
import { isPromise } from '@analytics/type-utils'
const myPromise = Promise.resolve()
console.log(isPromise(myPromise))
isRegex
Check if value is regular expression.
import { isRegex } from '@analytics/type-utils'
let myval = /pattern/gm
console.log(isRegex(myval))
isNoOp
Check if value is a noOp
function.
import { isNoOp } from '@analytics/type-utils'
function empty () { }
console.log(isNoOp(isNoOp))
isTruthy
Check if value is truthy.
import { isTruthy } from '@analytics/types-utils'
console.log(isTruthy(''))
console.log(isTruthy('false'))
console.log(isTruthy('FALSE'))
console.log(isTruthy(0))
console.log(isTruthy(null))
console.log(isTruthy(undefined))
console.log(isTruthy('true'))
console.log(isTruthy(1))
console.log(isTruthy({}))
console.log(isTruthy([]))
console.log(isTruthy(function() { }))
isEmail
Check if value is an email.
import { isEmail } from '@analytics/type-utils'
console.log(isEmail('email@email.com'))
console.log(isEmail('other-thing'))
isElement
Check if value is a a DOM node.
import { isElement } from '@analytics/type-utils'
const formElement = document.querySelector('.my-form')
console.log(isElement(formElement))
isNodeList
Check if value is a list of DOM nodes.
import { isNodeList } from '@analytics/type-utils'
const buttons = document.querySelectorAll('button')
console.log(isNodeList(buttons))
isForm
Check if value is a noOp
function.
import { isForm } from '@analytics/type-utils'
const formElement = document.querySelector('.my-form')
console.log(isForm(formElement))