├── LICENSE-GPL ├── LICENSE-MIT ├── README.markdown ├── ba-packify.js └── examples ├── aivopaas.html ├── aivopaas.min.js ├── aivopaas.pck.js ├── getify.html ├── getify.min.js ├── getify.pck.js ├── organ1k.html ├── organ1k.min.js └── organ1k.pck.js /LICENSE-GPL: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 "Cowboy" Ben Alman 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # Packify: Something like a JavaScript packer # 2 | 3 | Just a little something I'm wasting my time with. 4 | 5 | * this is a work in progress, so YMMV 6 | * probably only useful for not-too-small, but not-too-big bits of code 7 | * run it as `packify('code')` in the console 8 | * code should be pre-minified or closure compiled 9 | * unnecessary whitespace, semicolons, comments, etc should be removed beforehand 10 | * code should use `"` wherever possible (avoid `'`, as it needs to be escaped) 11 | * code should have lots of repeated strings, so reuse vars wherever possible 12 | * project page, "packifier" web app, lecture tour coming soon 13 | 14 | 15 | ## How it works ## 16 | 17 | ### Encoding ### 18 | 19 | 1. The input source is scanned for repeated strings between 2-20 chars in length, and an array is built. Each array item consists of the string and the number of occurrences. Items that won't save characters are discarded. 20 | 2. The array is sorted based on items that will yield the highest character savings (given the current state of the source). 21 | 3. The first array item, with the highest potential character savings, is shifted off the array. 22 | 4. Replace that item's string in the source with the next unused placeholder char (ASCII 1-31 and 127, excluding 10 and 13). 23 | 5. That ASCII character + the replacement string is appended to the replacement map. 24 | 6. Repeat steps 2-5 until there are no more placeholder ASCII chars or possible replacement strings. 25 | 26 | ### Decoding ### 27 | 28 | 1. For each placeholder char + replacement string combo in the replacement map, replace all matching placeholder chars in the source with the replacement string. 29 | 2. Eval. 30 | 31 | ## Examples ## 32 | 33 | Organ1k, my [JS1k demo](http://js1k.com/demo/450) 34 | [before](http://benalman.com/code/projects/javascript-packify/examples/organ1k.min.js) (1024b) 35 | [after](http://benalman.com/code/projects/javascript-packify/examples/organ1k.pck.js) (981b) - [test it out](http://benalman.com/code/projects/javascript-packify/examples/organ1k.html) 36 | 37 | Kyle Simpson's [JS1k demo](http://js1k.com/demo/424) 38 | [before](http://benalman.com/code/projects/javascript-packify/examples/getify.min.js) (1024b) 39 | [after](http://benalman.com/code/projects/javascript-packify/examples/getify.pck.js) (934b) - [test it out](http://benalman.com/code/projects/javascript-packify/examples/getify.html) 40 | 41 | @aivopaas's [JS1k demo](http://js1k.com/demo/197) 42 | [before](http://benalman.com/code/projects/javascript-packify/examples/aivopaas.min.js) (1314b unpacked, was 1024b using his crusher) 43 | [after](http://benalman.com/code/projects/javascript-packify/examples/aivopaas.pck.js) (1015b) - [test it out](http://benalman.com/code/projects/javascript-packify/examples/aivopaas.html) 44 | 45 | 46 | ## Credits ## 47 | 48 | While this code isn't based on [@aivopaas' Javascript crusher](http://www.iteral.com/jscrush/), which I didn't even know existed until after the fact, it was inspired by the "crushed" source of his [JS1k demo](http://js1k.com/demo/197), which motivated me to try to roll my own version. 49 | 50 | Special thanks to [Kyle Simpson](http://github.com/getify) for being the catalyst. 51 | 52 | 53 | ## License ## 54 | Copyright (c) 2010 "Cowboy" Ben Alman 55 | Dual licensed under the MIT and GPL licenses. 56 | [http://benalman.com/about/license/](http://benalman.com/about/license/) 57 | -------------------------------------------------------------------------------- /ba-packify.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * JavaScript Packify - v0.4 - 8/24/2010 3 | * http://benalman.com/ 4 | * 5 | * Copyright (c) 2010 "Cowboy" Ben Alman 6 | * Dual licensed under the MIT and GPL licenses. 7 | * http://benalman.com/about/license/ 8 | */ 9 | 10 | // Currently tested in WebKit console, TODO: node.js version. 11 | 12 | function packify( input ) { 13 | var script = input, 14 | len, 15 | i, 16 | chunk, 17 | chunk_size, 18 | re, 19 | matches, 20 | savings, 21 | 22 | potential, 23 | potentials = {}, 24 | potentials_arr = [], 25 | 26 | map = '', 27 | char_code, 28 | char, 29 | output; 30 | 31 | // Single quotes need to be escaped, so use double-quotes in your input 32 | // source whenever possible. 33 | script = script.replace( /'/g, "\\'" ); 34 | 35 | // Replace any non-space whitespace with spaces (shouldn't be necessary). 36 | script = script.replace( /\s+/g, ' ' ); 37 | 38 | // Return number of chars saved by replacing `count` occurences of `string`. 39 | function get_savings( string, count ) { 40 | return ( string.length - 1 ) * ( count - 1 ) - 2; 41 | }; 42 | 43 | // Just trying to keep things DRY here... Let's match some patterns! 44 | function get_re_match( pattern, text ) { 45 | var re = RegExp( pattern.replace( /(\W)/g, '\\$1' ), 'g' ); 46 | return [ 47 | text.match( re ) || [], 48 | re 49 | ]; 50 | }; 51 | 52 | // Look for recurring patterns between 2 and 20 characters in length (could 53 | // have been between 2 and len / 2, but that gets REALLY slow). 54 | for ( chunk_size = 2, len = script.length; chunk_size <= 20; chunk_size++ ) { 55 | 56 | // Start at the beginning of the input string, go to the end. 57 | for ( i = 0; i < len - chunk_size; i++ ) { 58 | 59 | // Grab the "chunk" at the current position. 60 | chunk = script.substr( i, chunk_size ); 61 | 62 | if ( !potentials[ chunk ] ) { 63 | // Find the number of chunk matches in the input script. 64 | matches = get_re_match( chunk, script )[0]; 65 | 66 | // If any matches, save this chunk as a potential pattern. By using an 67 | // object instead of an array, we don't have to worry about uniquing 68 | // the array as new potentials will just overwrite previous potentials. 69 | if ( get_savings( chunk, matches.length ) >= 0 ) { 70 | potentials[ chunk ] = matches.length; 71 | } 72 | } 73 | } 74 | } 75 | 76 | // Since we'll need to sort the potentials, create an array from the object. 77 | for ( i in potentials ) { 78 | potentials.hasOwnProperty( i ) 79 | && potentials_arr.push({ pattern: i, count: potentials[ i ] }); 80 | } 81 | 82 | // Potentials get sorted first by byte savings, then by # of occurrences 83 | // (favoring smaller count, longer patterns), then lexicographically. 84 | function sort_potentials( a, b ) { 85 | return get_savings( b.pattern, b.count ) - get_savings( a.pattern, a.count ) 86 | || a.count - b.count 87 | || ( a.pattern < b.pattern ? -1 : a.pattern > b.pattern ? 1 : 0 ); 88 | }; 89 | 90 | // Loop over all the potential patterns, unless we run out of replacement 91 | // chars first. Dealing with 7-bit ASCII, valid replacement chars are 1-31 92 | // & 127 (excluding ASCII 10 & 13). 93 | for ( char_code = 0; potentials_arr.length && char_code < 127; ) { 94 | 95 | // Re-calculate match counts. 96 | for ( i = 0, len = potentials_arr.length; i < len; i++ ) { 97 | potential = potentials_arr[i]; 98 | matches = get_re_match( potential.pattern, script )[0]; 99 | potential.count = matches.length; 100 | } 101 | 102 | // Sort the array of potentials such that replacements that will yield the 103 | // highest byte savings come first. 104 | potentials_arr.sort( sort_potentials ); 105 | 106 | // Get the current best potential replacement. 107 | potential = potentials_arr.shift(); 108 | 109 | // Find all chunk matches in the input string. 110 | chunk = potential.pattern; 111 | matches = get_re_match( chunk, script ); 112 | re = matches[1]; 113 | matches = matches[0]; 114 | 115 | // Ensure that replacing this potential pattern still actually saves bytes. 116 | savings = get_savings( chunk, matches.length ); 117 | if ( savings >= 0 ) { 118 | 119 | // Increment the current replacement character. 120 | char_code = ++char_code == 10 ? 11 121 | : char_code == 13 ? 14 122 | : char_code == 32 ? 127 123 | : char_code; 124 | 125 | // Get the replacement char. 126 | char = String.fromCharCode( char_code ); 127 | 128 | //console.log( char_code, char, matches.length, chunk, savings ); 129 | 130 | // Replace the pattern with the replacement character. 131 | script = script.replace( re, char ); 132 | 133 | // Add the char + pattern combo into the map of replacements. 134 | map += char + chunk; 135 | } 136 | } 137 | 138 | // For each group of 1 low ASCII char / 1+ regular ASCII chars combo in the 139 | // map string, replace the low ASCII char in the script string with the 140 | // remaining regular ASCII chars, then eval the script string. Using with in 141 | // this manner ensures that the temporary _ var won't be leaked. 142 | output = "" 143 | + "with({_:'" + script + "'})" 144 | + "'" + map + "'.replace(/.([ -~]+)/g,function(x,y){" 145 | + "_=_.replace(RegExp(x[0],'g'),y)" 146 | + "})," 147 | + "eval(_)"; 148 | 149 | if ( eval( output.replace( 'eval(_)', '_' ) ) === input ) { 150 | // If the output *actually* evals to the input string, packing was 151 | // successful. Log some useful stats and return the output. 152 | console.log( 'Success, ' + input.length + 'b -> ' + output.length 153 | + 'b (' + ( input.length - output.length ) + 'b or ' 154 | + ( ~~( ( 1 - output.length / input.length ) * 10000 ) / 100 ) 155 | + '% savings)' ); 156 | 157 | return output; 158 | 159 | } else { 160 | // Otherwise, exit with an error. 161 | console.log( 'Error!' ); 162 | return input; 163 | } 164 | }; 165 | 166 | // Small test string: 167 | 168 | //packify('console.log("I like that you like your hat!")'); 169 | 170 | // Organ1k: 171 | 172 | //packify('(function(J){var A,v,F,D,q,n,G=J.body.style,f=J.getElementById("c"),d=f.getContext("2d"),p=32,I=360,e=Math,B=e.min,b=e.sin,c=e.cos,l=e.random,z=e.PI*2,o=z/I,t=0,a=0,C=0,k=0,h=G.margin=0,H=2,s=2,r=3,E=6,g=p,u=[],i=[],m="f001fa01ff0107010ff100f14081e8e".split(1),j=l(G.overflow="hidden")*I,w=l(onmousemove=function(x){g=0;q=x.clientX-F;n=x.clientY-D})<.5?1:-1;setInterval(function(L,x,K,y,M){if(!(++t%p)){while(k==~~(y=l(M=l())*6));k=~~y;y<.4?w=-w:y<2?h++:y<3?C=M*7:y<4?H=M*8+1:y<5?s=M*3+1:r=B(E=M*8+4,l()*5+5)-2}A=f.width=innerWidth;v=f.height=innerHeight;L=B(F=A/2,D=v/2);x=L/I;L-=20*x;if(++g>p){if(C<1){j-=H*w*4;q=b(j*o)*L;n=c(j*o)*L}else{j-=H*w*2;y=e.abs(q=b(j*o)*L);q=y*c(M=e.atan2(0,q)+j*o/C);n=y*b(M)}}for(K=0;KE?-1:MDon\'t idle...or else!

"+k.innerHTML;o=0;w=e.random;l=j.getElementById("c");z=l.style;A=l.getContext("2d");B=[];m=[0,0];g=0;function h(C,G){return(w()*(G-C)+C)<<0}function D(){with(A){clearRect(0,0,u,v);for(t=2;t;){r=0xffffff&m[--t]<<4|h(4,14);for(i=5;i;){with((f=B[t])[--i]){s=h(4,8);x=e.min(U,e.max(0,x+a*b));y=e.min(V,e.max(0,y+c*d));if(x<=0||x>=U){a*=-1;b=s;m[t]=r}if(y<=0||y>=V){c*=-1;d=s;m[t]=r}}}beginPath();lineWidth=8;strokeStyle="#"+m[t].toString(16);moveTo((n=f[0]).x,n.y);for(i=5;i;){lineTo((n=f[--i]).x,n.y)}stroke();closePath()}}g||(g=setInterval(D,20))}function E(){clearTimeout(o);if(g){clearInterval(g);A.clearRect(g=0,0,u,v)}o=setTimeout(D,3E3)}function F(){U=(u=l.width=innerWidth)-1;V=(v=l.height=innerHeight)-1}with(k.style){margin="0";background="#000";color="#fff"}F();z.position="absolute";z.top="0";for(t=2;t;){f=B[--t]=[];for(i=5;i;){p=(1&((w()*9)<<0))?1:-1;q=h(4,8);f[--i]={x:h(1,U),y:h(1,V),a:p,c:p,b:q,d:q}}}E();j.onmousemove=j.onkeydown=E;onresize=F') 177 | 178 | // aivopaas's JS1k entry (un-packed first) 179 | 180 | //packify('w=document.getElementById("c"),g=w.getContext("2d"),E=4099,C=8191,R=[],e=l=n=L=0;I=function(a,c,d){return(i=[[[2,7],[2,6,2],[0,7,2],[2,3,2]],[[1,3,2],[6,3]],[[2,3,1],[3,6]],[[2,2,6],[0,7,1],[3,2,2],[4,7]],[[2,2,3],[1,7],[6,2,2],[0,7,4]],[[2,2,2,2],[0,15]],[[3,3]]][a%7])[(a+c)%i.length]};P=function(a,c,d){g.clearRect(0,0,99,w.height=171);g.fillText("Level",117,60);v(m,0,0);g.fillText((L/10<<0),150,60);v(I(l,0),14,1);g.fillText("Lines",117,70);v(I(k,r),x,y);g.fillText(L,150,70);g.fillText("Points",117,80);g.fillText(n,150,80);for(i in R){g.fillText(R[i][1],117,99+i*9);g.fillText(R[i][0],150,99+i*9);}};h=function(a,c,d){k=l;l=Math.random(r=0)*E<<0;x=6;y=-1};M=function(a,c,d){for(i in (o=I(k,r)))m[y+(i<<=0)]|=o[i]<0&&(n+=100*1<>=1);d++}};B=function(a,c,d){for(i=R.length;i--&&R[i][1]16-L/10){t(0,1,0);e=0}",40);s()') 181 | -------------------------------------------------------------------------------- /examples/aivopaas.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/aivopaas.min.js: -------------------------------------------------------------------------------- 1 | w=document.getElementById("c"),g=w.getContext("2d"),E=4099,C=8191,R=[],e=l=n=L=0;I=function(a,c,d){return(i=[[[2,7],[2,6,2],[0,7,2],[2,3,2]],[[1,3,2],[6,3]],[[2,3,1],[3,6]],[[2,2,6],[0,7,1],[3,2,2],[4,7]],[[2,2,3],[1,7],[6,2,2],[0,7,4]],[[2,2,2,2],[0,15]],[[3,3]]][a%7])[(a+c)%i.length]};P=function(a,c,d){g.clearRect(0,0,99,w.height=171);g.fillText("Level",117,60);v(m,0,0);g.fillText((L/10<<0),150,60);v(I(l,0),14,1);g.fillText("Lines",117,70);v(I(k,r),x,y);g.fillText(L,150,70);g.fillText("Points",117,80);g.fillText(n,150,80);for(i in R){g.fillText(R[i][1],117,99+i*9);g.fillText(R[i][0],150,99+i*9);}};h=function(a,c,d){k=l;l=Math.random(r=0)*E<<0;x=6;y=-1};M=function(a,c,d){for(i in (o=I(k,r)))m[y+(i<<=0)]|=o[i]<0&&(n+=100*1<>=1);d++}};B=function(a,c,d){for(i=R.length;i--&&R[i][1]16-L/10){t(0,1,0);e=0}",40);s() -------------------------------------------------------------------------------- /examples/aivopaas.pck.js: -------------------------------------------------------------------------------- 1 | with({_:'w=getElementById("cg=w.getContex"2dE=4099,C=8191,R=[],e=l=n=L=0;I (i=[[,72,6  7 2 ][1 6]3,6] ,6 73 4,7] 1,76  7,4] 05][3]]][a%7])[(a+c)%i]Pg.clearRec 99,w.height71"Level"60v(m, 0(L/105 60v(I(l,4"Lines"70v(),x,yL5 70"Points"80n5 80R){TexR[1]R[0]5 }hk=l;l=Math.random(r=*E0;x=6;y=-1M(o=)))y+(i=]|=ox;i=o8,a=0;o;o]-C?i]=o]:ah(i;m=E)L;a>0(n+00*1a ||s()t(o=+d)))if(ox+a&y+c+i]) cM(r+=d;x+=a;P(y+=c 1va){i=a,o=c-1;i;i%2Reco*9,d*9,8,8),o,i>>d}Bi=R;iR[1]16-L/1{0,0e=0}",40s()'})'=function(a,c,d){);g.fillText(],[for(i in [i]);,1for( ,2 0, returndocument.I(k,r};t(.lengthR[i+1]=17,m[99+i*9g.fill++,3--0)<<=1"),&&[2'.replace(/.([ -~]+)/g,function(x,y){_=_.replace(RegExp(x[0],'g'),y)}),eval(_) -------------------------------------------------------------------------------- /examples/getify.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/getify.min.js: -------------------------------------------------------------------------------- 1 | j=document;e=Math;k=j.body;k.innerHTML="

Don't idle...or else!

"+k.innerHTML;o=0;w=e.random;l=j.getElementById("c");z=l.style;A=l.getContext("2d");B=[];m=[0,0];g=0;function h(C,G){return(w()*(G-C)+C)<<0}function D(){with(A){clearRect(0,0,u,v);for(t=2;t;){r=0xffffff&m[--t]<<4|h(4,14);for(i=5;i;){with((f=B[t])[--i]){s=h(4,8);x=e.min(U,e.max(0,x+a*b));y=e.min(V,e.max(0,y+c*d));if(x<=0||x>=U){a*=-1;b=s;m[t]=r}if(y<=0||y>=V){c*=-1;d=s;m[t]=r}}}beginPath();lineWidth=8;strokeStyle="#"+m[t].toString(16);moveTo((n=f[0]).x,n.y);for(i=5;i;){lineTo((n=f[--i]).x,n.y)}stroke();closePath()}}g||(g=setInterval(D,20))}function E(){clearTimeout(o);if(g){clearInterval(g);A.clearRect(g=0,0,u,v)}o=setTimeout(D,3E3)}function F(){U=(u=l.width=innerWidth)-1;V=(v=l.height=innerHeight)-1}with(k.style){margin="0";background="#000";color="#fff"}F();z.position="absolute";z.top="0";for(t=2;t;){f=B[--t]=[];for(i=5;i;){p=(1&((w()*9)<<0))?1:-1;q=h(4,8);f[--i]={x:h(1,U),y:h(1,V),a:p,c:p,b:q,d:q}}}E();j.onmousemove=j.onkeydown=E;onresize=F 2 | -------------------------------------------------------------------------------- /examples/getify.pck.js: -------------------------------------------------------------------------------- 1 | with({_:'j=document;e=Math;kbody;k.HTML

D\'t idle...or else!

"+k.HTML;o=0;w=e.random;lgetElementById("c"zs;AgetCtext("2d"B=[];m=[0,0];g=0;C,Greturn(G-C)+C)<<0}D(witAr=0xffffff&mt]<<4|4,14)wit(f=B[t])i]s=4,8xU x+a*b)yV y+c*d)if(xx>=Ua*=bif(yy>=Vc*=d}}beginPatlineW=8;S#"+m[t].toString(16mov0 lin--i }(closePat)}}g||(g=set D,20))}E(oif(g gA.clearg=)}o=setD,3E3)}F(U=(uw=W)V=(vh=H)-1}witk.smarginbackground#00color#fff"}F(z.positiabsolute";z.topf=Bt]=[]p=(1&(9)<<0))?1:q=4,8fi]={x:1,U),y:1,V),a:p,c:p,b:q,d:q}}}E(j.mousemovekeydown=E;resize=F'})';for(i=5;i;){function );){clearinnerfor(t=2;t;){h(=s;m[t]=r} ,e.max(0, Interval( ]).x,n.y)eTo((n=f[Timeout(0,0,u,v=e.min(idthtyle-1;=l.[--){="strokeon(w()*<=0||Rect(eight0";=j.'.replace(/.([ -~]+)/g,function(x,y){_=_.replace(RegExp(x[0],'g'),y)}),eval(_) -------------------------------------------------------------------------------- /examples/organ1k.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/organ1k.min.js: -------------------------------------------------------------------------------- 1 | (function(J){var A,v,F,D,q,n,G=J.body.style,f=J.getElementById("c"),d=f.getContext("2d"),p=32,I=360,e=Math,B=e.min,b=e.sin,c=e.cos,l=e.random,z=e.PI*2,o=z/I,t=0,a=0,C=0,k=0,h=G.margin=0,H=2,s=2,r=3,E=6,g=p,u=[],i=[],m="f001fa01ff0107010ff100f14081e8e".split(1),j=l(G.overflow="hidden")*I,w=l(onmousemove=function(x){g=0;q=x.clientX-F;n=x.clientY-D})<.5?1:-1;setInterval(function(L,x,K,y,M){if(!(++t%p)){while(k==~~(y=l(M=l())*6));k=~~y;y<.4?w=-w:y<2?h++:y<3?C=M*7:y<4?H=M*8+1:y<5?s=M*3+1:r=B(E=M*8+4,l()*5+5)-2}A=f.width=innerWidth;v=f.height=innerHeight;L=B(F=A/2,D=v/2);x=L/I;L-=20*x;if(++g>p){if(C<1){j-=H*w*4;q=b(j*o)*L;n=c(j*o)*L}else{j-=H*w*2;y=e.abs(q=b(j*o)*L);q=y*c(M=e.atan2(0,q)+j*o/C);n=y*b(M)}}for(K=0;KE?-1:Mp C<1 4;q=b;n=c}else{ 2;yabs(q=bq=y*c(Matan2(q)+j*o/Cn=y*b(M)} KE?-1:M