Wrap long template literal line to multiline without creating a new line in the string

2020/11/13 20:41 · javascript ·  · 0评论

In es6 template literals, how can one wrap a long template literal to multiline without creating a new line in the string?

For example, if you do this:

const text = `a very long string that just continues
and continues and continues`

Then it will create a new line symbol to the string, as interpreting it to have a new line. How can one wrap the long template literal to multiple lines without creating the newline?

If you introduce a line continuation (\) at the point of the newline in the literal, it won't create a newline on output:

const text = `a very long string that just continues\
and continues and continues`;
console.log(text); // a very long string that just continuesand continues and continues

This is an old one. But it came up. If you leave any spaces in the editor it will put them in there.

if
  const text = `a very long string that just continues\
  and continues and continues`;

just do the normal + symbol

if
  const text = `a very long string that just continues` +
  `and continues and continues`;

You could just eat the line breaks inside your template literal.

// Thanks to https://twitter.com/awbjs for introducing me to the idea
// here: https://esdiscuss.org/topic/multiline-template-strings-that-don-t-break-indentation

const printLongLine = continues => {
    const text = `a very long string that just ${continues}${''
                 } and ${continues} and ${continues}`;
    return text;
}
console.log(printLongLine('continues'));

EDIT: I've made an tiny NPM module with this utility. It works on web and in Node and I highly recommend it over the code in my below answer as it's far more robust. It also allows for preserving newlines in the result if you manually input them as \n, and provides functions for when you already use template literal tags for something else: https://github.com/iansan5653/compress-tag


I know I'm late to answer here, but the accepted answer still has the drawback of not allowing indents after the line break, which means you still can't write very nice-looking code just by escaping newlines.

Instead, why not use a tagged template literal function?

function noWhiteSpace(strings, ...placeholders) {
  // Build the string as normal, combining all the strings and placeholders:
  let withSpace = strings.reduce((result, string, i) => (result + placeholders[i - 1] + string));
  let withoutSpace = withSpace.replace(/\s\s+/g, ' ');
  return withoutSpace;
}

Then you can just tag any template literal you want to have line breaks in:

let myString = noWhiteSpace`This is a really long string, that needs to wrap over
    several lines. With a normal template literal you can't do that, but you can 
    use a template literal tag to allow line breaks and indents.`;

This does have the drawback of possibly having unexpected behavior if a future developer isn't used to the tagged template syntax or if you don't use a descriptive function name, but it feels like the cleanest solution for now.

Another option is to use Array.join, like so:

[
    'This is a very long string. ',
    'It just keeps going ',
    'and going ',
    'and going ',
    'and going ',
    'and going ',
    'and going ',
    'and going',
].join('')

Use the old and the new. Template literals are great but if you want to avoid lengthy literals so as to have compact lines of code, concatenate them and ESLint won't cause a fuss.

const text = `a very long string that just continues`
  +` and continues and continues`;
console.log(text);

Similar to Doug's answer this is accepted by my TSLint config and remains untouched by my IntelliJ auto-formatter:

const text = `a very long string that just ${
  continues
} and ${continues} and ${continues}`

The solution proposed by @CodingIntrigue is not working for me on node 7. Well, it works if I do not use a line continuation on the first line, it fails otherwise.

This is probably not the best solution, but it works without problems:

(`
    border:1px solid blue;
    border-radius:10px;
    padding: 14px 25px;
    text-decoration:none;
    display: inline-block;
    text-align: center;`).replace(/\n/g,'').trim();
本文地址:http://javascript.askforanswer.com/wrap-long-template-literal-line-to-multiline-without-creating-a-new-line-in-the-string.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!