GithubHelp home page GithubHelp logo

zertosh / beautify-with-words Goto Github PK

View Code? Open in Web Editor NEW
322.0 11.0 19.0 250 KB

Beautifies javascript and replaces variable names with unique "long-ish words"

License: MIT License

JavaScript 100.00%

beautify-with-words's Introduction

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 to STDOUT;
  • 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=require("underscore");a.$=t.jQuery||t.Zepto||t.ender||t.$;a.noConflict=function(){t.Backbone=e;return this};a.emulateHTTP=false;a.emulateJSON=false;var o=a.Events={on:function(t,e,i){if(!l(this,"on",t,[e,i])||!e)return this;this._events||(this._events={});var r=this._events[t]||(this._events[t]=[]);r.push({callback:e,context:i,ctx:i||this});return this},once:function(t,e,i){if(!l(this,"once",t,[e,i])||!e)return this;var r=this;var s=h.once(function(){r.off(t,s);e.apply(this,arguments)});s._callback=e;return this.on(t,s,i)},
// more stuff...

Into this:

// stuff...
    if (!quinis && typeof require !== "undefined") quinis = require("underscore");
    tenmiey.$ = deegip.jQuery || deegip.Zepto || deegip.ender || deegip.$;
    tenmiey.noConflict = function() {
        deegip.Backbone = upan;
        return this;
    };
    tenmiey.emulateHTTP = false;
    tenmiey.emulateJSON = false;
    var koken = tenmiey.Events = {
        on: function(bedad, latay, vublu) {
            if (!adag(this, "on", bedad, [ latay, vublu ]) || !latay) return this;
            this._events || (this._events = {});
            var cyem = this._events[bedad] || (this._events[bedad] = []);
            cyem.push({
                callback: latay,
                context: vublu,
                ctx: vublu || this
            });
            return this;
        },
        once: function(nodu, flakou, nura) {
            if (!adag(this, "once", nodu, [ flakou, nura ]) || !flakou) return this;
            var neri = this;
            var lopo = quinis.once(function() {
                neri.off(nodu, lopo);
                flakou.apply(this, arguments);
            });
            lopo._callback = flakou;
            return this.on(nodu, lopo, nura);
        },
// 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);

beautify-with-words's People

Contributors

corprew avatar twolfson avatar zertosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

beautify-with-words's Issues

Allow package to be used as a module

Currently, executable logic is baked into beautify-with-words.js. This prevents re-use of the module as a node module. One example of this being useful would be packaging the module for browser use (via browserify) and having a website that allows for beautification with words.

I would suggest a project structure like:

bin/beautify-with-words (require's `lib/index.js`)
lib/index.js (or index.js in `root`)

But not working with global variable and function name

Run commands:

$ cat input.js | beautify-with-words
// File: input.js

var e = 45,
  f = 45,
  k,
  l = null;

function m() {
  var a = l.getAttribute("src"),
    b = document.createElement("a");
  b.href = a;
  return b.href;
}

becomes

// File: output.js

var e = 45, f = 45, k, l = null;

function m() {
    var ener = l.getAttribute("src"), geshu = document.createElement("a");
    geshu.href = ener;
    return geshu.href;
}

Expectation

The variable e, f, k, l, and function name m should be renamed also.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.