This is a simplified example, but I am working on a code translator that outputs javascript. Due to the way the parsing is done, I have to output the translation in pieces. I.e. I end up with a javascript file that looks similar to the following but much much longer:
function coolfunc() {
var result = "";
greet = function(user,town) {
var output = '';
output += 'Welcome ' + user + '!';
output += 'How is the weather in ' + town + '?';
return output;
}
goobye = function(user,town) {
var output = '';
output += 'Farewell ' + user + '!';
output += 'Enjoy the weather in ' + town + '!';
return output;
}
result += "Some output 1";
result += "Some output 2";
result += greet("Larry","Cool town");
result += goobye("Larry","Cool town");
return result;
}
Is there any post-processor I could use to condense the above into something like the following:
function coolfunc() {
greet = function(user,town) {
var output = 'Welcome ' + user + '!'+'How is the weather in ' + town + '?';
return output;
}
goobye = function(user,town) {
var output = 'Farewell ' + user + '!'+'Enjoy the weather in ' + town + '!';
return output;
}
var result = "Some output 1"+"Some output 2"+greet("Larry","Cool town")+goobye("Larry","Cool town");
return result;
}
If it could combine adjacent static string concatenations that would be gravy.
I figured that yuicompressor or closure compiler would do so, but as far as I can tell they don’t.
Edit:
Comments so far seem to be telling me to do this in the translator. I do not think this is the best option because it would make reading the translation very difficult… similar to why people write verbose code and then minify it for production.
If anyone comes across this, it looks like closure compiler can handle this as of revision 1576 (http://code.google.com/p/closure-compiler/source/detail?r=1576)