Last active
November 18, 2017 16:33
-
-
Save razouck/b1c111f9d904452b11ff0b1a8d93fa58 to your computer and use it in GitHub Desktop.
Returns the type/tag of a given value.
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
function tagof (value) { | |
return value != value ? 'NaN' : | |
Object.prototype.toString.call(value) | |
.match(/\[object (\w+)\]/)[1] | |
.toLowerCase(); | |
} |
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
[ | |
[], | |
false, | |
new Date(), | |
Error(), | |
function () {}, | |
1 / 'zero', | |
null, | |
0, | |
{}, | |
RegExp(/a/), | |
'a', | |
Symbol(), | |
undefined | |
] | |
.forEach(value => console.log(tagof(value))); | |
// That's all there is to it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment