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

typeofs

v1.0.0

Published

A module for getting more useful type names as well as actual names, when available.

Downloads

2

Readme

Types and Names

Coveralls Status Build Status Dependency Status npm version License Known Vulnerabilities

This will provide more useful types or names from any JavaScript value.

Usage

For just the type() function

const { type } = require( 'typeofs' );

console.log( type( 5 ) ); // => 'number'

or if you want more functionality

const 
    { type, name, nameOf, isAsync, isGenerator, isClass, isInstance, isFunctionInstance } = require( 'typeofs' ),
    myFunc = function() { return arguments; };

console.log( type( myFunc ) ); // => 'function'
console.log( name( myFunc ) ); // => 'myfunc'
console.log( nameOf( myFunc ) ); // => 'myFunc'
console.log( type( myFunc() ) ); // => 'arguments'
console.log( name( myFunc() ) ); // => 'Arguments'

console.log( isAsync( async function () {} ) ); // => true
console.log( isGenerator( function* () {} ) ); // => true
console.log( isClass( class {} ) ); // => true
console.log( isInstance( new class {} ) ); // => true
console.log( isFunctionInstance( new function () {} ) ); // => true

Here is a list of examples that covers most types and names.

| Value | type() | name() | nameOf() | | :--- | :--- | :--- | :--- | | null | null | null | null | | undefined | undefined | undefined | undefined | | 'foo', | string | string | String | | 5 | number | number | Number | | false | boolean | boolean | Boolean | | /^yes/ | object | regexp | RegExp | | [ foo, "bar" ] | array | array | Array | | { hello: 'world' } | object | object | Object | | function() {} | function | function | Function | | String( 'foo' ) | string | string | String | | Number( '42' ) | number | number | Number | | Boolean( '1' ) | boolean | boolean | Boolean | | new Date() | object | date | Date | | new RegExp( '^no','g' ) | object | regexp | RegExp | | new Array() | array | array | Array | | new Object() | object | object | Object | | Object.create( null ) | object | object | Object | | new Function( 'x','y','return x + y' ) | function | anonymous | anonymous | | new Error( 'error' ) | object | error | Error | | new TypeError( 'type error' ) | object | typeerror | TypeError | | Error | function | error | Error | | TypeError | function | typeerror | TypeError | | NaN | number | nan | NaN | | Infinity | number | number | Number | | Math | object | math | Math | | JSON, | object | json | JSON | | ( function() { return arguments; } )() | object | arguments | Arguments | | Symbol( 'foo' ) | symbol | symbol | Symbol | | Promise.resolve( 1 ) | object | promise | Promise | | Promise | function | promise | Promise | | class TestClass {} | function | testclass | TestClass | | new Person( 'alice', 5 ) | object | person | Person | | new AnonPerson( 'bob', 4 ) | object | anonperson | AnonPerson | | new ( class Foo { constructor() {} } ) | object | foo | Foo | | new ( class { constructor() {} } ) | object | object | Object | | classVar | function | named | Named | | anonClass | function | anonclass | anonClass | | function * gen() {} | function | gen | gen |

Functions

name( value )

Returns the name of the value, if available, as all lower case.

type( value )

Returns the type of the value as all lower case. Similar in most respects to the builtin typeof except it returns an Array as array and not object.

nameOf( value )

Returns the name of the value, if available, retaining upper and lower case letters.

isInstance( value )

This returns true if the object is an instance of a class. It has to be an instance of a class specifically, a function does not count as a constructor for this function.

isFunctionInstance( value )

Returns true if the object is an instance of a function (i.e. new function () {}). Will return false if it is an instance of a class.

isClass( value )

This returns true if the function provided has been defined with the class keyword.

isAsync( value )

Returns true if the function is defined using the async keyword.

isGenerator( value )

Will be true if the function is a generator.

functionInfo( value, returnIfNotFunction = null )

This will return an object with information about a function. If the value provided is not a function it will return null by default or whatever has been provided by the optional secodn parameter. The reason for why you might want to provide an alternate return would be situations like this:

functionInfo( 5 ).isGenerator   // Error, can't read 'isGenerator' of 'null'
// but...
functionInfo( 5, {} ).isGenerator   // falsey

infoOf( value )

Return an object with additional information regarding the value provided. The object will have fields similar to the functions listed above, if applicable and truthy.