Last active
April 7, 2021 23:31
-
-
Save westc/ce3ec0c450389897be6d02e8d3e49d6e to your computer and use it in GitHub Desktop.
An extended version of the typeOf() function which will either return the name of the class, "null", "undefined" or a boolean indicating if one of the passed type names matches the type of the 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 typeOf(value) { | |
for ( | |
var test, | |
argv = arguments, | |
argc = argv.length, | |
i = argc, | |
typeName = value == undefined | |
? value === undefined | |
? 'undefined' | |
: 'null' | |
: {}.toString.call(value).slice(8, -1); | |
--i > 0 && ((typeof(test = argv[i]) == 'string' || typeOf(test) != 'RegExp') ? typeName != test : !test.test(typeName)); | |
); | |
return argc > 1 ? !!i : typeName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Console tests where only the
value
is supplied:Console tests where the
value
is tested against strings and regular expressions: