Last active
May 22, 2021 11:39
-
-
Save ddlsmurf/570a061b820a5240c72a0f224e1aea23 to your computer and use it in GitHub Desktop.
More exhaustive version of typeof #snippet_js
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
toString = Object::toString | |
hasOwnProperty = Object::hasOwnProperty | |
propertyIsEnumerable = Object::propertyIsEnumerable | |
toStringTags = date: '[object Date]' | |
Utils = | |
### @return `undefined|null|boolean|string|symbol|number|NaN|array|arguments|date|object|function` ### | |
type: (val) -> | |
switch type = typeof val | |
when 'undefined', 'string', 'boolean', 'symbol' | |
return type | |
when 'number' | |
return (if Number.isNaN(val) || (val in [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]) then 'NaN' else type) | |
when 'object', 'function' | |
return if val == null | |
'null' | |
else if type == "object" && hasOwnProperty.call(val, 'length') && typeof val.length == 'number' && val.length > -1 && val.length % 1 == 0 | |
# beware of https://bugs.webkit.org/show_bug.cgi?id=142792 | |
if hasOwnProperty.call(val, 'callee') && !propertyIsEnumerable.call(val, 'callee') | |
'arguments' | |
else | |
'array' | |
else | |
if toString.call(val) == toStringTags.date | |
'date' | |
else | |
type | |
break # fo' linting (linter cant see that all above paths return) | |
else | |
throw Error("Unknown object kind: #{val}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment