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
function Person (name) {
this.name = name;
}
Person.prototype.barf = function () {
return this.name + " just barfed!";
};
var ralph = new Person('Ralph');
Type.of(ralph);
Type.is(ralph, Person);
Type.is(ralph, Object);
Type.instance(ralph, Person);
Type.instance(ralph, Object);
Latest Version
3.4.0
Installation
npm install type-of-is
or in package.json
{
...
"dependencies": {
"type-of-is": "~3.4.0"
}
}
Usage
var Type = require('type-of-is');
Type.of(obj);
Type.string(obj);
Type.is(obj, type);
Type.instance(obj, type);
Type.extension(SomeCoffeeScriptClassThatExtendsBarf, SomeCoffeeScriptClassNamedBarf)
Type(obj) === Type.of(obj);
Type(obj, type) === Type.is(obj, type);
More examples
var Type = require('type-of-is');
console.log(Type.of('hi there ok'));
console.log(Type.of(342));
console.log(Type.of({}));
console.log(Type.of([1, 2, 3]));
console.log(Type.of(null));
console.log(Type.of(undefined));
console.log(Type(true));
console.log(Type(function () {}));
console.log(Type(/abcd/));
console.log(Type(new Date()));
console.log(Type(new Error()));
console.log(Type.string('hi there ok'));
console.log(Type.string(342));
console.log(Type.string({}));
console.log(Type.string([1, 2, 3]));
console.log(Type.string(null));
console.log(Type.string(undefined));
console.log(Type.string(true));
console.log(Type.string(function () {}));
console.log(Type.string(/abcd/));
console.log(Type.string(new Date()));
console.log(Type.string(new Error()));
console.log(Type.is(true, Boolean));
console.log(Type.is("1231", Number));
console.log(Type.is("1231", String));
console.log(Type.is("1231", "String"));
console.log(Type.is("1231", Object));
console.log(Type([], Object));
console.log(Type({}, Object));
console.log(Type([], Array));
console.log(Type(new Date(), Date));
console.log(Type(new Date(), Object));
var s = "hihihi";
var Stringy = Type.of(s);
var t = new Stringy("hihihi");
console.log((s == t));
console.log((s === t));
function Person (name) {
this.name = name;
}
Person.prototype.barf = function () {
return this.name + " just barfed!";
};
var ralph = new Person('Ralph');
console.log(Type.of(ralph));
console.log(Type.is(ralph, Person));
console.log(Type.is(ralph, Object));
console.log(Type.instance(ralph, Person));
console.log(Type.instance(ralph, Object));
(function () {
console.log(Type.of(arguments));
})();
console.log(Type.of(Infinity));
console.log(Type.of(-Infinity));
console.log(Type.of(NaN));
console.log(Type.of(Math));
console.log(Type.of(JSON));
var s = "s";
var t = new Type.of(s)("t");
console.log(t.toUpperCase());
var str = 'hihihi';
console.log(Type.any(str, [String, Number, Array]));
console.log(Type(str, [Array, RegExp]));
var iFrame = document.createElement('IFRAME');
document.body.appendChild(iFrame);
var IFrameArray = window.frames[0].Array;
var array = new IFrameArray();
console.log(array instanceof Array);
console.log(array instanceof IFrameArray);
console.log(Type.of(array));
console.log(Type.is(array, Array));
console.log(Type.is(array, "Array"));
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
#Build status