@untydev/types v1.2.0
@untydev/types
Dynamic type checking utilities for JavaScript.
Introduction
The @untydev/types package provides various functions for checking and ensuring dynamic types of JavaScript values.
Installation
Using NPM:
npm i @untydev/typesUsage
import { ensureArray, ensureFunction } from '@untydev/types'
function map (arr, fn) {
  ensureArray(arr)
  ensureFunction(fn)  
  return arr.map(fn)
}API
isNull(v)
Checks whether v is null.
import { isNull } from '@untydev/types'
isNull(null) === trueisUndefined(v)
Checks whether v is undefined.
import { isUndefined } from '@untydev/types'
isUndefined() === true
isUndefined(undefined) === trueisBoolean(v)
Checks whether v is either true or false.
import { isBoolean } from '@untydev/types'
isBoolean(true) === true
isBoolean(false) === trueisInteger(v)
Checks whether v is an integer using Number.isSafeInteger.
import { isInteger } from '@untydev/types'
isInteger(1) === true
isInteger(0.1) === false
isInteger(NaN) === false
isInteger(Infinity) === falseisNumber(v)
Checks whether v is a number, excluding infinite values.
import { isNumber } from '@untydev/types'
isNumber(1) === true
isNumber(NaN) === false
isNumber(Infinity) === falseisBigInt(v)
Checks whether v is a BigInt.
import { isBigInt } from '@untydev/types'
isBigInt(1) === false
isBigInt(1n) === true
isBigInt(BigInt(1)) === trueisString(v)
Checks whether v is a string, excluding String object.
import { isString } from '@untydev/types'
isString('') === trueisArray(v)
Checks whether v is an array using Array.isArray.
import { isArray } from '@untydev/types'
isArray([]) === trueisFunction(v)
Checks whether v is a function.
import { isFunction } from '@untydev/types'
isFunction(() => null) === trueisObject(v)
Checks whether v is a non-null object or a function.
import { isObject } from '@untydev/types'
isObject({}) === true
isObject([]) === true
isObject(() => null) === true
isObject(null) === false
isObject('') === falseisPlainObject(v)
Checks whether v is an object a plain object which is an object created by the Object constructor or one with a
`[Prototype] of null. 
import { isPlainObject } from '@untydev/types'
isPlainObject({}) === true
isPlainObject(Object.create(null)) === true
isPlainObject(null) === false
class X {}
isPlainObject(new X()) === falseisSymbol(v)
Checks whether v is a Symbol.
import { isSymbol } from '@untydev/types'
isSymbol(Symbol('test')) === trueisIterable(v)
Checks whether v is iterable, e.g. implements the iterable protocol
import { isIterable } from '@untydev/types'
isIterable('') === true
isIterable([]) === true
isIterable({}) === falseisPromise(v)
Checks whether v is promise-like, e.g. implements then method.
import { isPromise } from '@untydev/types'
isPromise(Promise.resolve()) === true
isPromise({ then: () => null }) === trueensure*(v)
Additionally, every is* function has a ensure* counterpart, which throw TypeError exceptions if v is not a correct type.
import { ensureIterable } from '@untydev/types'
ensureIterable(null) // throws TypeError('Expected iterable but got null')typeOf(v)
Similarly to typeof operator, returns the type of v. It handles several special cases:
import { typeOf } from '@untydev/types'
typeOf(null) === 'null'
typeOf(NaN) === 'NaN'
typeOf(Infinity) === 'infinity'