Return to Snippet

Revision: 44573
at September 17, 2011 03:56 by wizard04


Updated Code
/* Robust typeof v3.0
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 Unported License
 * http://creativecommons.org/licenses/by/3.0/
 *
 * Author: Andy Harrison, http://dragonzreef.com/
 * Date: 16 September 2011
 */

//based on the brilliant solution of using Object.prototype.toString() from
// http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/

function typeOf(o)
{
	var undefined;
	if(o === undefined) return "undefined";
	if(o === null) return "null";
	return Object.prototype.toString.call(o).replace(/\[object (.*?)\]/, "$1");
}

function isUndefined(o){ return typeOf(o) == "undefined"; }
function isNull(o){ return typeOf(o) == "null"; }

function isBoolean(o){ return typeOf(o) == "Boolean"; }
function isNumber(o){ return typeOf(o) == "Number"; }
function isString(o){ return typeOf(o) == "String"; }
function isRegExp(o){ return typeOf(o) == "RegExp"; }
function isDate(o){ return typeOf(o) == "Date"; }
function isArray(o){ return typeOf(o) == "Array"; }
function isFunction(o){ return typeOf(o) == "Function"; }
function isObject(o){ return typeOf(o) == "Object"; }

Object.prototype.isBoolean = function(){ return typeOf(this) == "Boolean"; };
Object.prototype.isNumber = function(){ return typeOf(this) == "Number"; };
Object.prototype.isString = function(){ return typeOf(this) == "String"; };
Object.prototype.isRegExp = function(){ return typeOf(this) == "RegExp"; };
Object.prototype.isDate = function(){ return typeOf(this) == "Date"; };
Object.prototype.isArray = function(){ return typeOf(this) == "Array"; };
Object.prototype.isFunction = function(){ return typeOf(this) == "Function"; };
Object.prototype.isObject = function(){ return typeOf(this) == "Object"; };

if(!Array.isArray) Array.isArray = isArray;

/*
//test all data types
//IE 8 and older give "Object" as the type of an HTMLDivElement
var u;
function f(){}
var testValues = [
	{value: u, code: "= undefined", expected: "undefined"},
	{value: null, code: "= null", expected: "null"},
	{value: true, code: "= true", expected: "Boolean"},
	{value: (new Boolean), code: "= new Boolean", expected: "Boolean"},
	{value: 1, code: "= 1", expected: "Number"},
	{value: (new Number), code: "= new Number", expected: "Number"},
	{value: "s", code: "= \"s\"", expected: "String"},
	{value: (new String), code: "= new String", expected: "String"},
	{value: /r/, code: "= /r/", expected: "RegExp"},
	{value: (new RegExp), code: "= new RegEx)", expected: "RegExp"},
	{value: (new Date), code: "= new Date", expected: "Date"},
	{value: [], code: "= []", expected: "Array"},
	{value: [0,1], code: "= [0,1]", expected: "Array"},
	{value: (new Array), code: "= new Array", expected: "Array"},
	{value: f, code: "function f(){}", expected: "Function"},
	{value: function(){}, code: "= function(){}", expected: "Function"},
	{value: (new Function), code: "= new Function", expected: "Function"},
	{value: {}, code: "= {}", expected: "Object"},
	{value: (new Object), code: "= new Object", expected: "Object"},
	{value: (new f), code: "= new f", expected: "Object"},
	{value: document.createElement("div"), code: "= document.createElement(\"div\")", expected: "HTMLDivElement"}
];

var t = "<table><tr><th>Value</th><th>Code</th><th>Expected</th><th>Result</th></tr>";
var c;
for(var i=0; i<testValues.length; i++)
{
	c = " style=\"color:green\"";
	if(testValues[i].expected != typeOf(testValues[i].value)) c = " style=\"color:red\"";
	t += "<tr><td>"+testValues[i].value+"</td><td>"+testValues[i].code+"</td><td>"+testValues[i].expected+"</td><td"+c+">"+
		typeOf(testValues[i].value)+"</td></tr>";
}
t += "</table>";
document.getElementById("log").innerHTML += t;
*/

Revision: 44572
at April 23, 2011 04:23 by wizard04


Updated Code
//got this from http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
function typeOf(o)
{
	var undefined;
	if(o === undefined) return "undefined";
	if(o === null) return "null";
	return Object.prototype.toString.call(o).replace(/\[object (.*?)\]/, "$1");
}

function isUndefined(o){ return typeOf(o) == "undefined"; }
function isNull(o){ return typeOf(o) == "null"; }

function isBoolean(o){ return typeOf(o) == "Boolean"; }
function isNumber(o){ return typeOf(o) == "Number"; }
function isString(o){ return typeOf(o) == "String"; }
function isRegExp(o){ return typeOf(o) == "RegExp"; }
function isDate(o){ return typeOf(o) == "Date"; }
function isArray(o){ return typeOf(o) == "Array"; }
function isFunction(o){ return typeOf(o) == "Function"; }
function isObject(o){ return typeOf(o) == "Object"; }

Object.prototype.isBoolean = function(){ return typeOf(this) == "Boolean"; };
Object.prototype.isNumber = function(){ return typeOf(this) == "Number"; };
Object.prototype.isString = function(){ return typeOf(this) == "String"; };
Object.prototype.isRegExp = function(){ return typeOf(this) == "RegExp"; };
Object.prototype.isDate = function(){ return typeOf(this) == "Date"; };
Object.prototype.isArray = function(){ return typeOf(this) == "Array"; };
Object.prototype.isFunction = function(){ return typeOf(this) == "Function"; };
Object.prototype.isObject = function(){ return typeOf(this) == "Object"; };

/*
//test all data types
//IE 8 and older give "Object" as the type of an HTMLDivElement
var u;
function f(){}
var testValues = [
	{value: u, code: "= undefined", expected: "undefined"},
	{value: null, code: "= null", expected: "null"},
	{value: true, code: "= true", expected: "Boolean"},
	{value: (new Boolean), code: "= new Boolean", expected: "Boolean"},
	{value: 1, code: "= 1", expected: "Number"},
	{value: (new Number), code: "= new Number", expected: "Number"},
	{value: "s", code: "= \"s\"", expected: "String"},
	{value: (new String), code: "= new String", expected: "String"},
	{value: /r/, code: "= /r/", expected: "RegExp"},
	{value: (new RegExp), code: "= new RegEx)", expected: "RegExp"},
	{value: (new Date), code: "= new Date", expected: "Date"},
	{value: [], code: "= []", expected: "Array"},
	{value: [0,1], code: "= [0,1]", expected: "Array"},
	{value: (new Array), code: "= new Array", expected: "Array"},
	{value: f, code: "function f(){}", expected: "Function"},
	{value: function(){}, code: "= function(){}", expected: "Function"},
	{value: (new Function), code: "= new Function", expected: "Function"},
	{value: {}, code: "= {}", expected: "Object"},
	{value: (new Object), code: "= new Object", expected: "Object"},
	{value: (new f), code: "= new f", expected: "Object"},
	{value: document.createElement("div"), code: "= document.createElement(\"div\")", expected: "HTMLDivElement"}
];

var t = "<table><tr><th>Value</th><th>Code</th><th>Expected</th><th>Result</th></tr>";
var c;
for(var i=0; i<testValues.length; i++)
{
	c = " style=\"color:green\"";
	if(testValues[i].expected != typeOf(testValues[i].value)) c = " style=\"color:red\"";
	t += "<tr><td>"+testValues[i].value+"</td><td>"+testValues[i].code+"</td><td>"+testValues[i].expected+"</td><td"+c+">"+
		typeOf(testValues[i].value)+"</td></tr>";
}
t += "</table>";
document.getElementById("log").innerHTML += t;
*/

Revision: 44571
at April 14, 2011 05:30 by wizard04


Initial Code
//got this from http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
function typeOf(o)
{
	var undefined;
	if(o === undefined) return "undefined";
	if(o === null) return "null";
	return Object.prototype.toString.call(o).replace(/\[object (.*?)\]/, "$1");
}

function isUndefined(o){ return typeOf(o) == "undefined"; }
function isNull(o){ return typeOf(o) == "null"; }
function isBoolean(o){ return typeOf(o) == "Boolean"; }
function isNumber(o){ return typeOf(o) == "Number"; }
function isString(o){ return typeOf(o) == "String"; }
function isRegExp(o){ return typeOf(o) == "RegExp"; }
function isDate(o){ return typeOf(o) == "Date"; }
function isArray(o){ return typeOf(o) == "Array"; }
function isFunction(o){ return typeOf(o) == "Function"; }
function isObject(o){ return typeOf(o) == "Object"; }

/*
//test all data types
//IE 8 and older give "Object" as the type of an HTMLDivElement
var u;
function f(){}
var testValues = [
	{value: u, code: "= undefined", expected: "undefined"},
	{value: null, code: "= null", expected: "null"},
	{value: true, code: "= true", expected: "Boolean"},
	{value: (new Boolean), code: "= new Boolean", expected: "Boolean"},
	{value: 1, code: "= 1", expected: "Number"},
	{value: (new Number), code: "= new Number", expected: "Number"},
	{value: "s", code: "= \"s\"", expected: "String"},
	{value: (new String), code: "= new String", expected: "String"},
	{value: /r/, code: "= /r/", expected: "RegExp"},
	{value: (new RegExp), code: "= new RegEx)", expected: "RegExp"},
	{value: (new Date), code: "= new Date", expected: "Date"},
	{value: [], code: "= []", expected: "Array"},
	{value: [0,1], code: "= [0,1]", expected: "Array"},
	{value: (new Array), code: "= new Array", expected: "Array"},
	{value: f, code: "function f(){}", expected: "Function"},
	{value: function(){}, code: "= function(){}", expected: "Function"},
	{value: (new Function), code: "= new Function", expected: "Function"},
	{value: {}, code: "= {}", expected: "Object"},
	{value: (new Object), code: "= new Object", expected: "Object"},
	{value: (new f), code: "= new f", expected: "Object"},
	{value: document.createElement("div"), code: "= document.createElement(\"div\")", expected: "HTMLDivElement"}
];

var t = "<table><tr><th>Value</th><th>Code</th><th>Expected</th><th>Result</th></tr>";
var c;
for(var i=0; i<testValues.length; i++)
{
	c = " style=\"color:green\"";
	if(testValues[i].expected != typeOf(testValues[i].value)) c = " style=\"color:red\"";
	t += "<tr><td>"+testValues[i].value+"</td><td>"+testValues[i].code+"</td><td>"+testValues[i].expected+"</td><td"+c+">"+
		typeOf(testValues[i].value)+"</td></tr>";
}
t += "</table>";
document.body.innerHTML += t;
*/

Initial URL
http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/

Initial Description
Brilliant solution using Object.prototype.toString()

Test cases included (commented out)

Initial Title
typeOf - simple yet robust

Initial Tags
javascript

Initial Language
JavaScript