Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

undent

twitchbronbron7.7kMIT1.0.0TypeScript support: included

Smartly removes leading indentation from multiline strings

readme

undent

Remove indentation from multi-line strings.

build status coverage status monthly downloads npm version license

Installation

npm

npm install undent -g

Usage

import undent from 'undent';
//use as a function
const first = undent(`multi-line\n\tindented\nstring`); // "multi-line\n\t\nstring"

//use as a template literal tag
const second = undent`
    multi-line
        indented
    string
`; // "multi-line\n    indented\nstring"

The algorithm

undent finds the line with the least amount of indentation (excluding whitespace-only lines), and then removes that amount of indentation from every line.

For example:

const text = undent`
        two levels in
    one level in
                three levels in
`;
console.log(text);

prints

    two levels in
one level in
            three levels in

Features

  • remove leading indentation based on the smallest indentation found for all lines (excluding whitespace-only lines)
  • trim the end of every line
  • remove leading/trailing newlines
  • retain existing newline characters (\r or \r\n)

Changelog

Click here to view the changelog.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

1.0.0 - 2025-06-03

Changed

  • major version bump to v1.0.0 since this project has been deemed stable
  • chore: fix some outdated devDependencies

0.1.0 - 2022-04-25

Added

  • Initial release