Package Exports
- pretty-js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pretty-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
PrettyJS
Beautify / pretty print JavaScript and JSON. Turn really ugly and poorly indented files into masterpieces.
Requirements
You must have an ECMAScript 5 environment. If you're in a browser or otherwise don't have convenient things like Array.prototype.forEach then you can use es5-shim to patch in the necessary functions.
This also relies on Complexion and ComplexionJS. If you plan on using this in a web browser, I strongly suggest you look into Browserify or a similar technique to bundle up JavaScript.
Installation
When you want to run pretty-js from the command line, use npm to install the script globally.
npm install -g pretty-jsIf you'd rather call the library directly, then list "pretty-js" as a dependency in your package.json file and let npm install it locally. You can do it easily with npm.
npm install --save-dev pretty-jsUsing the API
I find that code examples are very illustrative.
var code, options, prettyJs;
// Load the library
prettyJs = require('pretty-js');
// Let's format some code quickly with the defaults
code = '// some JavaScript code goes in here';
console.log(prettyJs(code)); // Displays formatted code
// Format code and specify a couple options
options = {
indent: "\t", // Switch to tabs for indentation
newline: "\r\n" // Windows-style newlines
};
console.log(prettyJs(code, options));Options
All of these options are also available when using the command-line interface. When there is a difference in the default value, that is explained for each option along with the reasoning behind the difference.
bom (boolean or null)
Always add, remove, or just preserve the BOM (byte order mark) in a file. The BOM can cause problems plus JavaScript and JSON files are expected to typically be in UTF-8.
true: Always add a BOM to the filefalse: Always remove a BOM from the filenull: Preserve a BOM if one exists
Defaults to false to help remove problems.
commentSpace (string)
Spaces to the left of single-line comments.
someFunction(); // single-line commentDefaults to " " (two spaces)
convertStrings ("double", "single", false/null)
Strings can be 'single quoted' or "double quoted". For consistency, the pretty printer will change (and properly re-escape) strings to match your preferred style.
Defaults to double so it works better with JSON.
elseNewline (boolean)
When enabled, put else and catch on a new line. When disabled, those keywords will be on the same line as the previous closing brace.
Defaults to false.
indent (string)
What to use for a single indentation level.
The eternal "spaces vs. tabs" debate comes up a lot.
Defaults to " " (four spaces).
jslint (boolean)
When truthy this uses jslint-compatible rules. For instance, this forces quoteProperties to be false. There's some other changes regarding switch blocks and punctuation as well.
Defaults to false.
newline (string)
This is the string to use for newlines.
- "\n" is for Mac, Unix and Linux
- "\r\n" is for Windows
- "\r" is the old Mac style
Defaults to "\n" (Mac/Unix/Linux style).
quoteProperties (boolean or null)
Wrap object properties in quotes or remove them.
true: Always add quotes around propertiesfalse: Always remove quotes around properties when syntactically possiblenull: Keep quoted properties quoted and unquoted properties unquoted
Defaults to false.
Running via Command Line
Did you install this script globally? (npm install -g pretty-js) If so, you now have the boundless power of the JavaScript pretty printer in a convenient command-line interface.
pretty-js [options] [filename ...]There's a lot of options and you can control all of the aspects of the pretty printer. Use pretty-js --help for a lot of information.
Reading from stdin is an option as well, thanks to processFiles. Both of the following are options for you.
cat some-file.js | pretty-js > formatted-file.js
cat some-file.js | pretty-js - > formatted-file.jsDevelopment
If you want to work on this library, you need to check out the repository and run npm install to get the dependencies.
Tests are always included. Make sure tests cover your changes. To run the current tests, just use npm test or grunt test (they will run the same test suite).
License
This software is licensed under an MIT license with an additional non-advertising clause.



