bchecketts/js-beautify

为与Composer一起使用而fork自einars/js-beautify

安装数量: 28,746

依赖关系: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分叉: 1,374

语言:JavaScript

dev-master 2013-12-02 18:49 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:12:32 UTC


README

Build Status NPM version

这个小美化器可以将书签脚本、丑陋的JavaScript、由Dean Edward流行的打包器打包的脚本以及由javascriptobfuscator.com处理的脚本重新格式化和重新缩进。

用法

您可以使用JS Beautifier在您的网络浏览器中美化JavaScript,或者使用node.js或python在命令行中。

网络浏览器

打开 jsbeautifier.org。选项可通过用户界面访问。

Python

要使用python美化

$ pip install jsbeautifier
$ js-beautify file.js

美化后的输出将发送到 stdout

要使用jsbeautifier作为库,很简单

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

...或者,要指定一些选项

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

JavaScript

作为Python脚本的替代方案,您也可以安装NPM包 js-beautify。全局安装后,它提供了一个可执行的js-beautify脚本。与Python脚本一样,除非另外配置,否则美化后的结果将发送到stdout

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

您还可以将js-beautify用作node库(本地安装,npm默认设置)

$ 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 }));
});

选项

这些是Python和JS脚本的命令行标志

CLI Options:
  -f, --file       Input file(s) (Pass '-' for stdin)
  -r, --replace    Write output in-place, replacing input
  -o, --outfile    Write output to file (default stdout)
  --config         Path to config file
  --type           [js|css|html] ["js"]
  -q, --quiet      Suppress logging to stdout
  -h, --help       Show this help
  -v, --version    Show the version

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 line-breaks (--no-preserve-newlines disables)
  -m, --max-preserve-newlines   Number of line-breaks to be preserved in one chunk [10]
  -P, --space-in-paren          Add padding spaces within paren, ie. f( a, b )
  -j, --jslint-happy            Enable jslint-stricter mode
  -b, --brace-style             [collapse|expand|end-expand] ["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
  -w, --wrap-line-length        Wrap lines at next opportunity after N characters [0]
  -X, --e4x                     Pass E4X xml literals through untouched
  --good-stuff                  Warm the cockles of Crockford's heart

这些在很大程度上对应于库接口下划线选项键,默认值如下

{
    "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,
    "wrap_line_length": 0
}

除了CLI参数外,您还可以通过以下方式将配置传递给JS可执行文件

  • 任何以jsbeautify_为前缀的环境变量
  • --config参数指定的JSON格式文件
  • 包含JSON数据的任何级别的.jsbeautifyrc文件,在$PWD以上的文件系统中

此堆栈中较早提供的配置源将覆盖较晚的配置源。

您可能会注意到,CLI选项和默认哈希不完全是相关的。历史上,Python和JS API没有100%相同。例如,space_before_conditional目前仅为JS专用,无法从CLI脚本访问。还有一些其他额外的情况使我们无法100%兼容API。欢迎提交补丁!

CSS & HTML

除了js-beautify可执行文件外,还提供了css-beautifyhtml-beautify作为进入这些脚本的简单接口。或者,分别使用js-beautify --cssjs-beautify --html可以达到相同的效果。

// Programmatic access
var beautify_js = require('js-beautify'); // also available under "js" export
var beautify_css = require('js-beautify').css;
var beautify_html = require('js-beautify').html;

// All methods accept two arguments, the string to be beautified, and an options object.

CSS和HTML美化器的范围更简单,选项更少。

CSS Beautifier Options:
  -s, --indent-size             Indentation size [4]
  -c, --indent-char             Indentation character [" "]

HTML Beautifier Options:
  -I, --indent-inner-html       Indent <head> and <body> sections. Default is false.
  -s, --indent-size             Indentation size [4]
  -c, --indent-char             Indentation character [" "]
  -b, --brace-style             [collapse|expand|end-expand] ["collapse"]
  -S, --indent-scripts          [keep|separate|normal] ["normal"]
  -w, --wrap-line-length        Maximum characters per line (0 disables) [250]
  -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]
  -U, --unformatted             List of tags (defaults to inline) that should not be reformatted

许可证

如果您发现这很有用或适合您,您可以以任何方式使用此软件,但您必须保留版权声明和许可证。(MIT)

致谢

还要感谢Jason Diamond、Patrick Hof、Nochum Sossonko、Andreas Schneider、Dave Vasilevsky、Vital Batmanov、Ron Baldwin、Gabriel Harrison、Chris J. Shull、Mathias Bynens、Vittorio Gambaletta以及其他人。js-beautify@1.3.4