Giter Site home page Giter Site logo

typecheck.js's Introduction

TypeCheck.js

TypeCheck is an improved version of JavaScript's typeof operator.

For example, typeof 12.0 simply returns 'number'. typeCheck.getType(12.0) provides more detailed information as well: ['number', 'finite', 'integer'].

And for an array, typeof [] in plain JavaScript just returns 'object'. JavaScript arrays are also objects, so typeCheck.getType([]) returns both: ['object', 'array'].

Here are the types, and subtypes of each type, that TypeCheck recognizes:

Type Subtypes
'number' 'infinite', 'finite', 'integer'
'string' 'numericString', 'integerString'
'object' 'array', 'function', 'date', 'regexp'
'boolean'
'undefined'
'null'
'NaN'

These are mostly consistent with the types returned by the typeof operator.

However, unlike typeof in plain JavaScript:

  • TypeCheck has separate types for null and NaN, whereas typeof considers null to be an 'object' and NaN (Not A Number) to be a 'number'.
  • TypeCheck classifies 'function' as a subtype of 'object', whereas typeof considers it a 'function' only.
  • TypeCheck also classifies 'regexp' as a subtype of 'object', whereas typeof in some older browsers considers a regexp to be a 'function' instead.

Usage

TypeCheck provides the following methods:

typeCheck.getTypes(valueToCheck);

getTypes returns an array of strings, which lists all of valueToCheck's types and subtypes.

typeCheck.typeMatch(valueToCheck, typeOrSubtype)

typeMatch returns true if the array returned by typeCheck.getTypes(valueToCheck) includes typeOrSubtype.

typecheck.js's People

Contributors

jeremykohn avatar

Watchers

 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.