Skip to content

dedent

Remove indentation from a string

385 bytes

Usage

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.

import * as
import _
_
from 'radashi'
// Explicit indentation
import _
_
.
function dedent(text: string, indent?: string | null): string (+1 overload)
export dedent

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.

@seehttps://radashi.js.org/reference/string/dedent

@example

// This is indented with 4 spaces.
const input = `
Hello
World
`
// Explicit indentation
dedent(input, ' ')
// => ' Hello\n World\n'
// Detected indentation
dedent(input)
// => 'Hello\nWorld\n'
// Tagged template strings
const str = dedent`
Foo ${1 + 1}
Bar ${2 * 2}
`
// => 'Foo 2\nBar 4'

@version12.3.0

dedent
('\n Hello\n World!\n\n', ' ')
// => ' Hello\n World!\n'
// Detected indentation
import _
_
.
function dedent(text: string, indent?: string | null): string (+1 overload)
export dedent

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.

@seehttps://radashi.js.org/reference/string/dedent

@example

// This is indented with 4 spaces.
const input = `
Hello
World
`
// Explicit indentation
dedent(input, ' ')
// => ' Hello\n World\n'
// Detected indentation
dedent(input)
// => 'Hello\nWorld\n'
// Tagged template strings
const str = dedent`
Foo ${1 + 1}
Bar ${2 * 2}
`
// => 'Foo 2\nBar 4'

@version12.3.0

dedent
('\n Hello\n World!\n\n')
// => 'Hello\nWorld!\n'

Tagged template strings

When using dedent in a tagged template string, the indentation is always inferred.

// Real-world example: Email template
const
const emailTemplate: string
emailTemplate
=
import _
_
.
function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
export dedent

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.

@seehttps://radashi.js.org/reference/string/dedent

@example

// This is indented with 4 spaces.
const input = `
Hello
World
`
// Explicit indentation
dedent(input, ' ')
// => ' Hello\n World\n'
// Detected indentation
dedent(input)
// => 'Hello\nWorld\n'
// Tagged template strings
const str = dedent`
Foo ${1 + 1}
Bar ${2 * 2}
`
// => 'Foo 2\nBar 4'

@version12.3.0

dedent
`
Hello, ${
const userName: "JohnDoe"
userName
}!
Thank you for your recent purchase. Your order details are below:
Order ID: ${
const orderId: 12345
orderId
}
Product: ${
const productName: "Widget"
productName
}
Quantity: ${
const quantity: 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.

const
const items: string[]
items
= ['one', 'two', 'three']
const
const list: string
list
=
import _
_
.
function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
export dedent

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.

@seehttps://radashi.js.org/reference/string/dedent

@example

// This is indented with 4 spaces.
const input = `
Hello
World
`
// Explicit indentation
dedent(input, ' ')
// => ' Hello\n World\n'
// Detected indentation
dedent(input)
// => 'Hello\nWorld\n'
// Tagged template strings
const str = dedent`
Foo ${1 + 1}
Bar ${2 * 2}
`
// => 'Foo 2\nBar 4'

@version12.3.0

dedent
`
My List:
${
const items: string[]
items
.
Array<string>.join(separator?: string): string

Adds all the elements of an array into a string, separated by the specified separator string.

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

let
let story: string
story
=
import _
_
.
function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
export dedent

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.

@seehttps://radashi.js.org/reference/string/dedent

@example

// This is indented with 4 spaces.
const input = `
Hello
World
`
// Explicit indentation
dedent(input, ' ')
// => ' Hello\n World\n'
// Detected indentation
dedent(input)
// => 'Hello\nWorld\n'
// Tagged template strings
const str = dedent`
Foo ${1 + 1}
Bar ${2 * 2}
`
// => 'Foo 2\nBar 4'

@version12.3.0

dedent
`
There once was a programmer
who had a lot of trouble.
`
if (
function isLateAtNight(): boolean
isLateAtNight
()) {
let story: string
story
+=
import _
_
.
function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
export dedent

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.

@seehttps://radashi.js.org/reference/string/dedent

@example

// This is indented with 4 spaces.
const input = `
Hello
World
`
// Explicit indentation
dedent(input, ' ')
// => ' Hello\n World\n'
// Detected indentation
dedent(input)
// => 'Hello\nWorld\n'
// Tagged template strings
const str = dedent`
Foo ${1 + 1}
Bar ${2 * 2}
`
// => 'Foo 2\nBar 4'

@version12.3.0

dedent
`
He was so confused
that he couldn't even
tell what was going on.
`
}
let story: string
story
// =>
// There once was a programmer
// who had a lot of trouble.
// He was so confused
// that he couldn't even
// tell what was going on.

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:

  1. Add \n to your empty line.

    let story: string
    story
    +=
    import _
    _
    .
    function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
    export dedent

    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.

    @seehttps://radashi.js.org/reference/string/dedent

    @example

    // This is indented with 4 spaces.
    const input = `
    Hello
    World
    `
    // Explicit indentation
    dedent(input, ' ')
    // => ' Hello\n World\n'
    // Detected indentation
    dedent(input)
    // => 'Hello\nWorld\n'
    // Tagged template strings
    const str = dedent`
    Foo ${1 + 1}
    Bar ${2 * 2}
    `
    // => 'Foo 2\nBar 4'

    @version12.3.0

    dedent
    `
    \n
    He was so confused
    that he couldn't even
    tell what was going on.
    `
  2. Append the line breaks separately.

    let story: string
    story
    += '\n\n'
    let story: string
    story
    +=
    import _
    _
    .
    function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
    export dedent

    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.

    @seehttps://radashi.js.org/reference/string/dedent

    @example

    // This is indented with 4 spaces.
    const input = `
    Hello
    World
    `
    // Explicit indentation
    dedent(input, ' ')
    // => ' Hello\n World\n'
    // Detected indentation
    dedent(input)
    // => 'Hello\nWorld\n'
    // Tagged template strings
    const str = dedent`
    Foo ${1 + 1}
    Bar ${2 * 2}
    `
    // => 'Foo 2\nBar 4'

    @version12.3.0

    dedent
    `
    He was so confused
    that he couldn't even
    tell what was going on.
    `
  3. Include two empty lines at the start of each paragraph. (An unadvised solution as its intention is not the most clear)

    let story: string
    story
    +=
    import _
    _
    .
    function dedent(template: TemplateStringsArray, ...values: unknown[]): string (+1 overload)
    export dedent

    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.

    @seehttps://radashi.js.org/reference/string/dedent

    @example

    // This is indented with 4 spaces.
    const input = `
    Hello
    World
    `
    // Explicit indentation
    dedent(input, ' ')
    // => ' Hello\n World\n'
    // Detected indentation
    dedent(input)
    // => 'Hello\nWorld\n'
    // Tagged template strings
    const str = dedent`
    Foo ${1 + 1}
    Bar ${2 * 2}
    `
    // => 'Foo 2\nBar 4'

    @version12.3.0

    dedent
    `
    He was so confused
    that he couldn't even
    tell what was going on.
    `