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

@jesdavpet/wtf

v1.1.0

Published

Utility library simplifying type detection and comparison.

Downloads

4

Readme

Type evaluation and comparison with Wtf.js

  • Side steps JavaScript's inconsistent typeof and instanceof operators
  • Dependency free, and ultra lightweight at 0.3kb (minified)

JavaScript's native typeof operator can be awkward, it's one of the language's warts. Take for example typeof null, which one would expect to return "Null", but is actually "Object"! Unfortunately instanceof is even less helpful than typeof.

This can lead to unnecessarily complex (and buggy) comparison statements. Enter Wtf.js to the rescue...

Using Wtf.js type evaluation and predicates

Predefined default types will return true / false indicating if the (optional) argument passed was of that type -- if no argument is passed, the type string for that type is returned instead:

  • Wtf.ARRAY([thing])
  • Wtf.FUNCTION([thing])
  • Wtf.NULL([thing])
  • Wtf.NUMBER([thing])
  • Wtf.OBJECT([thing])
  • Wtf.STRING([thing])
  • Wtf.UNDEFINED([thing])

Therefore, you can use these functions either as predicates or constants.

Test for an array type:

if (Wtf.ARRAY(x)) {
  // etc.
}

Display the type of an unknown object for debugging: console.log(Wtf.type(thing))

Handling a function argument which may be one of a variety of known types:

switch (Wtf.type(thing)) {
    case Wtf.STRING() : // Handle string case
        break;
    case Wtf.NUMBER() : // Handle number case
        break;
    case Wtf.ARRAY() : // Handle array case
        break;
    // etc.
}

Since custom objects / classes will evaluate as a Wtf.OBJECT(), the use of instanceof to narrow down the object's sub-type base on constructor as a second step.

if (Wtf.OBJECT(x)) {
  if (x instanceof FancyClass) doFancyThings();
  // etc.
}

On not using Wtf.js

Common libraries such as: jQuery, Underscore or Lodash, etc. include type helper functions that accomplish the same thing -- use those instead.

NOTE: Wtf.js is a reinvention of a very small wheel for a size optimized -- and, therefore, dependency-free -- JavaScript project. If that's your use case, then enjoy!