Skip to content

dedent

Strips the common leading whitespace from every line of a multi-line string, and trims a single leading/trailing blank line if present.

Lets you write readable, indented multi-line strings in source code (typically template literals) without that indentation leaking into the output.

Available since v3.0.0

import { dedent } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { dedent } from 'helpers4/string';
dedent(str: string): string
ParameterTypeDescription
strstringThe string to dedent

string — The dedented string

Write readable multi-line strings without leaking source indentation

Section titled “Write readable multi-line strings without leaking source indentation”

Strips the common leading whitespace and the wrapping blank lines.

dedent(`
  Hello
    World
`)
// => 'Hello\n  World'

The minimum indentation across all lines is what gets removed

Section titled “The minimum indentation across all lines is what gets removed”

Lines with more indentation than the minimum keep their relative indent.

dedent('    a\n  b\n      c')
// => '  a\nb\n    c'

View source on GitHub