views:

106

answers:

7

There is js file with a lot of code in unreadable format (all code in one line): JS file

Is there a tool to format to a "normal" view?

A: 

A quick Google gives me this. It seems there are several options available.

Brian Agnew
+1  A: 

http://www.javascriptbeautifier.com/

Jonathan Sampson
I loved this one (best results)
samuel
A: 

There's loads, but I like this one

Ben Shelock
A: 

Have a look st Best source code formatter for Javascript?

astander
A: 

Also take a look at this: http://closure-compiler.appspot.com/home; A Google tool.

Choose [Pretty print] under Formatting

Harmen
+2  A: 

Brian Agnew's link should work fine. There's also the standalone Polystyle which I can recommend (costs $15 though).

On a side note, it may be better for you to get the non-minified version of TinyMCE and work with that in the first place. Some minifiers not only remove line breaks, but change variables names and other code elements as well.

Pekka
what is the reason for formating code like that? Does it make it more efficient?
samuel
@samuel. It makes the file sizes considerably smaller which speeds up download of large js files. @Pekka, totally separate side note, but do you use Skype much? I added you if you want to chat sometime about that feature request.
Doug Neiner
Dowload size samuel. Its all about the download size.
astander
@Doug: Yes, certainly! I'm in the middle of a *huge* project right now and have little time left, but how about some time come january?
Pekka
@Pekka, sounds great! Until then I'll catch up with you here on SO. good luck with your project!
Doug Neiner
@Doug, excellent. Cheers and speak to you later!
Pekka
+1  A: 

Most JavaScript engines beautify functions. Knowing this, here is a function that can help you with your problem:

function beautify (code) {
  return new Function(code).toString(0)
    .replace(/^function\s*\w*\s*\(\s*\)\s*{?|;?}?$/g, "")
    .replace(/\n\s{4}/g, "\n").replace(/^\n/, "")
}

SpiderMonkey and Rhino can also un-beautify (minify) them if you use function.toString(-1) in case you ever need to do the opposite. I also have a function for that too:

function minify (code) {
  new Function(code).toString(-1)
    .replace(/^function\s*\w*\s*\(\s*\)\s*{?|;?}?$/g, "");
}

Edit: It seems you only need to do this for tinymce.js. You can download the TinyMCE source code as it's open source.

Eli Grey