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

@analytics/type-utils

v0.6.2

Published

Tiny runtime type checking utils

Downloads

667,661

Readme

Type Utilities

A tiny tree shakable utility library for runtime type checking.

The entire package weighs in at 2.22kb.

See live demo.

Why this package?

This package exposes re-usable runtime type checking functions. This is useful for shrinking bundle sizes.

How to install

Install @analytics/type-utils from npm.

npm install @analytics/type-utils

API

Below is the api for @analytics/type-utils.

isBrowser

Check if currently in browser context

import { isBrowser } from '@analytics/type-utils'

if (isBrowser) {
  console.log('do things in browser env')
}

isNode

Check if currently in Node.js context

import { isNode } from '@analytics/type-utils'

if (isNode) {
  console.log('do things in node env')
}

isDeno

Check if currently in Deno context

import { isDeno } from '@analytics/type-utils'

if (isDeno) {
  console.log('do things in deno env')
}

isWebWorker

Check if currently in WebWorker context

import { isWebWorker } from '@analytics/type-utils'

if (isWebWorker) {
  console.log('do things in webworker env')
}

isJsDom

Check if currently in JSDOM context

import { isJsDom } from '@analytics/type-utils'

if (isJsDom) {
  console.log('do things in JSDOM env')
}

isString

Check if value is string

import { isString } from '@analytics/type-utils'

const xyz = 'hi'
console.log(isString(xyz))
// true

isNumber

Check if value is number

import { isNumber } from '@analytics/type-utils'

const xyz = 123
console.log(isNumber(xyz))
// true

isBoolean

Check if value is boolean

import { isBoolean } from '@analytics/type-utils'

const myBool = true
console.log(isBoolean(myBool))
// true

isPrimitive

Check if value is primitive JS value.

import { isPrimitive } from '@analytics/type-utils'

isPrimitive(true) // =>  true
isPrimitive({}) // => false
isPrimitive(0) // =>  true
isPrimitive('1') // =>  true
isPrimitive(1.1) // =>  true
isPrimitive(NaN) // =>  true
isPrimitive(Infinity) // =>  true
isPrimitive(function() {}) // => false
isPrimitive(Date), // => false
isPrimitive(null) // =>  true
isPrimitive(undefined) // =>  true

isArray

Check if value is array

import { isArray } from '@analytics/type-utils'

const myArr = ['x', 'y']
console.log(isArray(myArr))
// true

isObject

Check if value is object

import { isObject } from '@analytics/type-utils'

const myObj = { cool: 'hello' }
console.log(isObject(myObj))
// true

isUndefined

Check if value is undefined

import { isUndefined } from '@analytics/type-utils'

let myval
console.log(isUndefined(myval))
// true

isFunction

Check if value is function

import { isFunction } from '@analytics/type-utils'

function xyz() {}
console.log(isFunction(xyz))
// true

isClass

Check if value is javascript class

import { isClass } from '@analytics/type-utils'

class MyClass {}
console.log(isClass(MyClass))
// true

isPromise

Check if value is javascript promise

import { isPromise } from '@analytics/type-utils'

const myPromise = Promise.resolve()
console.log(isPromise(myPromise))
// true

isErrorLike

Check if value is javascript isErrorLike

import { isErrorLike } from '@analytics/type-utils'

isErrorLike(new Error()) // True
isErrorLike({ name: "Error!", message: "This is an error", other: 0 }) // True
isErrorLike({}) // False
isErrorLike({ name: "Error", message: null }) // False
// Works as a typguard
const something = {name: "Error", message: "This is an error"} as unknown
if (isErrorLike(something)) {
  console.log(something.name) // No Typescript error
}

isRegex

Check if value is regular expression.

import { isRegex } from '@analytics/type-utils'

let myval = /pattern/gm
console.log(isRegex(myval))
// true

isNoOp

Check if value is a noOp function.

import { isNoOp } from '@analytics/type-utils'

function empty () { }
console.log(isNoOp(isNoOp))
// true

isTruthy

Check if value is truthy.

import { isTruthy } from '@analytics/types-utils'

console.log(isTruthy('')) // false
console.log(isTruthy('false')) // false
console.log(isTruthy('FALSE')) // false
console.log(isTruthy(0)) // false
console.log(isTruthy(null)) // false
console.log(isTruthy(undefined)) // false
console.log(isTruthy('true')) // true
console.log(isTruthy(1)) // true
console.log(isTruthy({})) // true
console.log(isTruthy([])) // true
console.log(isTruthy(function() { })) // true

isEmail

Check if value is an email.

import { isEmail } from '@analytics/type-utils'

console.log(isEmail('[email protected]'))
// true
console.log(isEmail('other-thing'))
// false

isElement

Check if value is a a DOM node.

import { isElement } from '@analytics/type-utils'

const formElement = document.querySelector('.my-form')
console.log(isElement(formElement))
// true

isNodeList

Check if value is a list of DOM nodes.

import { isNodeList } from '@analytics/type-utils'

const buttons = document.querySelectorAll('button')
console.log(isNodeList(buttons))
// true

isForm

Check if value is a noOp function.

import { isForm } from '@analytics/type-utils'

const formElement = document.querySelector('.my-form')
console.log(isForm(formElement))
// true

Alternative libs

  • Native node types utils
  • https://github.com/jonschlinkert/kind-of
  • https://github.com/enricomarino/is
  • https://github.com/mesqueeb/is-what
  • https://github.com/stdlib-js/assert