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

whats-the-type

v2.4.0

Published

Type detection beyond `typeof`

Downloads

33

Readme

What's the type?

DeepScan grade

typeof returns [object Object] for almost everything beyond primitives. This module works around this drawback and returns a more accurate type.

What's new in v2?

  • v2 brings some improvements but also introduces a breaking change by no longer returning its results in lower case.

    1. getType() now returns values in a case sensitive manner. Using lower case in v1 was meant to be more consistent with the behavior of typeof, but it made it impossible to distinguish between SomeObject and someobject.
    2. getType({ foo: 'bar' }) now returns PlainObject rather than object.
    3. getType(function* ()) now returns GeneratorFunction instead of generator.
    4. isGeneratorFunction() replaces isGenerator().
    5. getType() now works around the issue with Function.name and returns the object type correctly. It will fall back to Object in the case of failure.

    If you're using the is<Type>() functions only, your code will keep on working without any changes, except for the now fixed Function.name issue [5]. As for getType() you'll need to update your code with regards to the case sensitivity [1].

  • v2.1 - v2.4 add more code examples as well as a range of new is<Type>() functions; check out the complete list under Usage.

  • v2.4 adds reference tables as a look-up for the possible return values of getType().

Installation

npm install whats-the-type

Usage

What's the type comes with one generic detector, getType() and many is<Type>() functions:

Importing

You can import all functionality at once if you want to, but this means importing loads of things you might not need. Sure, treeshaking can make up for this but you can control things as well, the choice is yours.

// Import 'whats-the-type' if you have a bunch of different types to check. This imports all functions at once.
import detector from 'whats-the-type'; // note that whats-the-type is implemented as ESM and not in CJS

// use `getType` if you don't know what type to expect
import getType from 'whats-the-type/getType.js';

// use the `is<Type>` functions if you want to confirm if a value is of a specific type
import isString from 'whats-the-type/isString.js';
import isPlainObject from 'whats-the-type/isPlainObject.js';

getType()

getType() returns a string such as Null, Undefined, Function, String, etc. These values mostly correspond to the value's constructor name. In other words, the function doesn't simply return object for most types but is as precise as possible. If you are reading this at NPM, head over to the full documentation to find reference tables with just about any possible return value.

// the detector always returns a string
getType(['a', 'b', 'c']); // 'Array'

// use the import name (e.g. 'detector') if you imported everything
detector.getType(123); // 'Number'

// here are some more examples but getType() isn't limited to these
const v1 = null;
getType(v1); // "Null"

let v2;
getType(v2); // "Undefined"

const v3 = function() {};
getType(v3); // "Function", use isCallable() to cover async functions and generators as well

const v4 = async function() {};
getType(v4); // "AsyncFunction"

const v5 = function* generator(i) {};
getType(v5); // "GeneratorFunction"

const v6 = { foo: "bar" };
getType(v6); // "PlainObject", not "Object", as you may have expected

const v7 = new Map();
getType(v7); // "Map"

const v8 = new Set();
getType(v8); // "Set"

const v9 = new WeakMap();
getType(v9); // "WeakMap"

const v10 = new WeakSet();
getType(v10); // "WeakSet"

const v11 = new Date();
getType(v11); // "Date"

const v12 = new Error();
getType(v12); // "Error"

const v13 = new Promise(() => {});
getType(v13); // "Promise"

class CustomClass {}
const v14 = new CustomClass();
getType(v14); // "CustomClass"
getType(class CustomClass {}) // "Class", note the difference: CustomClass has not been initiated yet

is<Type>()

is<Type>() functions return true if the value is of the specified type. They exist for the most common types and are explained in the docs.

isString('a'); // true

isString(123); // false

isPlainObject({ a: 1 }); // true

isMap(new Map()); // true

isCallable(() => {}); // true

isAsyncFunction(async () => {}); // true

isGeneratorFunction(function* () {}); // true

isWeakMap(new WeakMap()); // true

isWeakSet(new WeakSet()); // true

Resources