Package Exports
- type-name
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (type-name) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
type-name
Just a reasonable typeof
DESCRIPTION
typeName function returns reasonable type name for input value.
| description | input | result |
|---|---|---|
| string literal | 'foo' |
'string' |
| number literal | 5 |
'number' |
| boolean literal | false |
'boolean' |
| regexp literal (Android 4.1+) | /^not/ |
'RegExp' |
| array literal | ['foo', 4] |
'Array' |
| object literal | {name: 'bar'} |
'Object' (be careful!) |
| function expression | function () {} |
'function' |
| String object | new String('foo') |
'String' |
| Number object | new Number('3') |
'Number' |
| Boolean object | new Boolean('1') |
'Boolean' |
| Date object | new Date() |
'Date' |
| RegExp object (Android 4.1+) | new RegExp('^not', 'g') |
'RegExp' |
| Array object | new Array() |
'Array' |
| Object object | new Object() |
'Object' |
| Function object | new Function('x', 'y', 'return x + y') |
'function' (be careful!) |
| Error object | new Error('error!') |
'Error' |
| TypeError object | new TypeError('type error!') |
'TypeError' |
| NaN | NaN |
'number' |
| Infinity | Infinity |
'number' |
| Math | Math |
'Math' |
| JSON (IE8+) | JSON |
'JSON' |
| arguments object (IE9+) | (function(){ return arguments; })() |
'Arguments' |
| null literal | null |
'null' |
| undefined value | undefined |
'undefined' |
| User-defined constructor | new Person('alice', 5) |
'Person' |
| Anonymous constructor | new AnonPerson('bob', 4) |
'' |
EXAMPLE
var typeName = require('type-name'),
assert = require('assert');
assert(typeName('foo') === 'string');
assert(typeName(5) === 'number');
assert(typeName(false) === 'boolean');
assert(typeName(/^not/) === 'RegExp');
assert(typeName(['foo', 4]) === 'Array');
assert(typeName({name: 'bar'}) === 'Object');
assert(typeName(function () {}) === 'function');
assert(typeName(new String('foo')) === 'String');
assert(typeName(new Number('3')) === 'Number');
assert(typeName(new Boolean('1')) === 'Boolean');
assert(typeName(new Date()) === 'Date');
assert(typeName(new RegExp('^not', 'g')) === 'RegExp');
assert(typeName(new Array()) === 'Array');
assert(typeName(new Object()) === 'Object');
assert(typeName(new Function('x', 'y', 'return x + y')) === 'function');
assert(typeName(new Error('error!')) === 'Error');
assert(typeName(new TypeError('type error!')) === 'TypeError');
assert(typeName(NaN) === 'number');
assert(typeName(Infinity) === 'number');
assert(typeName(Math) === 'Math');
assert(typeName(JSON) === 'JSON'); // IE8+
assert(typeName((function(){ return arguments; })()) === 'Arguments'); // IE9+
assert(typeName(null) === 'null');
assert(typeName(undefined) === 'undefined');
function Person(name, age) {
this.name = name;
this.age = age;
}
var AnonPerson = function(name, age) {
this.name = name;
this.age = age;
};
assert(typeName(new Person('alice', 5)) === 'Person');
assert(typeName(new AnonPerson('bob', 4)) === '');INSTALL
via npm
Install
$ npm install --save type-nameUse
var typeName = require('type-name');
console.log(typeName(anyVar));use type-name npm module on browser
typeName function is exported
<script type="text/javascript" src="./path/to/node_modules/type-name/build/type-name.js"></script>via bower
Install
$ bower install --save type-nameLoad (typeName function is exported)
<script type="text/javascript" src="./path/to/bower_components/type-name/build/type-name.js"></script>Use
console.log(typeName(anyVar));AUTHOR
CONTRIBUTORS
LICENSE
Licensed under the MIT license.