fjl-typeof
Strict typeof
implementation. Gives you a value's type name.
Usage
typeOf(x)
Returns a value's "type" name; E.g. (value).constructor.name
else for null
returns Null
; for undefined
returns Undefined
;
for NaN
returns 'NaN'; for every other value type returns their type's name (functionality includes the likes of Symbol
all
native javascript constructors and includes userland ones).
Params:
-
x
- Some value to get type name of.
Returns:
A string representing the type's name;
E.g., typeOf("hello") === 'String'
#####Examples:
typeOf('hello') === 'String'
typeOf(99) === 'Number'
typeOf(0 / 0) === 'NaN'
typeOf(Symbol('hello')) === 'Symbol'
typeOf(null) === 'Null'
typeOf(undefined) === 'Undefined'
etc..
Motivation
- Javascript doesn't have a built-in, all-around type checker. This function allows one to build one.
- Is a more robust alternative than using the native
typeof
since it gets type names of all native and userland types. - Very useful in-cases where many type checks are necessary (long logical (
switch
,if
) control statement chains etc.).
Pre-requistes
Ecmascript 5+
Installation
-
npm install fjl-typeof
or yarn add fjl-typeof
License
BSD 3