Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fregante opened this issue Apr 6, 2020 · 9 comments
Open

Comments

@fregante
Copy link

fregante commented Apr 6, 2020

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.'
@sindresorhus
Copy link
Owner

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
Copy link
Owner

Back to your request, I'm willing to add an option for this. Any suggestion what it should be called?

I think the only thing missing is trimming the string before stripping indentation.

@fregante
Copy link
Author

fregante commented Apr 6, 2020

trim: true would be nice and short but it might sound like it will also trim any initial indentation that's not currently stripped.

trimNewLines: true is more exact

const fire = stripIndent(`
		THIS IS FINE.
	I'M OK WITH THE EVENTS UNFOLDING CURRENTLY.
`, {trimNewLines: true});

// => '\tTHIS IS FINE.\nI'M OK WITH THE EVENTS UNFOLDING CURRENTLY.'

@sindresorhus
Copy link
Owner

trimNewLines: true is more exact

Not exact enough. It doesn't trim all newlines, just the leading/trailing newlines. I guess trimLeadingTrailingNewlines is too verbose for you?

@fregante
Copy link
Author

fregante commented Apr 6, 2020

trimCodeStyleHelpingLeadingAndTrailingNewLines

A bit. Autocomplete cam help but there's still too much room for error/typos.

@sindresorhus
Copy link
Owner

  • trimNewlinesAround
  • trimSurroundingNewlines
  • trimEnclosingNewlines

@fregante
Copy link
Author

fregante commented Apr 6, 2020

For the record, common-tags does a full trim by default (all whitespace is trimmed):
common-tags

However it trims even some indentation that shouldn't be, like the first one

common-tags full

@papb
Copy link

papb commented May 2, 2020

Isn't this request equivalent to outdent?

@mmkal
Copy link

mmkal commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants