@finibit/types v0.1.1
@finibit/types
Dynamic type checking utilities for JavaScript.
Introduction
The @finibit/types package provides various functions for checking and ensuring dynamic types of JavaScript values.
Installation
Using NPM:
npm i @finibit/typesUsage
import { ensureArray, ensureFunction } from '@finibit/types'
function map (arr, fn) {
ensureArray(arr)
ensureFunction(fn)
return arr.map(fn)
}API
isNull(v)
Checks whether v is null.
import { isNull } from '@finibit/types'
isNull(null) === trueisUndefined(v)
Checks whether v is undefined.
import { isUndefined } from '@finibit/types'
isUndefined() === true
isUndefined(undefined) === trueisBoolean(v)
Checks whether v is either true or false.
import { isBoolean } from '@finibit/types'
isBoolean(true) === true
isBoolean(false) === trueisInteger(v)
Checks whether v is an integer using Number.isSafeInteger.
import { isInteger } from '@finibit/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 '@finibit/types'
isNumber(1) === true
isNumber(NaN) === false
isNumber(Infinity) === falseisString(v)
Checks whether v is a string, excluding String object.
import { isString } from '@finibit/types'
isString('') === trueisArray(v)
Checks whether v is an array using Array.isArray.
import { isArray } from '@finibit/types'
isArray([]) === trueisFunction(v)
Checks whether v is a function.
import { isFunction } from '@finibit/types'
isFunction(() => null) === trueisObject(v)
Checks whether v is a non-null object or a function.
import { isObject } from '@finibit/types'
isObject({}) === true
isObject([]) === true
isObject(() => null) === true
isObject(null) === false
isObject('') === falseisSymbol(v)
Checks whether v is a Symbol.
import { isSymbol } from '@finibit/types'
isSymbol(Symbol('test')) === trueisIterable(v)
Checks whether v is iterable, e.g. implements the iterable protocol
import { isIterable } from '@finibit/types'
isIterable('') === true
isIterable([]) === true
isIterable({}) === falseisPromise(v)
Checks whether v is promise-like, e.g. implements then method.
import { isPromise } from '@finibit/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 '@finibit/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 '@finibit/types'
typeOf(null) === 'null'
typeOf(NaN) === 'NaN'
typeOf(Infinity) === 'infinity'