tagOf

1.0.2 • Public • Published

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.

Package Sidebar

Install

npm i tagOf

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • pwn