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

is-explicit

v3.0.1

Published

Combines instance of operator and typeof operator in a way that works seamlessly with objects and literals.

Downloads

138

Readme

is-explicit

Why ?

  • You want to do explicit type checks.
  • You don't want to have to differentiate between typeof and instanceof
  • You keep a cool head and bring justice to chaos.

2.0

Breaking changes from the 1.x release, but the resulting changes make is-explicit faster and more composable.

Usage

npm install is-explicit

import is from 'is-explicit'

Does a variable have a value?

is.defined(undefined)         //false
is.defined(null)              //false
is.defined(NaN)               //false
is.defined(10)                //true

Is a variable a specific type?

is([], Object)         //true
is([], Array)          //true

is(true, Boolean)      //true
is(false, Boolean)     //true
is(10, Number)         //true

is('str', String)      //true
is('str', Object)      //false

is(new String('str'), String)   //true
is(new String('str'), Object)   //true

is(/expr/, RegExp)             //true
is(/expr/, Object)             //true

is(function(){}, Object)       //true
is(function(){}, Function)     //true

is(new function(){}, Object)   //true
is(new function(){}, Function) //False

is(Array, Object)      //true
is(Array, Function)    //true
is(Array, Array)       //false

is(Symbol(), Symbol)        //true
is(Symbol.iterator, Symbol) //true
is(Symbol(), Object)        //false

Is a variable a custom type?

function Foo() {}
is (new Foo(), Foo) // true

const foo = function(){}
const bar = function(){}

is(new foo(), foo) // true
is(new bar(), foo) // false

Is a variable one of multiple types?

is('str', [Number, Boolean, String]) // true

Types are expected to be functions:

is('str', 'otherstring') //throws Error

Is a value an Array of a specific type?

is.arrayOf(['str'], String)                       // == true
is.arrayOf([0, false, new Date(), 'str'], String) // == false
is.arrayOf([0,'str',10,'cake'], [String, Number]) // == true,

Is a value an Object of a specific type?

is.objectOf([ 0, 'str' ], String)                     // == false
is.objectOf({ }, String)                              // == false
is.objectOf({ foo: '1', bar: '2' }, String)           // == true
is.objectOf({ foo: '1', bar: 2 }, [ String, Number ]) // == true

Is a value a plain Object?

is.plainObject(['str'])             // == false
is.plainObject(new Date())          // == false
is.plainObject({})                  // == true
is.plainObject(new Object())        // == true

function FooBar() { }

is.plainObject(new FooBar)           // == false

Binding

Binding is now binds it to a type:

class Foo { }

const isFoo = Foo::is

isFoo(new Foo()) // true

Bind to multiple types:

const isBoolOrFunc = [ Boolean, Function ]::is

isBoolOrFunc(false) // true

Better composition:

const mixed = [ 0, 'string', 1, true, 2, Symbol('bad') ]

const numbers = mixed.filter(Number::is)

Shortcuts

import is from 'is-explicit'

const value = 'whatever'

is.string(value)    // == String::is(value)
is.number(value)    // == Number::is(value)
is.bool(value)      // == Boolean::is(value)
is.func(value)      // == Function::is(value)
is.symbol(value)    // == Symbol::is(value)
is.primitive(value) // == [ Boolean, Number, String ]::is(value)

is.arrayOf.string(value)    // == String::is.arrayOf
is.arrayOf.number (value)   // == Number::is.arrayOf
is.arrayOf.bool(value)      // == Boolean::is.arrayOf
is.arrayOf.symbol(value)    // == Symbol::is.arrayOf
is.arrayOf.func(value)      // == Function::is.arrayOf
is.arrayOf.primitive(value) // == [ Boolean, Number, String ]::is.arrayOf

is.objectOf.string(value)    // == String::is.objectOf
is.objectOf.number (value)   // == Number::is.objectOf
is.objectOf.bool(value)      // == Boolean::is.objectOf
is.objectOf.symbol(value)    // == Symbol::is.objectOf
is.objectOf.func(value)      // == Function::is.objectOf
is.objectOf.primitive(value) // == [ Boolean, Number, String ]::isArrayOf

Additional Helpers

import is from 'is-explicit'

is.defined() // returns true if input is not null, undefined or NaN

is.arrayOf.plainObject() // returns true if input is an array of plain objects

is.instanceable() // returns true if input is a function with a prototype (ie: arrow functions will return false)

is.subclassOf() // returns true if input is a class that extends the provided type