Beautifier for javascript
Go to file
Daniel Stockman dbe3da8091 0.3.5
2013-03-14 19:39:25 -07:00
attic It's official: python/jsbeautifier.py 2011-03-27 23:32:08 +03:00
config Pass through new wrap_line_length option, with 'unlimited' default (0). 2013-03-13 11:23:39 -07:00
php Fixed unescape_strings bug introduced along \uNNNN unescaping. 2012-06-25 00:09:14 +02:00
python Enable configurable line wrapping 2013-03-12 15:27:34 -07:00
tests Reduce the cases where output needs trimming by indenting tokens when printed. 2013-03-14 17:38:05 -07:00
third-party Move jquery to third-party/ 2013-02-07 01:29:21 +02:00
unpackers Fix possible str.replace freeze when unpacking packer 2013-02-25 15:29:09 +02:00
.gitignore Merge branch 'master' into node-package 2013-02-28 09:52:20 -08:00
.npmignore Remove README.md from .npmignore 2012-07-16 10:58:15 -07:00
beautify-css.js fix #168 beautify css breaks non string data-urls 2013-01-15 11:29:47 +04:00
beautify-html.js Bump up the default style_html line length a notch 2013-02-06 23:38:45 +02:00
beautify.js Defaulting print_newline parameters to false 2013-03-14 17:43:38 -07:00
cli.js Move cli procedural calls into exports.interpret, calling immediately if directly executed. 2013-03-13 12:01:31 -07:00
favicon.png Who needs .ico anyway. png ftw 2012-05-31 14:31:43 +03:00
index.html Add @bitwiseman to jsb-footer 2013-03-13 11:52:55 +02:00
index.js Bridge between beautify.js and direct node_module require. 2012-07-13 16:53:32 -07:00
license.txt It's official: python/jsbeautifier.py 2011-03-27 23:32:08 +03:00
Makefile Extract TK_COMMA from TK_OPERATOR, fix #125 2012-07-05 18:34:35 +03:00
package.json 0.3.5 2013-03-14 19:39:25 -07:00
README.md Fix plaintext code block formatting 2012-12-06 14:02:05 -08:00

JS Beautifier

...or, more specifically, all of the code powering jsbeautifier.org.

This little beautifier will reformat and reindent bookmarklets, ugly JavaScript, unpack scripts packed by Dean Edwards popular packer, as well as deobfuscate scripts processed by javascriptobfuscator.com.

Usage

To beautify from the command-line you can use the provided Python script/library or npm package.

Python

./js-beautify file.js beautifies a file, output goes to stdout.

To use jsbeautifier as a library is simple:

import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js')

...or, to specify some options:

opts = jsbeautifier.default_options()
opts.indent_size = 2
res = jsbeautifier.beautify('some javascript', opts)

JavaScript

As an alternative to the Python script, you may install the NPM package js-beautify. When installed globally, it provides an executable js-beautify script. As with the Python script, the beautified result is sent to stdout unless otherwise configured.

$ npm -g install js-beautify
$ js-beautify foo.js

You can also use js-beautify as a node library (install locally, the npm default):

$ npm install js-beautify
var beautify = require('js-beautify').js_beautify,
    fs = require('fs');

fs.readFile('foo.js', 'utf8', function (err, data) {
    if (err) {
        throw err;
    }
    console.log(beautify(data, { indent_size: 2 }));
});

Options

These are the command-line flags for both Python and JS scripts:

CLI Options:
  -f, --file                    Input file(s) (Pass '-' for stdin). These can also be passed directly.
  -r, --replace                 Write output in-place, replacing input
  -o, --outfile                 Write output to file (default stdout)
  --config                      Path to config file
  -v, --version                 Show the version
  -h, --help                    Show this help

Beautifier Options:
  -s, --indent-size             Indentation size [4]
  -c, --indent-char             Indentation character [" "]
  -l, --indent-level            Initial indentation level [0]
  -t, --indent-with-tabs        Indent with tabs, overrides -s and -c
  -p, --preserve-newlines       Preserve existing line-breaks (--no-preserve-newlines disables)
  -m, --max-preserve-newlines   Maximum number of line-breaks to be preserved in one chunk [10]
  -j, --jslint-happy            Enable jslint-stricter mode
  -b, --brace-style             [collapse|expand|end-expand|expand-strict] ["collapse"]
  -B, --break-chained-methods   Break chained method calls across subsequent lines
  -k, --keep-array-indentation  Preserve array indentation
  -x, --unescape-strings        Decode printable characters encoded in xNN notation
  -g, --good-stuff              Warm the cockles of Crockford's heart

These largely correspond to the underscored option keys for both library interfaces, which have these defaults:

{
    "indent_size": 4,
    "indent_char": " ",
    "indent_level": 0,
    "indent_with_tabs": false,
    "preserve_newlines": true,
    "max_preserve_newlines": 10,
    "jslint_happy": false,
    "brace_style": "collapse",
    "keep_array_indentation": false,
    "keep_function_indentation": false,
    "space_before_conditional": true,
    "break_chained_methods": false,
    "eval_code": false,
    "unescape_strings": false
}

In addition to CLI arguments, you may pass config to the JS executable via:

  • any jsbeautify_-prefixed environment variables
  • a JSON-formatted file indicated by the --config parameter
  • a .jsbeautifyrc file containing JSON data at any level of the filesystem above $PWD

Configuration sources provided earlier in this stack will override later ones.

You might notice that the CLI options and defaults hash aren't 100% correlated. Historically, the Python and JS APIs have not been 100% identical. For example, space_before_conditional is currently JS-only, and not addressable from the CLI script. There are a few other additional cases keeping us from 100% API-compatibility. Patches welcome!

License

You are free to use this in any way you want, in case you find this useful or working for you. (MIT)

Credits

Written by Einar Lielmanis, einar@jsbeautifier.org Python version flourished by Stefano Sanfilippo a.little.coder@gmail.com

Thanks to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull, Mathias Bynens, Vittorio Gambaletta and others.