Skip to content

Instantly share code, notes, and snippets.

@pyrobot
Created November 10, 2012 04:07
Show Gist options
  • Save pyrobot/4049843 to your computer and use it in GitHub Desktop.
Save pyrobot/4049843 to your computer and use it in GitHub Desktop.
CoffeeScript beautify function
beautify: (string, indent = 0) ->
switch ch = string[0] || ''
when '' then ""
when '{' or '[' then "#{ch}\n#{Array(++indent+1).join('\t')}#{@beautify(string[1..], indent)}"
when '}' or ']' then "\n#{Array(--indent+1).join('\t')}#{ch}#{@beautify(string[1..], indent)}"
when ',' then "#{ch}\n#{Array(indent+1).join('\t')}#{@beautify(string[1..], indent)}"
when ':' then " : #{@beautify(string[1..], indent)}"
else "#{ch}#{@beautify(string[1..], indent)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment