Usage
Example
Future
Description
This Perl script takes a javascript source file as standard in, removes extra spaces, blank lines, comments, and attempts to compact the lines to a specified number of characters. It prints to standard out.
Download source.
Usage
trim80.pl [-j] [-s width] FILE
Options
- -j
- Justify the margins on output.
- -s width
- Set the number of characters per line. Default is 60.
Example
Given the following piece of javascript source.
$ cat my.js
function loadXMLDoc(url) {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
}
}
function processReqChange() {
if (req.readyState == 4) { // readyState 4 means our XMLHttpRequest is complete
if (req.status == 200) { // the HTTP Response is 200 OK
doIt(); // our function(s) to display the new data
} else { // anything besides response code 200 goes here
alert("There was a problem fetching the xml\n" +
req.statusText);
}
}
}
function showBox(evt) {
var elem = evt.target;
if (elem.selectedIndex > 0) {
try {
loadXMLDoc(elem.options[elem.selectedIndex].value);
}
catch(e) {
var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
alert("can't get xml data:\n" + msg);
return;
}
} else if (elem.attributes.getNamedItem("name").value) {
try {
loadXMLDoc(elem.attributes.getNamedItem("name").value);
} catch(e) {
var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
alert("msg: 243. can't get xml data:\n" + msg);
return;
}
}
}
function doIt() {
// var bs = req.responseXML.getElementsByTagName("sports-title")[0];
var bs = req.responseText;
var div = document.getElementById("boxscore");
div.innerHTML = "";
div.innerHTML = bs;
}
This will justify the javascript source into lines of 60 characters.
$ trim80.pl -j my.js
function loadXMLDoc(url){ if(window.XMLHttpRequest){req=new
XMLHttpRequest(); req.onreadystatechange=processReqChange;
req.open("GET", url, true); req.send(null); } } function
processReqChange(){ if(req.readyState==4){if(req.status==
200){ doIt(); } else { alert
("There was a problem fetching the xml\n" +req.statusText);}
} } function showBox(evt){ var elem=evt.target; if
(elem.selectedIndex > 0){ try {
loadXMLDoc(elem.options[elem.selectedIndex].value); }catch
(e){ var msg=(typeof e== "string")?e:((e.message)?
e.message:"Unknown Error"); alert("can't get xml data:\n"+
msg); return ; } } else if
(elem.attributes.getNamedItem("name").value){ try {
loadXMLDoc(elem.attributes.getNamedItem("name").value); }
catch(e){ var msg=(typeof e== "string")?e:((e.message)?
e.message:"Unknown Error"); alert
("msg: 243. can't get xml data:\n"+msg); return;}}}function
doIt(){ var bs=req.responseText; var
div=document.getElementById("boxscore"); div.innerHTML="";
div.innerHTML=bs;}
This will condense the javascript source into lines of 80 characters.
$ trim80.pl -s 80 my.js
function loadXMLDoc(url){if(window.XMLHttpRequest){req=new XMLHttpRequest();
req.onreadystatechange=processReqChange;req.open("GET",url,true);req.send(null);
}}function processReqChange(){if(req.readyState==4){if(req.status==200){doIt();}
else{alert("There was a problem fetching the xml\n" +req.statusText);}}}function
showBox(evt){var elem=evt.target;if(elem.selectedIndex > 0){try {
loadXMLDoc(elem.options[elem.selectedIndex].value);}catch(e){var msg=(typeof e==
"string")?e:((e.message)?e.message:"Unknown Error");alert
("can't get xml data:\n"+msg);return;}} else if
(elem.attributes.getNamedItem("name").value){try {
loadXMLDoc(elem.attributes.getNamedItem("name").value);} catch(e){var
msg=(typeof e=="string")?e:((e.message)?e.message:"Unknown Error");alert
("msg: 243. can't get xml data:\n"+msg);return;}}}function doIt(){var
bs=req.responseText;var div=document.getElementById("boxscore");
div.innerHTML="";div.innerHTML=bs;}
Future
- Substitute variable names with a, b,...n.
- Substitute function names with a, b,...n.
- Complete list of keywords that are safe to split on.
- Make padding algorithm smarter.
