Skip to content

Instantly share code, notes, and snippets.

@devongovett
Created July 18, 2010 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devongovett/480769 to your computer and use it in GitHub Desktop.
Save devongovett/480769 to your computer and use it in GitHub Desktop.
/*
* 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