Created
September 15, 2011 03:55
-
-
Save draeton/1218489 to your computer and use it in GitHub Desktop.
isType methods
This file contains 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
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