Richard Bucker

javascript and long strings

Posted at — Dec 30, 2012

I have not been a fan of NodeJS but recently I have been writing a lot of JavaScript. As a result of this bias I found myself passing on perl, python and ruby in order to “template” a SQL script in favor of NodeJS. (I suppose if I had to use a single callback I would have a different opinion but I did like the utility nature of the experience.)Installing node and the necessary dependencies was pretty simple:# install NVM (nodejs version manager)curl https://raw.github.com/creationix/nvm/master/install.sh | sh# install nodejsnvm install 0.8.16# I’m not certaint if this is required but I USE and set this version of nodejs as default.nvm use 0.8.16nvm alias default 0.8.16# install the mustache template library npm install mustache# install the sprintf library because it’s simplenpm install sprintfThat’s it. The rest of the project was my code. Initially I was looking for some “javascript long string” help in google and I did find some useful examples, however, some did not work with NodeJS. Likevar string = (<r><![CDATA[ <li class=" style=“background-color: rgb(248, 248, 248) !important; border-left-color: rgb(108, 226, 108) !important; border-left-style: solid !important; border-width: 0px 0px 0px 3px !important; list-style: decimal outside; margin: 0px !important; padding: 0px 0px 0px 10px !important;"><div class=” style=“border: 0px !important; color: rgb(0, 0, 0) !important; margin: 0px !important; padding: 0px !important; width: 472px;">    The text string goes here.  Since this is a XML CDATA section, <li class=” style=“background-color: rgb(248, 248, 248) !important; border-left-color: rgb(108, 226, 108) !important; border-left-style: solid !important; border-width: 0px 0px 0px 3px !important; list-style: decimal outside; margin: 0px !important; padding: 0px 0px 0px 10px !important;"><div class=” style=“border: 0px !important; color: rgb(0, 0, 0) !important; margin: 0px !important; padding: 0px !important; width: 472px;">   stuff like <> work fine too, even if definitely invalid XML.   <li class=” style=“background-color: rgb(248, 248, 248) !important; border-left-color: rgb(108, 226, 108) !important; border-left-style: solid !important; border-width: 0px 0px 0px 3px !important; list-style: decimal outside; margin: 0px !important; padding: 0px 0px 0px 10px !important;"><div class=” style=“border: 0px !important; color: rgb(0, 0, 0) !important; margin: 0px !important; padding: 0px !important; width: 472px;">]]></r>).toString();(snipped from here)This example might work in a browser but it does not seem to work in NodeJS. As an aside there was a  CoffeeScript version that I liked but I was not going to install CoffeeScript as a dependency for this little trinket.Ultimately I implemented this:function hereDoc(f) { return f.toString(). replace(/^[^/]+/*!?/, ''). replace(/*/[^/]+$/, '');}You can have here-documents like this:var tennysonQuote = hereDoc(function() {/! Theirs not to make reply, Theirs not to reason why, Theirs but to do and die/}); and it worked great. I put the rest of the bits together and I’m generating SQL like I had hoped. By the time I have to run this script again I hope to clean it up and automate it so I never have to run it again.