Coder Social home page Coder Social logo

tagof's Introduction

tagOf.js

version build status downloads

saucelabs matrix

typeof alternative:

  • robust and well tested
  • works with user-defined classes
  • works with node, AMD and all browsers, including legacy ones like IE6
// tagOf( value:any ): string

tagOf( null ) // 'null'
tagOf( undefined ) // 'undefined'
tagOf( 0 ) // 'number'
tagOf( new Number( 0 ) ) // 'number'
tagOf( '' ) // 'string'
tagOf( new String( '' ) ) // 'string'
tagOf( true ) // 'boolean'
tagOf( false ) // 'boolean'
tagOf( new Boolean( true ) ) // 'boolean'
tagOf( new Boolean( false ) ) // 'boolean'
tagOf( {} ) // 'object'
tagOf( [] ) // 'array'
tagOf( function () {} ) // 'function'
tagOf( new Date() ) // 'date'
tagOf( /^/i ) // 'regexp'
tagOf( new RegExp() ) // 'regexp'

// ES6 additions
tagOf( Symbol( 'tagOf') ) // 'symbol'
tagOf( Symbol.for( 'tagOf' ) ) // 'symbol'
tagOf( new Set() ) // 'set'
tagOf( new WeakSet() ) // 'weakset'
tagOf( new Map() ) // 'map'
tagOf( new WeakMap() ) // 'weakmap'

// user-defined classes
class Circle {}
function Square() {}
tagOf( new Circle() ) // 'circle'
tagOf( new Square() ) // 'square'
tagOf( new class {} ) // 'object' - does not work with anonymous class
tagOf( new ( function {} ) ) // 'object' - does not work with anonymous constructor

Install

Node:

$ npm install --save tagOf
const tagOf = require( 'tagOf' )

Browser:

<script src="path/to/tagOf.js"></script>

tagOf will register itself as an anonymous AMD module if AMD loader presents, otherwise it is defined as a browser global named unsuprisingly, tagOf

Caveats

Does not work with Error objects.

tagof's People

Contributors

mov-78 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

r2fresh

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.