Last active
December 23, 2015 08:29
-
-
Save klaascuvelier/6607961 to your computer and use it in GitHub Desktop.
Type check for JavaScript objects.
Based on code from @jonbretman's slides from JsConf (https://speakerdeck.com/jonbretman/ask-not-what-javascript-can-do-for-you, slide 25) Fix for non-isNaN types.
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
var toString = Object.prototype.toString, | |
regex = /\[object (.*?)\]/, | |
type = function (o) { | |
var match, typeMatch; | |
// Special case for DOM elements | |
if (o && o.nodeType === 1) { | |
return 'element'; | |
} | |
match = toString.call(o).match(regex); | |
typeMatch = match[1].toLowerCase(); | |
// Special case for NaN | |
if (typeMatch === 'number') { | |
return isNaN(o) ? 'nan' : typeMatch; | |
} else { | |
return typeMatch; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment