• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 9 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a ๐Ÿ’ฉ tool that replaces boring variable names with their fun emoji counterparts

Emojify

Emojify is a useless tool that replaces boring variable names with their fun emoji counterparts. ๐Ÿ’ฉ๐Ÿ”ฅ

Install

This package is no longer hosted on npm

Another developer requested the use of the package name emojify, which I happily granted because this is package is useless and will be better suited when attached to a real project. I've decided not to republish it under a new name.

If you still to want to install it for some reason, you can do so by installing directly from this repository.

For command line use:

npm install -g danthareja/emojify

For programmatic use:

npm install danthareja/emojify

Usage

Emojify is a drop-in replacement for UglifyJS2 with one major difference - it minifies your variables by default and replaces them with hilarious emojis.

From the command line, run:

emojify input1.js [input2.js ...] [options]

Anything you can do with UglifyJS2 you can do with Emojify, with a few notable exceptions:

  • --mangle and --beautify are turned on by default
  • You can minify your code a little with --compress (-c) or a lot with --uglify (-u)

Examples

Let's run @mathias's String.fromCodePoint polyfill through some emojification

Our source file: String-fromCodePoint.js

/*! https://mths.be/fromcodepoint v0.2.1 by @mathias */
if (!String.fromCodePoint) {
  (function() {
    var defineProperty = (function() {
      // IE 8 only supports `Object.defineProperty` on DOM elements
      try {
        var object = {};
        var $defineProperty = Object.defineProperty;
        var result = $defineProperty(object, object, object) && $defineProperty;
      } catch(error) {}
      return result;
    }());
    var stringFromCharCode = String.fromCharCode;
    var floor = Math.floor;
    var fromCodePoint = function(_) {
      var MAX_SIZE = 0x4000;
      var codeUnits = [];
      var highSurrogate;
      var lowSurrogate;
      var index = -1;
      var length = arguments.length;
      if (!length) {
        return '';
      }
      var result = '';
      while (++index < length) {
        var codePoint = Number(arguments[index]);
        if (
          !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
          codePoint < 0 || // not a valid Unicode code point
          codePoint > 0x10FFFF || // not a valid Unicode code point
          floor(codePoint) != codePoint // not an integer
        ) {
          throw RangeError('Invalid code point: ' + codePoint);
        }
        if (codePoint <= 0xFFFF) { // BMP code point
          codeUnits.push(codePoint);
        } else { // Astral code point; split in surrogate halves
          // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
          codePoint -= 0x10000;
          highSurrogate = (codePoint >> 10) + 0xD800;
          lowSurrogate = (codePoint % 0x400) + 0xDC00;
          codeUnits.push(highSurrogate, lowSurrogate);
        }
        if (index + 1 == length || codeUnits.length > MAX_SIZE) {
          result += stringFromCharCode.apply(null, codeUnits);
          codeUnits.length = 0;
        }
      }
      return result;
    };
    if (defineProperty) {
      defineProperty(String, 'fromCodePoint', {
        'value': fromCodePoint,
        'configurable': true,
        'writable': true
      });
    } else {
      String.fromCodePoint = fromCodePoint;
    }
  }());
}

Default behavior

Running emojify /path/to/String-fromCodePoint.js outputs:

if (!String.fromCodePoint) {
    (function() {
        var ๐Ÿš€ = function() {
            try {
                var ๐Ÿš€ = {};
                var ๐Ÿ’ฉ = Object.defineProperty;
                var ๐Ÿ”ฅ = ๐Ÿ’ฉ(๐Ÿš€, ๐Ÿš€, ๐Ÿš€) && ๐Ÿ’ฉ;
            } catch (๐Ÿ•) {}
            return ๐Ÿ”ฅ;
        }();
        var ๐Ÿ’ฉ = String.fromCharCode;
        var ๐Ÿ”ฅ = Math.floor;
        var ๐Ÿ• = function(๐Ÿš€) {
            var ๐Ÿ• = 16384;
            var ๐Ÿ” = [];
            var ๐ŸŸ;
            var ๐Ÿบ;
            var ๐Ÿป = -1;
            var ๐Ÿ’ = arguments.length;
            if (!๐Ÿ’) {
                return "";
            }
            var ๐Ÿถ = "";
            while (++๐Ÿป < ๐Ÿ’) {
                var ๐Ÿฑ = Number(arguments[๐Ÿป]);
                if (!isFinite(๐Ÿฑ) || ๐Ÿฑ < 0 || ๐Ÿฑ > 1114111 || ๐Ÿ”ฅ(๐Ÿฑ) != ๐Ÿฑ) {
                    throw RangeError("Invalid code point: " + ๐Ÿฑ);
                }
                if (๐Ÿฑ <= 65535) {
                    ๐Ÿ”.push(๐Ÿฑ);
                } else {
                    ๐Ÿฑ -= 65536;
                    ๐ŸŸ = (๐Ÿฑ >> 10) + 55296;
                    ๐Ÿบ = ๐Ÿฑ % 1024 + 56320;
                    ๐Ÿ”.push(๐ŸŸ, ๐Ÿบ);
                }
                if (๐Ÿป + 1 == ๐Ÿ’ || ๐Ÿ”.length > ๐Ÿ•) {
                    ๐Ÿถ += ๐Ÿ’ฉ.apply(null, ๐Ÿ”);
                    ๐Ÿ”.length = 0;
                }
            }
            return ๐Ÿถ;
        };
        if (๐Ÿš€) {
            ๐Ÿš€(String, "fromCodePoint", {
                value: ๐Ÿ•,
                configurable: true,
                writable: true
            });
        } else {
            String.fromCodePoint = ๐Ÿ•;
        }
    })();
}

More minification

Running emojify /path/to/String-fromCodePoint.js -o /path/to/String-fromCodePoint.min.js -u outputs:

if(!String.fromCodePoint){(function(){var๐Ÿš€=function(){try{var๐Ÿš€={};var๐Ÿ’ฉ=Object.defineProperty;var๐Ÿ”ฅ=๐Ÿ’ฉ(๐Ÿš€,๐Ÿš€,๐Ÿš€)&&๐Ÿ’ฉ}catch(๐Ÿ•){}return๐Ÿ”ฅ}();var๐Ÿ’ฉ=String.fromCharCode;var๐Ÿ”ฅ=Math.floor;var๐Ÿ•=function(๐Ÿš€){var๐Ÿ•=16384;var๐Ÿ”=[];var๐ŸŸ;var๐Ÿบ;var๐Ÿป=-1;var๐Ÿ’=arguments.length;if(!๐Ÿ’){return""}var๐Ÿถ="";while(++๐Ÿป<๐Ÿ’){var๐Ÿฑ=Number(arguments[๐Ÿป]);if(!isFinite(๐Ÿฑ)||๐Ÿฑ<0||๐Ÿฑ>1114111||๐Ÿ”ฅ(๐Ÿฑ)!=๐Ÿฑ){throw RangeError("Invalid code point: "+๐Ÿฑ)}if(๐Ÿฑ<=65535){๐Ÿ”.push(๐Ÿฑ)}else{๐Ÿฑ-=65536;๐ŸŸ=(๐Ÿฑ>>10)+55296;๐Ÿบ=๐Ÿฑ%1024+56320;๐Ÿ”.push(๐ŸŸ,๐Ÿบ)}if(๐Ÿป+1==๐Ÿ’||๐Ÿ”.length>๐Ÿ•){๐Ÿถ+=๐Ÿ’ฉ.apply(null,๐Ÿ”);๐Ÿ”.length=0}}return๐Ÿถ};if(๐Ÿš€){๐Ÿš€(String,"fromCodePoint",{value:๐Ÿ•,configurable:true,writable:true})}else{String.fromCodePoint=๐Ÿ•}})()}

to a new file located at /path/to/String-fromCodePoint.min.js

Less minification but more emojis

You can go even harder by passing in --mangle-props. This not only replaces top-level variable names but also hunts down properties on objects.

Running emojify /path/to/String-fromCodePoint.js --compress --mangle-props outputs:

String.๐Ÿš€ || !function() {
    var ๐Ÿš€ = function() {
        try {
            var ๐Ÿš€ = {}, ๐Ÿ’ฉ = Object.defineProperty, ๐Ÿ”ฅ = ๐Ÿ’ฉ(๐Ÿš€, ๐Ÿš€, ๐Ÿš€) && ๐Ÿ’ฉ;
        } catch (๐Ÿ•) {}
        return ๐Ÿ”ฅ;
    }(), ๐Ÿ’ฉ = String.fromCharCode, ๐Ÿ”ฅ = Math.floor, ๐Ÿ• = function(๐Ÿš€) {
        var ๐Ÿ•, ๐Ÿ”, ๐ŸŸ = 16384, ๐Ÿบ = [], ๐Ÿป = -1, ๐Ÿ’ = arguments.length;
        if (!๐Ÿ’) return "";
        for (var ๐Ÿถ = ""; ++๐Ÿป < ๐Ÿ’; ) {
            var ๐Ÿฑ = Number(arguments[๐Ÿป]);
            if (!isFinite(๐Ÿฑ) || 0 > ๐Ÿฑ || ๐Ÿฑ > 1114111 || ๐Ÿ”ฅ(๐Ÿฑ) != ๐Ÿฑ) throw RangeError("Invalid code point: " + ๐Ÿฑ);
            65535 >= ๐Ÿฑ ? ๐Ÿบ.push(๐Ÿฑ) : (๐Ÿฑ -= 65536, ๐Ÿ• = (๐Ÿฑ >> 10) + 55296, ๐Ÿ” = ๐Ÿฑ % 1024 + 56320, 
            ๐Ÿบ.push(๐Ÿ•, ๐Ÿ”)), (๐Ÿป + 1 == ๐Ÿ’ || ๐Ÿบ.length > ๐ŸŸ) && (๐Ÿถ += ๐Ÿ’ฉ.apply(null, ๐Ÿบ), 
            ๐Ÿบ.length = 0);
        }
        return ๐Ÿถ;
    };
    ๐Ÿš€ ? ๐Ÿš€(String, "fromCodePoint", {
        "๐Ÿ’ฉ": ๐Ÿ•,
        "๐Ÿ”ฅ": !0,
        "๐Ÿ•": !0
    }) : String.๐Ÿš€ = ๐Ÿ•;
}();

More usage

Read up on the rest of the commands here

API Reference

Assuming installation via NPM, you can load Emojify in your application like this:

var Emojify = require("emojify");

There's a single top-level function which combines all the steps. If you don't need additional customization, you might want to go with minify. Example:

var result = Emojify.minify("/path/to/file.js");
console.log(result.code); // minified output
// if you need to pass code instead of file name
var result = Emojify.minify("var b = function () {};", {fromString: true});

You can also compress multiple files:

var result = Emojify.minify([ "file1.js", "file2.js", "file3.js" ]);
console.log(result.code);

Read up on the rest of the programmatic API here

FAQs

Can I run Emojified code?

Nope ๐Ÿ˜ข

While ES6 brings stronger Unicode support to our beloved language, not all symbols can be used as valid identifiers. We can use things like var เฒ _เฒ  = 42, but not var ๐Ÿ’ฉ = 43.

@mathias has a great post explaining the details of valid identifiers in ES6

So what's the point?

Maybe one day ESXX will support emoji identifiers. Until then it's just for the lulz ๐Ÿ˜‚

I want my own emojis, the ones you picked suck

You can do that๐Ÿ‘

I've exposed the array of characters that Emojify uses to replace your variable names. Simply edit the array in lib/manglers.js with your emojis of choice.

You can include any number of emojis in this array, but I recommend at least 40 different entires for enough combinations to cover a decent sized file. Here's a good site to copy emojis from.

You could grab some of the (more boring) supported characters and produce actualy working code!

Acknowledgements

This project would not exist without UglifyJS. Also, @mathiasbynens is a Unicode beast. I learned so much from his wonderful resources.