I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality.
I found this:
heredoc = \'\\
As others have said, ES6 template strings give you most of what traditional heredocs provide.
If you want to go a step further and use a tagged template string, theredoc is a nice utility function that lets you do this:
if (yourCodeIsIndented) {
console.log(theredoc`
Theredoc will strip the
same amount of indentation
from each line.
You can still indent
further if you want.
It will also chop off the
whitespace-only first and
last lines.
`)
}
Try ES6 String Template, you can do something like
var hereDoc = `
This
is
a
Multiple
Line
String
`.trim()
hereDoc == 'This\nis\na\nMultiple\nLine\nString'
=> true
You can use this great feature even in older browsers with TypeScript
No, unfortunately JavaScript does not support anything like heredoc.
I'm posting this version as it avoids the use of regex for something so trivial.
IMHO regex is an obfuscation that was created as a practical joke amongst perl developers. the rest of the community took them seriously and we now pay the price, decades later. don't use regex, except for backward compatabilty with legacy code. there is no excuse these days to write code that is not immediately human readable and understandable. regex violates this principle on every level.
I've also added a way to add the result to the current page, not that this was asked for.
function pretty_css () {
/*
pre { color: blue; }
*/
}
function css_src (css_fn) {
var css = css_fn.toString();
css = css.substr(css.indexOf("/*")+2);
return css.substr(0,css.lastIndexOf("*/")).trim();
}
function addCss(rule) {
let css = document.createElement('style');
css.type = 'text/css';
if (css.styleSheet) css.styleSheet.cssText = rule; // Support for IE
else css.appendChild(document.createTextNode(rule)); // Support for the rest
document.getElementsByTagName("head")[0].appendChild(css);
}
addCss(css_src(pretty_css));
document.querySelector("pre").innerHTML=css_src(pretty_css);
<pre></pre>
You could use CoffeeScript, a language that compiles down to JavaScript. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime.
And of course, it has heredocs :)
Building on Zv_oDD's answer, I created a similar function for easier reuse.
Warning: This is a non-standard feature of many JS interpreters, and will probably be removed at some point, but as I'm building a script to be only used in Chrome, I am using it! Do not ever rely on this for client-facing websites!
// Multiline Function String - Nate Ferrero - Public Domain
function heredoc(fn) {
return fn.toString().match(/\/\*\s*([\s\S]*?)\s*\*\//m)[1];
};
Use:
var txt = heredoc(function () {/*
A test of horrible
Multi-line strings!
*/});
Returns:
"A test of horrible
Multi-line strings!"
Notes:
Edits:
2/2/2014 - changed to not mess with the Function prototype at all and use the name heredoc instead.
5/26/2017 - updated whitespace to reflect modern coding standards.