Skip to content

Instantly share code, notes, and snippets.

@starandtina
Forked from kirilloid/type.js
Created August 23, 2017 13:35
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 starandtina/1c930ddf53ceb864d8c4a1d49e8b2490 to your computer and use it in GitHub Desktop.
Save starandtina/1c930ddf53ceb864d8c4a1d49e8b2490 to your computer and use it in GitHub Desktop.
getType
function getType (value) {
let type = typeof value;
if (type === 'object') {
return value ? Object.prototype.toString.call(value).slice(8, -1) : 'null';
}
return type;
}
[NaN, 0, 1, Infinity, // numbers
null, undefined, false, 'str', // other primitives
new String('a'), new Boolean(true), // wrapped primitives
{}, [], new Map, new WeakSet(), // containers
/regex/, new Date(), // other custom objects
window, navigator, // native objects
function () {}, (() => {}), atob,// functions
Symbol(), // symbol
{[Symbol.toStringTag]: 'Custom'} // @@ToStringTag
].map(getType);
// and the result is
[
"number", "number", "number", "number", // numbers
"null", "undefined", "boolean", "string",// other primitives
"String", "Boolean", // wrapped primitives - notice the uppercase
"Object", "Array", "Map", "WeakSet", // containers
"RegExp", "Date", // other custom objects
"Window", "Navigator", // native objects
"function", "function", "function", // functions
"symbol", // symbol
"Custom", // @@ToStringTag
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment