Created
December 9, 2010 11:17
-
-
Save jacob414/734620 to your computer and use it in GitHub Desktop.
Quick and dirty Javascript repr() function using CoffeeScript
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
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 _.isArray o | |
'[' + [''+repr(e, depth + 1, max) for e in o] + ']' | |
else | |
'{' + [''+k+':'+repr(o[k], depth + 1, max) for k in _.keys(o)] + '}' | |
when 'undefined' then 'undefined' | |
else o |
when 'string' then "\"#{o.replace /"/g, '\\"'}\""
Ah, nice one! Integrated.
showing \n and \r in strings would be nice.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB: Requires Underscore.js