Skip to content

Instantly share code, notes, and snippets.

@raphamorim
Created January 26, 2016 22:48
Show Gist options
  • Save raphamorim/f1ba6936f247fd3bcd5f to your computer and use it in GitHub Desktop.
Save raphamorim/f1ba6936f247fd3bcd5f to your computer and use it in GitHub Desktop.
// From QUnit.js
function objectType( obj ) {
if ( typeof obj === "undefined" ) {
return "undefined";
}
// Consider: typeof null === object
if ( obj === null ) {
return "null";
}
var match = toString.call( obj ).match( /^\[object\s(.*)\]$/ ),
type = match && match[ 1 ];
switch ( type ) {
case "Number":
if ( isNaN( obj ) ) {
return "nan";
}
return "number";
case "String":
case "Boolean":
case "Array":
case "Set":
case "Map":
case "Date":
case "RegExp":
case "Function":
case "Symbol":
return type.toLowerCase();
}
if ( typeof obj === "object" ) {
return "object";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment