Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

es6 multiline template strings with no new lines and allow indents

Been using es6 more and more for most work these days. One caveat is template strings.

I like to limit my line character count to 80. So if I need to concatenate a long string, it works fine because concatenation can be multiple lines like this:

const insert = 'dog'; const str = 'a really long ' + insert + ' can be a great asset for ' +   insert + ' when it is a ' + dog; 

However, trying to do that with template literals would just give you a multi-line string with ${insert} placing dog in the resulting string. Not ideal when you want to use template literals for things like url assembly, etc.

I haven't yet found a good way to maintain my line character limit and still use long template literals. Anyone have some ideas?

The other question that is marked as an accepted is only a partial answer. Below is another problem with template literals that I forgot to include before.

The problem with using new line characters is that it doesn't allow for indentation without inserting spaces into the final string. i.e.

const insert = 'dog'; const str = `a really long ${insert} can be a great asset for\   ${insert} when it is a ${insert}`; 

The resulting string looks like this:

a really long dog can be a great asset for  dog when it is a dog 

Overall this is a minor issue but would be interesting if there was a fix to allow multiline indenting.

like image 702
Geuis Avatar asked Nov 18 '16 08:11

Geuis


People also ask

How do you break a line in template literal?

To escape a backtick in a template literal, put a backslash ( \ ) before the backtick.

What is string interpolation in ES6?

String interpolation is a new feature of ES6, that can make multi-line strings without the need for an escape character. We can use apostrophes and quotes easily that they can make our strings and therefore our code easier to read as well.

How do I create a multiline string in JavaScript?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

What is ${} JavaScript?

${} is a placeholder that is used in template literals. You can use any valid JavaScript expression such as variable, arithmetic operation, function call, and others inside ${}. The expression used inside ${} is executed at runtime, and its output is passed as a string to template literals.


1 Answers

Two answers for this problem, but only one may be considered optimal.

Inside template literals, javascript can be used inside of expressions like ${}. Its therefore possible to have indented multiline template literals such as the following. The caveat is some valid js character or value must be present in the expression, such as an empty string or variable.

const templateLiteral = `abcdefgh${''   }ijklmnopqrst${''   }uvwxyz`;  // "abcdefghijklmnopqrstuvwxyz" 

This method makes your code look like crap. Not recommended.

The second method was recommended by @SzybkiSasza and seems to be the best option available. For some reason concatenating template literals didn't occur to me as possible. I'm derp.

const templateLiteral = `abcdefgh` +   `ijklmnopqrst` +   `uvwxyz`;  // "abcdefghijklmnopqrstuvwxyz" 
like image 108
Geuis Avatar answered Sep 30 '22 14:09

Geuis