@untydev/types

1.2.0 • Public • Published

@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/types

Usage

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) === true

isUndefined(v)

Checks whether v is undefined.

import { isUndefined } from '@untydev/types'

isUndefined() === true
isUndefined(undefined) === true

isBoolean(v)

Checks whether v is either true or false.

import { isBoolean } from '@untydev/types'

isBoolean(true) === true
isBoolean(false) === true

isInteger(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) === false

isNumber(v)

Checks whether v is a number, excluding infinite values.

import { isNumber } from '@untydev/types'

isNumber(1) === true
isNumber(NaN) === false
isNumber(Infinity) === false

isBigInt(v)

Checks whether v is a BigInt.

import { isBigInt } from '@untydev/types'

isBigInt(1) === false
isBigInt(1n) === true
isBigInt(BigInt(1)) === true

isString(v)

Checks whether v is a string, excluding String object.

import { isString } from '@untydev/types'

isString('') === true

isArray(v)

Checks whether v is an array using Array.isArray.

import { isArray } from '@untydev/types'

isArray([]) === true

isFunction(v)

Checks whether v is a function.

import { isFunction } from '@untydev/types'

isFunction(() => null) === true

isObject(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('') === false

isPlainObject(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()) === false

isSymbol(v)

Checks whether v is a Symbol.

import { isSymbol } from '@untydev/types'

isSymbol(Symbol('test')) === true

isIterable(v)

Checks whether v is iterable, e.g. implements the iterable protocol

import { isIterable } from '@untydev/types'

isIterable('') === true
isIterable([]) === true
isIterable({}) === false

isPromise(v)

Checks whether v is promise-like, e.g. implements then method.

import { isPromise } from '@untydev/types'

isPromise(Promise.resolve()) === true
isPromise({ then: () => null }) === true

ensure*(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'

Readme

Keywords

Package Sidebar

Install

npm i @untydev/types

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

26.6 kB

Total Files

11

Last publish

Collaborators

  • untydev