beautify-with-words
beautify-with-words
beautifies javascript and replaces variable names with unique "long-ish words". It uses UglifyJS2's beautifier, but uses a phonetic word generator to rename variables. This makes it easier less-hard to read unminified code and do things like search-and-replace.
Installation
With npm
as a global package:
{sudo} npm install -g beautify-with-words
Usage
beautify-with-words [input_file.js] [options]
beautify-with-words
takes one file at a time – or, if no input file is specified, then input is read from STDIN
.
- Use the
-o
/--output
option to specify an output file. By default, the output goes toSTDOUT
; - Use the
-b
/--beautify
to pass UglifyJS2 beautifier options; - And
-h
/--help
for help.
Reading from, and saving to, a file:
beautify-with-words backbone-min.js -o backbone-youre-beautiful-regardless.js
Send the output to STDOUT
, and turn off syntax beautification but keep variable renaming:
beautify-with-words backbone-min.js -b beautify=false
Tell the beautifier to always insert brackets in if
, for
, do
, while
or with
statements. Go here for more options.
beautify-with-words backbone-min.js -b bracketize=true
Sample
This:
curl http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js | beautify-with-words
Turns this:
// stuff...if!h&&typeof require!=="undefined"h=;a$=tjQuery||tZepto||tender||t$;a{tBackbone=e;return this};aemulateHTTP=false;aemulateJSON=false;var o=aEvents={if!||!ereturn this;this_events||this_events={};var r=this_eventst||this_eventst=;r;return this}{if!||!ereturn this;var r=this;var s=h;s_callback=e;return this}// more stuff...
Into this:
// stuff... if !quinis && typeof require !== "undefined" quinis = ; tenmiey$ = deegipjQuery || deegipZepto || deegipender || deegip$; tenmiey { deegipBackbone = upan; return this; }; tenmieyemulateHTTP = false; tenmieyemulateJSON = false; var koken = tenmieyEvents = { if ! || !latay return this; this_events || this_events = {}; var cyem = this_eventsbedad || this_eventsbedad = ; cyem; return this; } { if ! || !flakou return this; var neri = this; var lopo = quinis; lopo_callback = flakou; return this; }// more stuff...
API Usage
var beautifyWithWords = require('beautify-with-words');
var beautifiedCode = beautifyWithWords(code, options);
code
is a string of the code you want to beautify. options
is optional, and must be an object. It has a b
property, which is an object of the options to be passed to UglifyJS2.
var fs = require('fs');
var beautifyWithWords = require('beautify-with-words');
var backboneSource = fs.readFileSync('backbone-min.js', { encoding: 'utf8' });
var beautified = beautifyWithWords(backboneSource, { b: { bracketize: true } });
console.log(beautified);