Skip to content

Instantly share code, notes, and snippets.

@monkeym4ster
Forked from pyrobot/beautify.coffee
Created April 13, 2016 16:02
Show Gist options
  • Save monkeym4ster/f586a1f3aa701bb7e5e8bc568ec313ec to your computer and use it in GitHub Desktop.
Save monkeym4ster/f586a1f3aa701bb7e5e8bc568ec313ec 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