Skip to content

Instantly share code, notes, and snippets.

@programmingthomas
Last active September 13, 2017 09:19
Show Gist options
  • Save programmingthomas/5223938 to your computer and use it in GitHub Desktop.
Save programmingthomas/5223938 to your computer and use it in GitHub Desktop.
MaxifyJS v0.1 - Minification is cool because it kills whitespace and optimizes your code. Maxification is better; it adds whitespace, unnecessary comments and makes your code inefficient.
<!DOCTYPE html>
<html>
<head>
<title>MaxifyJS 0.1</title>
<style>
textarea
{
height:600px;
max-height:600px;
width:49%;
display:inline-block;
overflow:scroll;
}
#entry
{
align:left;
}
#results
{
align:right;
}
</style>
</head>
<body>
<textarea id="entry" onkeyup="setScriptContents(maxifyJS(document.getElementById('entry').value))">Type your code here</textarea>
<textarea id="results" contenteditable>See the results here</textarea>
<script>
var newLineConstant = "\n";
var nlc = newLineConstant;
var tabConstant = " ";
var varComments = ["This creates a new variable", "Creates a variable", "Makes a new variable"];
var functionComments = ["This line of code defines a new function", "This handy FUNCTION creates a function", "Functions are SOOO coool"];
var ifComments = ["Iffy iffy iffy if", "If this then that!", "I love ifs", "This line of code defines an IF statement. If the statement is true, the code will execute"];
var elseComments = ["That other thing. Yeah, that wasn't true. Run this instead", "ELSE???????", "i am a noob. i dont now wat dis lin of code doos."];
var arrayComments = ["You're a wizard, 'Array", "An array is like a beautiful list of objects"];
var emptyLineComments = ["This is empty line of code. It doesn't do anything", "Empty lines of code add beauty to code"];
var commentComments = ["This next line of code contains a comment. Comments are really useful for adding extra information", "Comments make code readable", "COMMENTCEPTION"];
var alertComments = ["Alerts are a handy way to pop up information to the user", "Alerts annoy the user. Use alerts for everything", "If you don't love alerts then you haven't lived"];
var forComments = ["For loops go round for a bit", "Declares a for loop", "I'd do anything, FOR you dear anything", "2 + 2 = 4"];
var whileComments = ["While loops spin my head right round, right round", "Declares a while loop"];
function setScriptContents(contents)
{
document.getElementById("results").value = contents;
document.getElementById("results").height = document.getElementById("entry").height;
}
function maxifyJS(original)
{
var split = original.split(nlc);
if (split.length == 0) split[0] = original;
//Comment line
var cl = split.length > 10 ? "" : nlc;
var newScript = "/*" + nlc + "This is the beginning of the file" + nlc + "*/" + nlc;
var indentation = 1;
for (var i = 0; i < Math.max(1, split.length - 1); i++)
{
var t = tabs(indentation);
var loc = split[i];
if (split[i].indexOf("var ") >= 0 && split[i].indexOf("for") < 0)
{
var allDecs = loc.substring(loc.indexOf("var ") + 4).split(/(,)(?=(?:[^"]|"[^"]*")*$)/);
loc = "";
for (var n = 0; n < allDecs.length; n++)
{
if (allDecs[n].trim() != "," & allDecs[n].trim() != "") loc += t + "var " + allDecs[n] + (allDecs[n].trim().indexOf(";") == allDecs[n].length - 2 ? "" :";") + nlc;
}
}
if (split[i].indexOf("}") >= 0) indentation--;
if (loc == "") newScript += comment(loc, emptyLineComments, t, cl);
else if (loc.indexOf("for") >= 0) newScript += comment(loc, forComments, t, cl);
else if (loc.indexOf("while") >= 0) newScript += comment(loc, whileComments, t, cl);
else if (loc.indexOf("var ") >= 0) newScript += comment(loc, varComments, t, cl);
else if (loc.indexOf("if") >= 0) newScript += comment(loc, ifComments, t, cl);
else if (loc.indexOf("function") >= 0) newScript += comment(loc, functionComments, t, cl);
else if (loc.indexOf("else") >= 0) newScript += comment(loc, elseComments, t, cl);
else if (loc.indexOf("array") >= 0) newScript += comment(loc, arrayComments, t, cl);
else if (loc.indexOf("alert") >= 0) newScript += comment(loc, alertComments, t, cl);
else if (loc.indexOf("/*") >= 0 || loc.indexOf("//") >= 0) newScript += comment(loc, commentComments, t, cl);
else newScript += tabs(indentation) + loc + nlc;
if (split[i].indexOf("{") >= 0) indentation++;
}
newScript += "/*" + cl + "This is the end of the file" + cl + "*/";
return newScript;
}
function comment(lineOfCode, comments, t, cl)
{
return t + "/*" + (cl.length > 0 ? cl + t : tabConstant) + random(comments) + (cl.length > 0 ? cl + t : tabConstant) + "*/" + nlc + t + lineOfCode + Array(Math.floor(Math.random() * 10)).join(tabConstant) + nlc;
}
function random(arrayToSelectFrom)
{
return arrayToSelectFrom[Math.floor(Math.random() * arrayToSelectFrom.length)];
}
function tabs(count)
{
return Array(count).join(tabConstant);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment