npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@untydev/types

v1.2.0

Published

Dynamic type checking utilities for JavaScript

Downloads

6

Readme

@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'