Remove indentation from every line of a given string. Optionally, provide your own indent to control the amount of indentation to remove. If you don’t provide an indent, the amount of indentation to remove is determined by the first non-empty line of the given string.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Thank you for your recent purchase. Your order details are below:
Order ID: ${
constorderId:12345
orderId}
Product: ${
constproductName:"Widget"
productName}
Quantity: ${
constquantity:2
quantity}
If you have any questions, please contact our support team.
Best regards,
The Support Team
`
// => `Hello, JohnDoe!
//
// Thank you for your recent purchase. Your order details are below:
// Order ID: 12345
// Product: Widget
// Quantity: 2
//
// If you have any questions, please contact our support team.
//
// Best regards,
// The Support Team`
Multi-line embedded strings
When embedding a string with dedent, you don’t have to worry about the indentation of the embedded string. For example, if you have an array where each item needs its own line, just join it with join('\n') and dedent will make sure it all works out.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Adds all the elements of an array into a string, separated by the specified separator string.
@param ― separator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.
join('\n')}
`
// => 'My List:\n one\n two\n three'
Spacing issues?
There’s a common use case of building up a long string of “paragraphs” (for lack of a better word) using if conditions and dedent. If you can’t get an empty line to appear between two paragraphs, you’re not alone and this section is for you.
Since dedent strips the first and last empty line of a given string, your dedented string has no spacing around it by default. You might try to include an empty line at the start of each paragraph to achieve the desired spacing, like the following example.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
The empty line above “He was so confused” was intended to separate the paragraph from the previous one. But as you can see, it didn’t work. The empty line you added was appended to the last line of the previous paragraph (“who had a lot of trouble”) instead of being a separate line.
Here are 3 possible solutions for this issue. Pick your favorite:
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.
Remove indentation from a string. The given string is expected to
be consistently indented (i.e. the leading whitespace of the first
non-empty line is the minimum required for all non-empty lines).
If the indent argument is nullish, the indentation is detected
from the first non-empty line. Detection is cheap and robust for
most use cases, so you should only set an explicit indent if
necessary.