Giter Club home page Giter Club logo

wtf's Introduction

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!

wtf's People

Contributors

jesdavpet avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.