question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you deal with leading/trailing line breaks in template literals?

See original GitHub issue

This 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:open
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
mmkalcommented, Jun 1, 2020

It was such a missed opportunity how template literals were designed. In Swift, this does exactly what you want:

foo(
	"""
	Foo
	Bar
	"""
)
let foo = """
		Foo
		"""

@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.

1reaction
papbcommented, May 2, 2020

Isn’t this request equivalent to outdent?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found