Skip to content

Instantly share code, notes, and snippets.

@soapie
Forked from jacob414/repr.coffee
Last active December 22, 2015 02:59
Show Gist options
  • Save soapie/6407110 to your computer and use it in GitHub Desktop.
Save soapie/6407110 to your computer and use it in GitHub Desktop.
repr = (o, depth=0, max=2) ->
if depth > max
'<..>'
else
switch typeof o
when 'string' then "\"#{o.replace /"/g, '\\"'}\""
when 'function' then 'function'
when 'object'
if o is null then 'null'
if Array.isArray o
'[' + [''+repr(e, depth + 1, max) for e in o] + ']'
else
'{' + [''+k+':'+repr(o[k], depth + 1, max) for k in Object.keys(o)] + '}'
when 'undefined' then 'undefined'
else o.toString # bool or number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment