tadeuzagallo
10/25/2013 - 6:50 PM

140Bytes - Self 2 Binary

140Bytes - Self 2 Binary

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                    Version 2, December 2004

 Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO.

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(a){//placeholder for internal function
  a=Function('a,'+ // string to be parsed
             'b,'+ // output string
             'c,'+ // keys of the string
             'd',  // byte offset for each char
             "b='';" + // initialize output string
             "for(c in a)"+ // iterate through input string
               "for(d=8;d--;)"+ // iterate through each of the 8 bits
                 "b+=a.charCodeAt(c)&1<<d?1:0;"+ // check if the bit is on
             "return b" // return the output string
   );
   return a + // return the function declaration
     "\n\n"+ 
     a(a+"") // plus the binary version of the string
}()
function(a){a=Function('a,b,c,d',"b='';for(c in a)for(d=8;d--;)b+=a.charCodeAt(c)&1<<d?1:0;return b");return a+"\n\n"+a(a+"")}()
{
  "name": "Self2Binary",

  "description": "Simple function that converts and outputs its source to binary",

  "keywords": [
    "string",
    "binary"
  ]
}
<!DOCTYPE html> 
<title>Self 2 Binary</title>
<style>
pre {
  word-wrap: break-word;
}
</style>
<div>Expected value: <pre>function anonymous(a,b,c,d
/**/) {
b='';for(c in a)for(d=8;d--;)b+=a.charCodeAt(c)&amp;1&lt;&lt;d?1:0;return b
}

011001100111010101101110011000110111010001101001011011110110111000100000011000010110111001101111011011100111100101101101011011110111010101110011001010000110000100101100011000100010110001100011001011000110010000001010001011110010101000101010001011110010100100100000011110110000101001100010001111010010011100100111001110110110011001101111011100100010100001100011001000000110100101101110001000000110000100101001011001100110111101110010001010000110010000111101001110000011101101100100001011010010110100111011001010010110001000101011001111010110000100101110011000110110100001100001011100100100001101101111011001000110010101000001011101000010100001100011001010010010011000110001001111000011110001100100001111110011000100111010001100000011101101110010011001010111010001110101011100100110111000100000011000100000101001111101</pre></div>
<div>Actual value: <pre id="ret"></pre></div>
<script>
  // write a small example that shows off the API for your example
  // and tests it in one fell swoop.

  var myFunction = function(a){a=Function('a,b,c,d',"b='';for(c in a)for(d=8;d--;)b+=a.charCodeAt(c)&1<<d?1:0;return b");return a+"\n\n"+a(a+"")}();

  document.getElementById( "ret" ).innerText = myFunction
</script>