CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-js-beautify

A comprehensive code beautification library for JavaScript, CSS, and HTML with extensive formatting options and cross-platform support

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

javascript.mddocs/

JavaScript Beautification

JavaScript code formatting with comprehensive support for modern JavaScript syntax, various brace styles, and advanced formatting options. Includes support for deobfuscation and unpacking of minified code.

Core Imports

// Import JavaScript beautifier
const { js_beautify } = require('js-beautify');
const beautify = require('js-beautify'); // beautify.js is the same as js_beautify

// Or use the short alias
const { js } = require('js-beautify');

ESM:

import beautify from 'js-beautify';
// Use beautify.js() or just beautify() for JavaScript

Capabilities

JS Beautify Function

Beautifies JavaScript source code with extensive formatting control and syntax support.

/**
 * Beautify JavaScript source code
 * @param {string} js_source_text - JavaScript code to beautify
 * @param {Object} options - Formatting options (optional)
 * @param {string} [options.brace_style='collapse'] - Brace placement style: 'collapse', 'expand', 'end-expand', 'none', 'preserve-inline'
 * @param {boolean} [options.unindent_chained_methods=false] - Don't indent chained method calls
 * @param {boolean} [options.break_chained_methods=false] - Break chained method calls across subsequent lines
 * @param {boolean} [options.space_in_paren=false] - Add padding spaces within parentheses
 * @param {boolean} [options.space_in_empty_paren=false] - Add a single space inside empty parentheses
 * @param {boolean} [options.jslint_happy=false] - Enable jslint-stricter mode
 * @param {boolean} [options.space_after_anon_function=false] - Add space before anonymous function parentheses
 * @param {boolean} [options.space_after_named_function=false] - Add space before named function parentheses
 * @param {boolean} [options.keep_array_indentation=false] - Preserve array indentation
 * @param {boolean} [options.space_before_conditional=true] - Add space before conditional statements
 * @param {boolean} [options.unescape_strings=false] - Decode printable characters encoded in xNN notation
 * @param {boolean} [options.e4x=false] - Pass E4X xml literals through untouched
 * @param {boolean} [options.comma_first=false] - Put commas at the beginning of new line instead of end
 * @param {string} [options.operator_position='before-newline'] - Set operator position relative to newlines: 'before-newline', 'after-newline', 'preserve-newline'
 * @returns {string} Beautified JavaScript code
 */
function js_beautify(js_source_text, options);

/**
 * Get default options for JavaScript beautifier
 * @returns {Object} Default options object with all JavaScript-specific options
 */
js_beautify.defaultOptions();

Default Options Function

Retrieve the default configuration options for JavaScript beautification.

/**
 * Get default options for JavaScript beautifier
 * @returns {Object} Default options object with all JavaScript-specific options
 */
js_beautify.defaultOptions();

Usage Examples:

const beautify = require('js-beautify');

// Basic beautification
const uglyCode = 'function test(){var a=1;var b=2;return a+b;}';
const beautiful = beautify.js(uglyCode, { indent_size: 2 });
// Result: 
// function test() {
//   var a = 1;
//   var b = 2;
//   return a + b;
// }

// Advanced formatting with custom options
const complexCode = 'if(condition){doSomething();doSomethingElse();}else{doDefault();}';
const formatted = beautify.js(complexCode, {
  indent_size: 4,
  brace_style: 'expand',
  space_before_conditional: true
});

// Chained method formatting
const chainedCode = 'obj.method1().method2().method3();';
const chainFormatted = beautify.js(chainedCode, {
  break_chained_methods: true,
  indent_size: 2
});

Default Options

Returns the default configuration options for JavaScript beautification.

/**
 * Get default options for JavaScript beautification
 * @returns Default JSBeautifyOptions object
 */
function defaultOptions(): JSBeautifyOptions;

Usage Example:

const beautify = require('js-beautify');

// Get default options
const defaults = beautify.js.defaultOptions();
console.log(defaults.indent_size); // 4
console.log(defaults.brace_style); // 'collapse'

// Modify defaults for custom configuration
const myOptions = {
  ...defaults,
  indent_size: 2,
  space_after_anon_function: true
};

Brace Style Options

Collapse (Default)

Places opening braces on the same line as control statements.

if (condition) {
  statement;
}

Expand

Places opening braces on new lines.

if (condition)
{
  statement;
}

End-Expand

Like expand, but places closing braces on separate lines.

if (condition)
{
  statement;
}

None

Attempts to keep braces where they are.

Preserve-Inline

Preserve inline block braces and formatting (can be combined with other styles).

Operator Position Options

Before-Newline (Default)

Places operators before line breaks.

const result = value1 +
  value2 +
  value3;

After-Newline

Places operators after line breaks.

const result = value1
  + value2
  + value3;

Preserve-Newline

Preserves existing operator positioning.

Special Features

JSLint Compatibility

When jslint_happy is enabled, the formatter makes adjustments to be compatible with JSLint requirements:

  • Forces space_after_anon_function to true
  • Adjusts various formatting rules to match JSLint expectations

E4X Support

When e4x is enabled, E4X (ECMAScript for XML) literals are passed through untouched, preserving their formatting.

String Unescaping

When unescape_strings is enabled, the beautifier will decode printable characters that were encoded in xNN notation.

Array Indentation

When keep_array_indentation is enabled, the original indentation of arrays is preserved even when it doesn't match the configured indent settings.

docs

cli.md

configuration.md

css.md

html.md

index.md

javascript.md

tile.json