@analytics/type-utils — Tiny runtime type checking utils. One of the small, focused utility packages that power the analytics library, usable standalone.
Full documentation, synced from the package README:
A tiny tree shakable utility library for runtime type checking in 2.3kb.
This library will work with analytics or as a standalone package.
Exposes 60 re-usable functions and environment flags for runtime type checking.
This package exposes re-usable runtime type checking functions. This is useful for shrinking bundle sizes.
Install @analytics/type-utils from npm.
npm install @analytics/type-utilsBelow is the api for @analytics/type-utils.
isProdBoolean flag, true when process.env.NODE_ENV is 'production'.
import { isProd } from '@analytics/type-utils'
if (isProd) {
console.log('do things in production env')
}isStagingBoolean flag, true when process.env.NODE_ENV is 'staging'.
import { isStaging } from '@analytics/type-utils'
if (isStaging) {
console.log('do things in staging env')
}isDevBoolean flag, true when process.env.NODE_ENV is 'development'.
import { isDev } from '@analytics/type-utils'
if (isDev) {
console.log('do things in development env')
}isBrowserBoolean flag, true when running in a browser context (document is defined).
import { isBrowser } from '@analytics/type-utils'
if (isBrowser) {
console.log('do things in browser env')
}isLocalHostBoolean flag, true when in a browser and window.location.hostname is 'localhost'.
import { isLocalHost } from '@analytics/type-utils'
if (isLocalHost) {
console.log('running on localhost')
}isNodeBoolean flag, true when running in a Node.js context.
import { isNode } from '@analytics/type-utils'
if (isNode) {
console.log('do things in node env')
}isDenoBoolean flag, true when running in a Deno context.
import { isDeno } from '@analytics/type-utils'
if (isDeno) {
console.log('do things in deno env')
}isWebWorkerBoolean flag, true when running in a WebWorker context.
import { isWebWorker } from '@analytics/type-utils'
if (isWebWorker) {
console.log('do things in webworker env')
}isJsDomBoolean flag, true when running in a JSDOM context.
import { isJsDom } from '@analytics/type-utils'
if (isJsDom) {
console.log('do things in JSDOM env')
}getTypeNameReturns the capitalized type name of a value.
import { getTypeName } from '@analytics/type-utils'
getTypeName([]) // 'Array'
getTypeName(null) // 'Null'getTypeReturns the type of a value, lowercased by default. Pass false to keep the capitalized name.
import { getType } from '@analytics/type-utils'
getType([]) // 'array'
getType(new Date()) // 'date'
getType([], false) // 'Array'ctorNameReturns the constructor name of a value, or null if it has no function constructor.
import { ctorName } from '@analytics/type-utils'
ctorName(new Date()) // 'Date'
ctorName([]) // 'Array'isFunctionCheck if value is a function.
import { isFunction } from '@analytics/type-utils'
function xyz() {}
isFunction(xyz) // trueisStringCheck if value is a string.
import { isString } from '@analytics/type-utils'
isString('hi') // trueisUndefinedCheck if value is undefined.
import { isUndefined } from '@analytics/type-utils'
let myVal
isUndefined(myVal) // trueisDefinedCheck if value is not undefined.
import { isDefined } from '@analytics/type-utils'
isDefined('hi') // true
isDefined(undefined) // falseisBooleanCheck if value is a boolean.
import { isBoolean } from '@analytics/type-utils'
isBoolean(true) // trueisSymbolCheck if value is a symbol.
import { isSymbol } from '@analytics/type-utils'
isSymbol(Symbol('x')) // trueisNullCheck if value is null.
import { isNull } from '@analytics/type-utils'
isNull(null) // trueisNumberCheck if value is a number. Returns false for NaN.
import { isNumber } from '@analytics/type-utils'
isNumber(123) // true
isNumber(NaN) // false
isNumber(Infinity) // trueisNumberLikeCheck if value can be parsed as a number.
import { isNumberLike } from '@analytics/type-utils'
isNumberLike('1.1') // true
isNumberLike('abc') // falseisClassCheck if value is a javascript class.
import { isClass } from '@analytics/type-utils'
class MyClass {}
isClass(MyClass) // trueisArrayCheck if value is an array.
import { isArray } from '@analytics/type-utils'
isArray(['x', 'y']) // trueisObjectCheck if value is a plain object.
import { isObject } from '@analytics/type-utils'
isObject({ cool: 'hello' }) // true
isObject([]) // falseisObjectLikeCheck if value is object-like (truthy and typeof object).
import { isObjectLike } from '@analytics/type-utils'
isObjectLike({}) // true
isObjectLike(null) // falseisJsonCheck if value is a parsable JSON string.
import { isJson } from '@analytics/type-utils'
isJson('{"a":5}') // true
isJson('[]') // true
isJson('{a":5}') // falseisPrimitiveCheck if value is a primitive scalar value.
import { isPrimitive } from '@analytics/type-utils'
isPrimitive(true) // true
isPrimitive(0) // true
isPrimitive(null) // true
isPrimitive({}) // false
isPrimitive(function() {}) // falseisMethodCheck if an object has a function at the given property name.
import { isMethod } from '@analytics/type-utils'
const obj = { key: () => {}, keyTwo: 'foobar' }
isMethod(obj, 'key') // true
isMethod(obj, 'keyTwo') // falseisPromiseCheck if value is a Promise.
import { isPromise } from '@analytics/type-utils'
isPromise(Promise.resolve()) // trueisGeneratorCheck if value is a generator.
import { isGenerator } from '@analytics/type-utils'
function* gen() { yield 1 }
isGenerator(gen()) // trueisGeneratorFunctionCheck if value is a generator function.
import { isGeneratorFunction } from '@analytics/type-utils'
isGeneratorFunction(function* () {}) // true
isGeneratorFunction(() => {}) // falseisAsyncFunctionCheck if value is an async function.
import { isAsyncFunction } from '@analytics/type-utils'
isAsyncFunction(async () => {}) // true
isAsyncFunction(() => {}) // falseisSetCheck if value is a Set.
import { isSet } from '@analytics/type-utils'
isSet(new Set()) // trueisMapCheck if value is a Map.
import { isMap } from '@analytics/type-utils'
isMap(new Map()) // trueisRegexCheck if value is a regular expression.
import { isRegex } from '@analytics/type-utils'
isRegex(/pattern/gm) // trueisBufferCheck if value is a Buffer.
import { isBuffer } from '@analytics/type-utils'
isBuffer(Buffer.from('x')) // trueisErrorCheck if value is an Error.
import { isError } from '@analytics/type-utils'
isError(new Error()) // trueisErrorLikeCheck if value has string name and message properties.
import { isErrorLike } from '@analytics/type-utils'
isErrorLike(new Error()) // true
isErrorLike({ name: 'Error!', message: 'This is an error' }) // true
isErrorLike({}) // falseisTypeErrorCheck if value is a TypeError.
import { isTypeError } from '@analytics/type-utils'
isTypeError(new TypeError()) // true
isTypeError(new Error()) // falseisSyntaxErrorCheck if value is a SyntaxError.
import { isSyntaxError } from '@analytics/type-utils'
isSyntaxError(new SyntaxError()) // true
isSyntaxError(new Error()) // falseisNoOpCheck if a function is a no-op (empty body).
import { isNoOp } from '@analytics/type-utils'
isNoOp(() => {}) // true
isNoOp(() => { console.log('hi') }) // falseisArgumentsCheck if value is a function arguments object.
import { isArguments } from '@analytics/type-utils'
function fn() { return isArguments(arguments) }
fn() // true
isArguments([]) // falseisTruthyCheck if value is truthy. The string 'false' is treated as false.
import { isTruthy } from '@analytics/type-utils'
isTruthy('true') // true
isTruthy(1) // true
isTruthy('false') // false
isTruthy(0) // falseisFalsyCheck if value is falsy.
import { isFalsy } from '@analytics/type-utils'
isFalsy(false) // true
isFalsy(null) // true
isFalsy('') // true
isFalsy([]) // falseisTrueCheck if value is exactly true.
import { isTrue } from '@analytics/type-utils'
isTrue(true) // true
isTrue(1) // falseisFalseCheck if value is exactly false.
import { isFalse } from '@analytics/type-utils'
isFalse(false) // true
isFalse(0) // falseisEmailCheck if value is an email address.
import { isEmail } from '@analytics/type-utils'
isEmail('email@email.com') // true
isEmail('other-thing') // falseisDateCheck if value is a Date.
import { isDate } from '@analytics/type-utils'
isDate(new Date()) // true
isDate('2022-01-02') // falseisIsoDateCheck if value is an ISO date string.
import { isIsoDate } from '@analytics/type-utils'
isIsoDate('2022-01-02T06:45:28.547Z') // true
isIsoDate('2022-01-02') // falseisEmptyCheck if value is empty (null, or an empty array, set, map, or object).
import { isEmpty } from '@analytics/type-utils'
isEmpty(null) // true
isEmpty([]) // true
isEmpty([1, 2, 3]) // false
isEmpty({ a: 1 }) // falseisNodeListCheck if value is a list of DOM nodes.
import { isNodeList } from '@analytics/type-utils'
const buttons = document.querySelectorAll('button')
isNodeList(buttons) // trueisElementCheck if value is a DOM element. Pass an optional node type to also match the element type.
import { isElement } from '@analytics/type-utils'
const formElement = document.querySelector('.my-form')
isElement(formElement) // true
isElement(formElement, 'form') // trueisNodeTypeCheck if a DOM element matches a given node name.
import { isNodeType } from '@analytics/type-utils'
const input = document.querySelector('input')
isNodeType(input, 'input') // trueisFormCheck if value is a <form> element.
import { isForm } from '@analytics/type-utils'
const formElement = document.querySelector('.my-form')
isForm(formElement) // trueisButtonCheck if value is a <button> element.
import { isButton } from '@analytics/type-utils'
const btn = document.querySelector('.my-button')
isButton(btn) // trueisInputCheck if value is an <input/> element.
import { isInput } from '@analytics/type-utils'
const input = document.querySelector('.my-input')
isInput(input) // trueisSelectCheck if value is a <select> element.
import { isSelect } from '@analytics/type-utils'
const select = document.querySelector('.my-select')
isSelect(select) // trueisHiddenCheck if a DOM element is hidden.
import { isHidden } from '@analytics/type-utils'
const el = document.querySelector('.my-el')
isHidden(el) // truenoOpA no-op function that does nothing.
import { noOp } from '@analytics/type-utils'
const callback = options.callback || noOp
callback()ensureArrayWrap a value in an array if it is not already one.
import { ensureArray } from '@analytics/type-utils'
ensureArray('x') // ['x']
ensureArray(['x', 'y']) // ['x', 'y']
ensureArray() // []