Created
July 18, 2010 22:42
-
-
Save devongovett/480769 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* is.js | |
* Function that returns whether an object is of a given type. | |
* Pass multiple types to test whether an object is of any of the types | |
* Originally by Dmitry Baranovskiy | |
* Modified by Devon Govett to support multiple types | |
*/ | |
function is(o /*, type, type */) { | |
var types = Array.prototype.slice.call(arguments, 1); | |
for(var i = 0, len = types.length; i < len; i++) { | |
var type = String(types[i]).toLowerCase(); | |
if((type == "null" && o === null) || | |
(type == typeof o) || | |
(type == "object" && o === Object(o)) || | |
(type == "array" && Array.isArray && Array.isArray(o)) || | |
Object.prototype.toString.call(o).slice(8, -1).toLowerCase() == type) return true; | |
} | |
return false; | |
} | |
//Examples | |
is("test", "array", "string"); //true | |
is(123, "number", "string"); //true | |
is([], "object"); //true | |
is([], "array"); //true | |
is(new String("test"), "string"); //true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment