https://github.com/alexindigo/precise-typeof
Detects precise type of objects like `Array()`, `new Number(1)`, `new Boolean(true)`, etc
https://github.com/alexindigo/precise-typeof
array instance instanceof kind kindof object plain-object pojo precise type typeof
Last synced: 9 months ago
JSON representation
Detects precise type of objects like `Array()`, `new Number(1)`, `new Boolean(true)`, etc
- Host: GitHub
- URL: https://github.com/alexindigo/precise-typeof
- Owner: alexindigo
- License: mit
- Created: 2016-01-18T07:55:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-01-19T10:03:49.000Z (almost 7 years ago)
- Last Synced: 2024-04-26T18:22:49.825Z (over 1 year ago)
- Topics: array, instance, instanceof, kind, kindof, object, plain-object, pojo, precise, type, typeof
- Language: JavaScript
- Homepage: https://www.npmjs.com/precise-typeof
- Size: 72.3 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# precise-typeof [](https://www.npmjs.com/package/precise-typeof)
Better `typeof`. Detects real type of the objects like `Array()`, `new Number(1)`, `new Boolean(true)`, etc.
[](https://travis-ci.org/alexindigo/precise-typeof)
[](https://travis-ci.org/alexindigo/precise-typeof)
[](https://travis-ci.org/alexindigo/precise-typeof)
[](https://ci.appveyor.com/project/alexindigo/precise-typeof)
[](https://coveralls.io/github/alexindigo/precise-typeof?branch=master)
[](https://david-dm.org/alexindigo/precise-typeof)
[](https://opensource.org/licenses/MIT)
| compression | size |
| :----------------------- | ------: |
| precise-typeof.js | 1.69 kB |
| precise-typeof.min.js | 809 B |
| precise-typeof.min.js.gz | 391 B |
## Install
```sh
$ yarn add precise-typeof
```
or
```sh
$ npm install --save precise-typeof
```
## Examples
```javascript
var typeOf = require('precise-typeof');
typeOf({}); // -> 'object'
typeOf(new function(){}); // -> 'object'
typeOf([]); // -> 'array'
typeOf(25); // -> 'number'
typeOf(Infinity); // -> 'number'
typeOf('ABC'); // -> 'string'
typeOf(function(){}); // -> 'function'
typeOf(Math.sin); // -> 'function'
typeOf(undefined); // -> 'undefined'
typeOf(true); // -> 'boolean'
typeOf(null); // -> 'null'
typeOf(NaN); // -> 'nan'
// object values
typeOf(new Object()); // -> 'object'
typeOf(new Array()); // -> 'array'
typeOf(new Number(5)); // -> 'number'
typeOf(new Number(Infinity)); // -> 'number'
typeOf(new String('ABC')); // -> 'string'
typeOf(new Function('a', 'b', 'return a + b')); // -> 'function'
typeOf(new Boolean()); // -> 'boolean'
typeOf(new Number('blabla')); // -> 'nan'
// instances
typeOf(new function Moment(){}); // -> 'object'
// special objects
typeOf(/s/); // -> 'regexp'
typeOf(new Date()); // -> 'date'
typeOf(Math); // -> 'math'
typeOf(new Error()); // -> 'error'
typeOf(arguments); // -> 'arguments'
// node
typeOf(global); // -> 'global'
typeOf(process); // -> 'process'
typeOf(Buffer('B')); // -> 'buffer'
typeOf(new Buffer(2)); // -> 'buffer'
typeOf(Buffer([62, 64, 66])); // -> 'buffer'
// es6
typeOf(Symbol('A')); // -> 'symbol'
// browser
typeOf(window); // -> 'global'
typeOf(document); // -> 'html'
typeOf(document.body); // -> 'html'
typeOf(document.getElementsByTagName('html')[0]); // -> 'html'
typeOf(document.getElementsByTagName('div')); // -> 'html'
typeOf(document.createElement('a')); // -> 'html'
typeOf(document.createTextNode('Abcd')); // -> 'text'
typeOf(document.createComment('abcd')); // -> 'comment'
typeOf(document.createEvent('Event')); // -> 'event'
typeOf(document.createEvent('UIEvents')); // -> 'event'
typeOf(document.createEvent('HTMLEvents')); // -> 'event'
typeOf(document.createEvent('MouseEvents')); // -> 'event'
```
### `{ pojoOnly: true }`
With `pojoOnly` flag it only reports Plain-Old Javascript Objects as `object`,
and reports "instances" by their constructor names (e.g. `Moment` for `moment` instance).
In case if object was created from the nameless function, it will be reported as `unknown`.
```javascript
var typeOf = require('precise-typeof');
// reported differently with `{pojoOnly: true}`
typeOf(new function Moment(){}, {pojoOnly: true}); // -> 'Moment'
typeOf(new function ABC(){}, {pojoOnly: true}); // -> 'ABC'
typeOf(new function(){}, {pojoOnly: true}); // -> 'unknown'
// same with or without `{pojoOnly: true}`
typeOf({}, {pojoOnly: true}); // -> 'object'
typeOf([], {pojoOnly: true}); // -> 'array'
typeOf(25, {pojoOnly: true}); // -> 'number'
typeOf(Infinity, {pojoOnly: true}); // -> 'number'
typeOf('ABC', {pojoOnly: true}); // -> 'string'
typeOf(function(){}, {pojoOnly: true}); // -> 'function'
typeOf(Math.sin, {pojoOnly: true}); // -> 'function'
typeOf(undefined, {pojoOnly: true}); // -> 'undefined'
typeOf(true, {pojoOnly: true}); // -> 'boolean'
typeOf(null, {pojoOnly: true}); // -> 'null'
typeOf(NaN, {pojoOnly: true}); // -> 'nan'
typeOf(new Object(), {pojoOnly: true}); // -> 'object'
typeOf(new Array(), {pojoOnly: true}); // -> 'array'
typeOf(new Number(5), {pojoOnly: true}); // -> 'number'
typeOf(new Number(Infinity), {pojoOnly: true}); // -> 'number'
typeOf(new String('ABC'), {pojoOnly: true}); // -> 'string'
typeOf(new Function(), {pojoOnly: true}); // -> 'function'
typeOf(new Boolean(), {pojoOnly: true}); // -> 'boolean'
typeOf(new Number('blabla'), {pojoOnly: true}); // -> 'nan'
```
## License
Precise-TypeOf is released under the MIT license.