Skip to content

Instantly share code, notes, and snippets.

@draeton
Created September 15, 2011 03:55
Show Gist options
  • Save draeton/1218489 to your computer and use it in GitHub Desktop.
Save draeton/1218489 to your computer and use it in GitHub Desktop.
isType methods
var o = {};
function getIsType(type) {
return function (o) {
return /* undefined */ typeof o === type ||
/* null */ o === null && type === "null" ||
Object.prototype.toString.call(o).match(/\s+(\w+)/)[1].toLowerCase() === type;
};
}
var types = "String Number Date Object Array RegExp Function Null Undefined Boolean".split(" ");
for (var i in types) {
if (types.hasOwnProperty(i)) {
o["is" + types[i]] = getIsType(types[i].toLowerCase());
}
}
// o.isString("") === true
// o.isNull(null) === true
// o.isUndefined() === true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment