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
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.