js-partial-type-of

1.0.0 • Public • Published

js-partial-type-of

Recent Version Travis CI - Build Status Coveralls - Code Coverage Status David - Dependencies David - DevDependencies Doclets Gitter - Repository Chat

Synopsis

A partial to provide a better typeof capability. This typeof partial returns the practically correct type in string representation of the specific object. Written in UMD.

Compatible with ECMAScript 6.

Install

npm install js-partial-type-of

Usage - Include/Initialization

  • AMD (e.g.: RequireJS)
define(['js-partial-type-of'], function(typeOf) {        
    // you can now use typeOf
});
  • CommonJS (e.g.: NodeJS)
var typeOf = require('js-partial-type-of');
 
// you can now use typeOf
  • Browser
// load the source from "node_modules/js-partial-type-of/dist/js-partial-type-of.js" - for development
// or from "node_modules/js-partial-type-of/dist/js-partial-type-of.min.js" - for production
 
var typeOf = js_partial_type_of; // it is available in the global namespace
 
// you can now use typeOf

## Usage - After Initialization

  • General usage
// check the type of primitive types
typeOf(undefined) === 'undefined'
typeOf(null)      === 'null' // notice, that this will return **the practically correct type**
typeOf(true)      === 'boolean'
typeOf(false)     === 'boolean'
typeOf(-1)        === 'number'
typeOf(0)         === 'number'
typeOf(1)         === 'number'
typeOf(-Infinity) === 'number'
typeOf(Infinity)  === 'number'
typeOf(NaN)       === 'number'
typeOf('')        === 'string'
typeOf('string')  === 'string'
typeOf(Symbol())  === 'symbol'
 
// check the type of reference types
typeOf([])           === 'array' // notice, that this will return **the practically correct type**
typeOf({})           === 'object'
typeOf(function(){}) === 'function'
typeOf(new Date())   === 'date'
typeOf(new RegExp()) === 'regexp'
typeOf(/s+/g)        === 'regexp' // inline RegExp
typeOf(new Error())  === 'error' // also for any other errors, that extend the basic Error Object
  • Default behaviour
// default behaviour - similar to native typeof, but it returns **the practically correct types**
typeOf([])                === 'array' // notice, that this will return **the practically correct type**
typeOf({})                === 'object'
typeOf(function(){})      === 'function'
typeOf(new Date())        === 'date'
typeOf(new RegExp())      === 'regexp'
typeOf(new Int8Array(0))  === 'typedarray'
typeOf(new CustomClass()) === 'object'
  • Extended behaviours
// extended behaviour - return the specific type, rather useful for extended types
// when originalCase is not specified, by default, every type will be all lowercase.
typeOf([],                true) === 'array'
typeOf({},                true) === 'object'
typeOf(function(){},      true) === 'function'
typeOf(new Date(),        true) === 'date'
typeOf(new RegExp(),      true) === 'regexp'
 
// **note the differences below compared to previous example**
typeOf(new Int8Array(0),  true) === 'int8array'
typeOf(new CustomClass(), true) === 'customclass'
 
// extended behaviour - return the specific type in original case
// it is useful for logging and/or debugging purposes in general
typeOf([],                true, true) === 'Array'
typeOf({},                true, true) === 'Object'
typeOf(function(){},      true, true) === 'Function'
typeOf(new Date(),        true, true) === 'Date'
typeOf(new RegExp(),      true, true) === 'RegExp'
 
// **note the differences below compared to previous example**
typeOf(new Int8Array(0),  true, true) === 'Int8Array'
typeOf(new CustomClass(), true, true) === 'CustomClass'
// check the type for typed arrays
typeOf(new Int8Array(0))         === 'typedarray'
typeOf(new Uint8Array(0))        === 'typedarray'
typeOf(new Uint8ClampedArray(0)) === 'typedarray'
typeOf(new Int16Array(0))        === 'typedarray'
typeOf(new Uint16Array(0))       === 'typedarray'
typeOf(new Int32Array(0))        === 'typedarray'
typeOf(new Uint32Array(0))       === 'typedarray'
typeOf(new Float32Array(0))      === 'typedarray'
typeOf(new Float64Array(0))      === 'typedarray'
 
// check their types with specificType = true
typeOf(new Int8Array(0),         true) === 'int8array'
typeOf(new Uint8Array(0),        true) === 'uint8array'
typeOf(new Uint8ClampedArray(0), true) === 'uint8clampedarray'
typeOf(new Int16Array(0),        true) === 'int16array'
typeOf(new Uint16Array(0),       true) === 'uint16array'
typeOf(new Int32Array(0),        true) === 'int32array'
typeOf(new Uint32Array(0),       true) === 'uint32array'
typeOf(new Float32Array(0),      true) === 'float32array'
typeOf(new Float64Array(0),      true) === 'float64array'
 
// get their constructor's names in non-converted (original) case.
typeOf(new Int8Array(0),         true, true) === 'Int8Array'
typeOf(new Uint8Array(0),        true, true) === 'Uint8Array'
typeOf(new Uint8ClampedArray(0), true, true) === 'Uint8ClampedArray'
typeOf(new Int16Array(0),        true, true) === 'Int16Array'
typeOf(new Uint16Array(0),       true, true) === 'Uint16Array'
typeOf(new Int32Array(0),        true, true) === 'Int32Array'
typeOf(new Uint32Array(0),       true, true) === 'Uint32Array'
typeOf(new Float32Array(0),      true, true) === 'Float32Array'
typeOf(new Float64Array(0),      true, true) === 'Float64Array'

Documentation

Check the source here since it's well structured and documented. Also you can find the rendered jsDoc documentation on Doclets.io.

Also, check the unit tests in order to grasp the full-fledged capabilities.

Have fun! ;)

Issues

If you find any bugs and other issues, check the GSDC Guide - Issues section on how to submit issues in a standardized way on the project's issues page.

In case you have any suggestions regarding the project (features, additional capabilities, etc.), check the GSDC Guide - Suggestions section on how to submit suggestions in an easy, standardized way on the project's issues page.

Contribution

In order to contribute to this project, check the GSDC Guide for an easy, standardized way on how to contribute to projects.

Support

If you by any means find this project useful, consider supporting the organization.

There are multiple options to support the project and the developers. Any means of support is beneficial and helpful.

License

MIT @ Richard King

Package Sidebar

Install

npm i js-partial-type-of

Weekly Downloads

3

Version

1.0.0

License

MIT

Last publish

Collaborators

  • jsopenstd
  • richrdkng