Skip to content

Instantly share code, notes, and snippets.

@wvl
Created February 10, 2011 00:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvl/819640 to your computer and use it in GitHub Desktop.
Save wvl/819640 to your computer and use it in GitHub Desktop.
Get a repeatable, readable version of the source of an object
{inspect} = require 'util'
source_for = (obj, level=[]) ->
source = ""
Object.keys(obj).sort().forEach (k) ->
indent = level.join('')
if typeof obj[k] == 'function'
source += indent + k + ": " +Object.getOwnPropertyDescriptor(obj, k).value.toString() + "\n"
else if typeof obj[k] == 'object'
source += indent + k + ":\n" + source_for(obj[k], level.concat(' '))
else
source += indent + k + ": " + inspect(obj[k]) + "\n"
return source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment