1.0.0 • Published 8 years ago

typeofs v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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.

Valuetype()name()nameOf()
nullnullnullnull
undefinedundefinedundefinedundefined
'foo',stringstringString
5numbernumberNumber
falsebooleanbooleanBoolean
/^yes/objectregexpRegExp
foo, "bar" arrayarrayArray
{ hello: 'world' }objectobjectObject
function() {}functionfunctionFunction
String( 'foo' )stringstringString
Number( '42' )numbernumberNumber
Boolean( '1' )booleanbooleanBoolean
new Date()objectdateDate
new RegExp( '^no','g' )objectregexpRegExp
new Array()arrayarrayArray
new Object()objectobjectObject
Object.create( null )objectobjectObject
new Function( 'x','y','return x + y' )functionanonymousanonymous
new Error( 'error' )objecterrorError
new TypeError( 'type error' )objecttypeerrorTypeError
ErrorfunctionerrorError
TypeErrorfunctiontypeerrorTypeError
NaNnumbernanNaN
InfinitynumbernumberNumber
MathobjectmathMath
JSON,objectjsonJSON
( function() { return arguments; } )()objectargumentsArguments
Symbol( 'foo' )symbolsymbolSymbol
Promise.resolve( 1 )objectpromisePromise
PromisefunctionpromisePromise
class TestClass {}functiontestclassTestClass
new Person( 'alice', 5 )objectpersonPerson
new AnonPerson( 'bob', 4 )objectanonpersonAnonPerson
new ( class Foo { constructor() {} } )objectfooFoo
new ( class { constructor() {} } )objectobjectObject
classVarfunctionnamedNamed
anonClassfunctionanonclassanonClass
function * gen() {}functiongengen

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.