Last active
September 25, 2015 23:08
-
-
Save Sija/1000205 to your computer and use it in GitHub Desktop.
Object.type
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
# Execute function immediately | |
typeOf = do -> | |
classToType = {} | |
for name in 'Boolean Number String Function Array Date RegExp Undefined Null'.split ' ' | |
classToType["[object #{name}]"] = name.toLowerCase() | |
# Return a function | |
(obj) -> | |
strType = Object::toString.call obj | |
classToType[strType] or 'object' |
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
typeOf = (value) -> | |
type = typeof value | |
if type is 'object' or type is 'function' | |
return 'null' if value is null | |
return 'array' if value instanceof Array | |
type = switch value.constructor | |
when String then 'string' | |
when Number then 'number' | |
when RegExp then 'regexp' | |
when Function then 'function' | |
when Date then 'date' | |
else type | |
type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment