npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

precise-typeof

v2.0.1

Published

Better 'typeof'. Detects real type of the objects like 'Array()', 'new Number(1)', 'new Boolean(true)', etc.

Downloads

3,531

Readme

precise-typeof NPM Module

Better typeof. Detects real type of the objects like Array(), new Number(1), new Boolean(true), etc.

PhantomJS Build Linux Build MacOS Build Windows Build

Coverage Status Dependency Status MIT license

| compression | size | | :----------------------- | ------: | | precise-typeof.js | 1.69 kB | | precise-typeof.min.js | 809 B | | precise-typeof.min.js.gz | 391 B |

Install

$ yarn add precise-typeof

or

$ npm install --save precise-typeof

Examples

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.

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.