Description
Sensible / unsurprising JavaScript type detection and comparison using a combination of ({}).toString and constructors.
Built in objects / primitives
obj | Type.of(obj) | Type.is(...) === true |
---|---|---|
{ x : 2 } |
Object | Type.is(obj, Object) |
function () {} |
Function | Type.is(obj, Function) |
[1, 2, 3] |
Array | Type.is(obj, Array) |
"barf" |
String | Type.is(obj, String) |
true |
Boolean | Type.is(obj, Boolean) |
10 |
Number | Type.is(obj, Number) |
new Date() |
Date | Type.is(obj, Date) |
/abc/ |
RegExp | Type.is(obj, RegExp) |
new Error("barf!") |
Error | Type.is(obj, Error) |
Objects created via new
{ thisname = name;}Personprototype { return thisname + " just barfed!";}; var ralph = 'Ralph'; Type; // [Function: Person]Type; // trueType; // falseType; // trueType; // true
Latest Version
3.4.0
Installation
npm install type-of-is
or in package.json
Usage
var Type = ; Type; // returns constructor type of an objectType; // provides type as StringType; // tests whether obj is of type (constructor or String)Type; // wrapper of "obj instanceof type"Type // The top level Type export delegates to Type.of or Type.is based on argument count === Type; === Type;
More examples
var Type = ; // Type.of(arg) and Type(one_argument) return constructor of typeconsole; // [Function: String]console; // [Function: Number]console; // [Function: Object]console; // [Function: Array]console; // nullconsole; // undefinedconsole; // [Function: Boolean]console; // [Function: Function]console; // [Function: RegExp]console; // [Function: Date]console; // [Function: Error] // Type.string(arg) returns the string name of constructorconsole; // "String"console; // "Number"console; // "Object"console; // "Array"console; // "null"console; // "undefined"console; // "Boolean"console; // "Function"console; // "RegExp"console; // "Date"console; // "Error" // Type.is(object, type) and Type(object, type) tests object typeconsole; // trueconsole; // falseconsole; // trueconsole; // trueconsole; // falseconsole; // falseconsole; // trueconsole; // trueconsole; // trueconsole; // false var s = "hihihi";var Stringy = Type;var t = "hihihi";console; // trueconsole; // false // User defined objects should be instances of Objects but also can get actual constructor type { thisname = name;}Personprototype { return thisname + " just barfed!";}; var ralph = 'Ralph'; console; // [Function: Person]console; // trueconsole; // falseconsole; // trueconsole; // true // arguments is weird edge case, there's no Arguments global but typeof arguments is "arguments"// type returned is Object, but not sure what would be preferable { console; // [Function: Object]}; // other built-insconsole; // [Function: Number]console; // [Function: Number]console; // [Function: Number]console; // [Function: Object]console; // [Function: Object] // Returning constructor as type allows it to be used to create new objects i.e.var s = "s";var t = s"t";console; // "T" // Type.any(obj, [Array, Of, Types]) and Type(obj, [Array, Of, Types]) should test whether// the object is any of the passed in typesvar str = 'hihihi';console; // trueconsole; // false // multi-frame domvar iFrame = document;documentbody;var IFrameArray = windowframes0Array;var array = ; console; //falseconsole; //true;console; // Arrayconsole; // falseconsole; // true
Rationale
Try to iron over some of the surprises in JavaScript type detection
-
typeof is unreliable / surprising in multiple cases (Array -> object, null -> object, etc.)
-
constructor checking is unreliable in multi-frame dom environments
-
type comparison using strings whose string case / formatting differs from constructor names introduces unnecessary complexity
-
({}).toString returns "[object Object]" for objects created via new rather than constructor name called with new
Links
http://ecma262-5.com/ELS5_HTML.htm
http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
http://skilldrick.co.uk/2011/09/understanding-typeof-instanceof-and-constructor-in-javascript/
http://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/
http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
http://www.2ality.com/2011/11/improving-typeof.html
TODO
check back on https://github.com/ariya/phantomjs/issues/11722