How do you deal with leading/trailing line breaks in template literals?
See original GitHub issueThis would be my main use case, but strip-indent
only literally strips the indentation.
const fire = stripIndent(`
THIS IS FINE.
I'M OK WITH THE EVENTS UNFOLDING CURRENTLY.
`);
// => '\nTHIS IS FINE.\nI'M OK WITH THE EVENTS UNFOLDING CURRENTLY.\n'
I’m currently using this instead of strip-indent
but maybe you have a better solution (or can extend strip-indent
)
const fire = `
THIS IS FINE.
I'M OK WITH THE EVENTS UNFOLDING CURRENTLY.
`.replace(/(\n|\b)\t+/g, '$1').trim(); // Strips *all* indentation
// => 'THIS IS FINE.\nI'M OK WITH THE EVENTS UNFOLDING CURRENTLY.'
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Template Strings ES6 prevent line breaks - javascript
Perform these code line breaks using concatenations: var string = `As all string substitutions in Template Strings are JavaScript` + `expressions, ...
Read more >Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, ... including line breaks and other whitespace characters.
Read more >How to Create Multi-Line String with Template Literals
It's super easy now to create multi-line strings with Template Literals in JavaScript. No more messy string line breaks. Yay, ES6!
Read more >How to write multi-line strings in template literals
Example 2: If you use double/single quote to write multiline string then we use the newline character (\n). Use an extra backslash ...
Read more >How to Create Multi-Line String with Template Literals in ...
With the arrival of Template Literals, it's finally super easy to produce multi-line strings. Previously, we had to use the \n or separate...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sindresorhus @fregante I created a proposal to extend es6 template literals to allow for triple-backtick delimited literals that automatically strip margins: https://github.com/mmkal/proposal-multi-backtick-templates
It’d be great to get any feedback based on usage of this library on how you think things like this should be handled. My hope is for the default behaviour to be “what you want” for the majority of cases, therefore avoiding the need for runtime string formatting as much as possible.
Isn’t this request equivalent to outdent?