TypeOf-In
allows your values to be compared with a set of Constructors or Strings representing the expected type.
Foretaste:
var typeOf = ;
Why use TypeOf-In ?
typeof and instanceof are, somehow, broken
null
For example, null has a type of an object, but it is not an instance of Object.
typeof null; // 'object'null instanceof Object //false
[fun fact] Object.prototype has the same result as null with typeof and instanceof
RegEx
Using a regular expression literal, someone would expect typeof to return a specific value like "regexp" which would be great in a perfect world.
typeof /regularExpression/ // 'object'/regularExpression/ instanceof RegExp // true
So, if you're dealing with regex, use instanceof.
Primitives x Objects
Unlike RegEx, other values like Number, String or Boolean have some issues when we want to retrieve their type when we use them as primitive or object (wrapper).
typeof 42 //'object' , create an objecttypeof Number42 //'number', create a primitive valuetypeof 42 //'number' 666 instanceof Number //false42 instanceof 666constructor //true, because:666constructor === Numberprototypeconstructor
So, the previous example shows that it is possible to verify a primitive value with typeof and its wrapper with instanceof but we can't test both of them with the same method even if they share the same constructor. One method to deal with this problem would be to use typeof value.valueOf().
NaN
One of the most famous example in JavaScript is NaN (a.K.a "Not a Number") which return a type of: number...
typeof NaN //'number' *sigh*typeof NaN //'object' *sigh* x 2
Prototypes
As you may have noticed above, prototypes have a weird behavior. For example, the prototype of an Array is an empty Array, and it is the same thing with Number, String, Boolean... which store a default value (0,"",false). Therefore, we would expect them to be an instance of their own constructor. But, sadly, it is not the case...
Numberprototype instanceof Number //falseNumberprototype instanceof Numberprototypeconstructor //false //the best method so far to deal with it.ObjectprototypetoString //'[object Number]'
And many more...
why TypeOf-In help us:
TypeOf-In uses typeof-- instead of typeof and instanceof to retrieve the type. typeof-- extracts the constructor name or call Object.prototype.toString to get the type matching your value which allows this library to test all kinds of values accurately. Global Objects reference
TypeOf-In supports:
Regex
Multi choices
primitive values and their respective wrapper return the same type
NaN, undefined and null have their own types: #NaN, #Undefined and #Null
it uses instanceof when necessary
and more!
table of type returned by typeof-- (as string)
Usage:
var typeOf = ;
Through an object
basic:
You can retrieve the type of your value as string by using getType
; //'String'
Or you can test your value with a string representing an expected type, or by using its constructor. For performance issues, you would prefer to use strings as much as you can, and also because only a few constructors are supported by all browsers (example: Promise, IndexedDB API...).
//better performances, support multi-platform; //'String' === 'String'; //'String' === 'String'; // '#Null' === '#Null' //better readability, use instanceof when possible; //'String' === 'String'; // x instanceof String; // '#Null' === '#Null'
multi:
It is also possible to compare your value with a set of different types by using an array.
; //false
regex:
Furthermore, TypeOf-In also supports RegEx against your value,
which is quite useful with < #####Error > or < HTML##### > types for example.
; //all errors except 'Error'; //HTMLBodyElement [IE9+]
ES6 and others objects:
This library can check all kind of objects. Allowing you to compare ES6 features like Promises, Generators, fat arrows... which is pretty neat.
//or, if we want to use instanceofvar GeneratorFunction = {}constructor
calling several times:
This is the main advantage of using TypeOf-In through an object, it allows you to deal with the same value in different ways
var myType = ;ifmyType //do something with the value as a stringelse ifmyType //you need to init your value!else ifmyType //something bad happened! but not a global errorelse ifmyType //is type 'Error'...
dealing with Objects:
This example shows different scenario where TypeOf-In will have a different behavior if the type is passed as a string or as a constructor.
Important: the library will not return an empty string("") but a "#Anonymous" type in the case of an instance of an anonymous constructor to improve readability.
//use instanceof: return true //use string comparison //true //false //true //true //false //more example ? {}; // ES6 equivalent: class Human {} var Person = Human; var {}; var person = ; var person1 = ; var person2 = ; ; //'Human' ; //'Human' ; //true ; //true //#instance against instance //if one of them has a valid constructor //use: x instanceof y.constructor; otherwise return false; ; //true ; //false //#instance against constructor ; //true ; //true ; //false ; //true //#special cases: instance of Anonymous (it behaves as the above examples) var {} myAnonymous = ; ; //return '#Anonymous' //true //true //true
Through a function
(decrease readability)
The recent version of typeof-in allows you to directly call the function in charge of the comparison, and by extension, not create an object every time you use typeOf() in your code.
This feature works exactly like the previous examples.
//for comparison: with one argument (default behavior); //with more than one argument:var typeOfIn = typeOf;;; //with zero argument : TypeOf-In expose getType() and In().var typeOf = ;typeOf //'String'typoOf;
force Object.prototype.toString
It is possible to force the use of Object.prototype.toString in typeof-- by adding the string 'forceObjectToString' at the last parameter of the In and getType methods.
;; var typeOfIn = typeOf;; var typeOf = ;typeOf;typeOf
with requireJS (AMD)
/*make sure to change the paths and module names according to your projects.*/require; ;
TypeOf only
In the case you only need the function used to retrieve the type of your values as a string (like typeof), you might be interested by using typeof-- directly
NPM commands
npm install typeof-in --save
npm test
About the environment
This library was created with function.name in mind, therefore, you will have better performances with recent version of your favorites browsers and servers (Node.JS V4...).
Be careful with [IE6-8], some objects has different behaviors (like arguments or document object).
Sorry for the capital letter in the In() method. IE6 doesn't like when I use the in keyword in my code...
you should consider using lodash before TypeOf-In, especially for simple cases
Finally, I'm open to any suggestions