UNPKG

6.22 kBMarkdownView Raw
1# JS Beautifier
2
3...or, more specifically, all of the code powering
4[jsbeautifier.org](http://jsbeautifier.org/).
5
6This little beautifier will reformat and reindent bookmarklets, ugly
7JavaScript, unpack scripts packed by Dean Edward’s popular packer,
8as well as deobfuscate scripts processed by
9[javascriptobfuscator.com](http://javascriptobfuscator.com/).
10
11## Usage
12
13To beautify from the command-line you can use the provided Python script/library or [npm](http://npmjs.org/) package.
14
15### Python
16
17`./js-beautify file.js` beautifies a file, output goes to `stdout`.
18
19To use `jsbeautifier` as a library is simple:
20
21``` python
22import jsbeautifier
23res = jsbeautifier.beautify('your javascript string')
24res = jsbeautifier.beautify_file('some_file.js')
25```
26
27...or, to specify some options:
28
29``` python
30opts = jsbeautifier.default_options()
31opts.indent_size = 2
32res = jsbeautifier.beautify('some javascript', opts)
33```
34
35### JavaScript
36
37As 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.
38
39```bash
40$ npm -g install js-beautify
41$ js-beautify foo.js
42```
43
44You can also use `js-beautify` as a `node` library (install locally, the `npm` default):
45
46```bash
47$ npm install js-beautify
48```
49
50```js
51var beautify = require('js-beautify').js_beautify,
52 fs = require('fs');
53
54fs.readFile('foo.js', 'utf8', function (err, data) {
55 if (err) {
56 throw err;
57 }
58 console.log(beautify(data, { indent_size: 2 }));
59});
60```
61
62### Options
63
64These are the command-line flags for both Python and JS scripts:
65
66```text
67CLI Options:
68 -f, --file Input file(s) (Pass '-' for stdin). These can also be passed directly.
69 -r, --replace Write output in-place, replacing input
70 -o, --outfile Write output to file (default stdout)
71 --config Path to config file
72 --type [js|css|html] ["js"]
73 -q, --quiet Suppress logging to stdout
74 -v, --version Show the version
75 -h, --help Show this help
76
77Beautifier Options:
78 -s, --indent-size Indentation size [4]
79 -c, --indent-char Indentation character [" "]
80 -l, --indent-level Initial indentation level [0]
81 -t, --indent-with-tabs Indent with tabs, overrides -s and -c
82 -p, --preserve-newlines Preserve existing line-breaks (--no-preserve-newlines disables)
83 -m, --max-preserve-newlines Maximum number of line-breaks to be preserved in one chunk [10]
84 -j, --jslint-happy Enable jslint-stricter mode
85 -b, --brace-style [collapse|expand|end-expand|expand-strict] ["collapse"]
86 -B, --break-chained-methods Break chained method calls across subsequent lines
87 -k, --keep-array-indentation Preserve array indentation
88 -x, --unescape-strings Decode printable characters encoded in xNN notation
89 -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]
90 --good-stuff Warm the cockles of Crockford's heart
91```
92
93These largely correspond to the underscored option keys for both library interfaces, which have these defaults:
94
95```json
96{
97 "indent_size": 4,
98 "indent_char": " ",
99 "indent_level": 0,
100 "indent_with_tabs": false,
101 "preserve_newlines": true,
102 "max_preserve_newlines": 10,
103 "jslint_happy": false,
104 "brace_style": "collapse",
105 "keep_array_indentation": false,
106 "keep_function_indentation": false,
107 "space_before_conditional": true,
108 "break_chained_methods": false,
109 "eval_code": false,
110 "unescape_strings": false,
111 "wrap_line_length": 0
112}
113```
114
115In addition to CLI arguments, you may pass config to the JS executable via:
116
117 * any `jsbeautify_`-prefixed environment variables
118 * a `JSON`-formatted file indicated by the `--config` parameter
119 * a `.jsbeautifyrc` file containing `JSON` data at any level of the filesystem above `$PWD`
120
121Configuration sources provided earlier in this stack will override later ones.
122
123You 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!
124
125#### CSS & HTML
126
127In addition to the `js-beautify` executable, `css-beautify` and `html-beautify` are also provided as an easy interface into those scripts. Alternatively, `js-beautify --css` or `js-beautify --html` will accomplish the same thing, respectively.
128
129```js
130// Programmatic access
131var beautify_js = require('js-beautify'); // also available under "js" export
132var beautify_css = require('js-beautify').css;
133var beautify_html = require('js-beautify').html;
134
135// All methods accept two arguments, the string to be beautified, and an options object.
136```
137
138The CSS & HTML beautifiers are much simpler in scope, and possess far fewer options.
139
140```text
141CSS Beautifier Options:
142 -s, --indent-size Indentation size [4]
143 -c, --indent-char Indentation character [" "]
144
145HTML Beautifier Options:
146 -s, --indent-size Indentation size [4]
147 -c, --indent-char Indentation character [" "]
148 -b, --brace-style [collapse|expand|end-expand] ["collapse"]
149 -S, --indent-scripts [keep|separate|normal] ["normal"]
150 -W, --max-char Maximum characters per line (0 disables) [250]
151 -U, --unformatted List of tags (defaults to inline) that should not be reformatted
152```
153
154## License
155
156You are free to use this in any way you want, in case you find this
157useful or working for you. (MIT)
158
159## Credits
160
161Written by Einar Lielmanis, <einar@jsbeautifier.org>
162Python version flourished by Stefano Sanfilippo <a.little.coder@gmail.com>
163
164Thanks to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave
165Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull,
166Mathias Bynens, Vittorio Gambaletta and others.