Skip to content

Instantly share code, notes, and snippets.

@Sija
Last active September 25, 2015 23:08
Show Gist options
  • Save Sija/1000205 to your computer and use it in GitHub Desktop.
Save Sija/1000205 to your computer and use it in GitHub Desktop.
Object.type
# 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'
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