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

@danielvigaru/check-type-of

v1.0.3

Published

[![CI](https://github.com/danielvigaru/check-type-of/actions/workflows/node.js.yml/badge.svg)](https://github.com/danielvigaru/check-type-of/actions/workflows/node.js.yml) [![npm](https://img.shields.io/badge/npm-FFF?style=flat&logo=npm&logoColor=fff&colo

Downloads

7

Readme

CheckType.of()

CI npm bundlephobia Ko-fi

Why did I create this?

Because in JavaScript, (almost) everything is an object:

typeof {}; // -> 'object' - totally agree
typeof []; // -> 'object' - are you sure?
typeof new Date(); // -> 'object' - ok, kind of makes sense because class instances are objects in JS, but it's useless
typeof null; // -> 'object' - what?
typeof undefined; // -> 'undefined' - finally something normal
typeof NaN; // -> 'number' - how? it's in the name

And to add more complex type checking:

// ES6 class
class A {}
typeof A; // -> 'function'
CheckType.of(A).type; // -> 'class'

// ES5 class
function B() {}
B.prototype.someMethod = function () {};
typeof B; // -> 'function'
CheckType.of(B).type; // -> 'class'

// Don't worry, these still work as expected:
CheckType.of(() => {}).type; // -> 'function'
CheckType.of(function () {}).type; // -> 'function'

Usage / Examples

Import the CheckType class without instantiating it; just use CheckType.of(item) to access all the methods.

import { CheckType, TItemType } from '@danielvigaru/check-type-of';

Using the type property

Get the type of an item by accessing the type: TItemType property:

typeof []; // -> 'object'
CheckType.of([]).type; // -> 'array'

See how it says 'array' and not 'object'? Yeah.

Using the is... and isNot... methods

These methods take a callback as a parameter and execute it if the item being checked matches the specified type. You can chain multiple is... methods.

  1. Use is... and isNot... methods like you would use if/else statements:
CheckType.of(new Date())
    .isDate(() => console.log("Looks like we've got ourselves a date!"))
    .isNotDate(() => console.log('This is anything but a date'));

// Output: "Looks like we've got ourselves a date!"

Note: You can't chain multiple isNot... methods because it wouldn't make sense; it would execute all/most of them each time.

  1. Chain multiple methods like you would in a switch statement:
CheckType.of(someUnknownVariable)
    .isNumber(() => console.log("'It's a number"))
    .isDate(() => console.log("Looks like we've got ourselves a date!"))
    .isArray(() => console.log("['A', 'r', 'r', 'a', 'y']"));

Method Reference

All of these methods have an isNot... counterpart.

| Method | Executes the callback if the item is: | .type property | | ------------- | -------------------------------------------- | ---------------- | | isArray | [], instanceof Array | 'array' | | isBoolean | true, false | 'boolean' | | isClass | ES5 class, ES6 class | 'class' | | isDate | instanceof Date | 'date' | | isFunction | a function that isn't an ES5 class | 'function' | | isMap | instanceof Map | 'map' | | isNull | null | 'null' | | isNullish* | null, undefined | - | | isNumber | a number: decimal, octal, hex, binary, float | 'number' | | isObject | {}, instanceof Object | 'object' | | isString | a string | 'string' | | isUndefined | undefined | 'undefined' |

* Utility method that does not assign a value to the .type property.