npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-dedent

v2.3.0

Published

TypeScript package which smartly trims and strips indentation from multi-line strings

Readme

TypeScript Dedent

codecov

TypeScript package which smartly trims and strips indentation from multi-line strings.

Usage Examples

import { dedent } from 'ts-dedent';

console.log(dedent`A string that gets so long you need to break it over
                    multiple lines. Luckily dedent is here to keep it
                    readable without lots of spaces ending up in the string
                    itself.`);

console.log(dedent`
  A string that gets so long you need to break it over
  multiple lines. Luckily dedent is here to keep it
  readable without lots of spaces ending up in the string
  itself.
`);
A string that gets so long you need to break it over
multiple lines. Luckily dedent is here to keep it
readable without lots of spaces ending up in the string
itself.

console.log(dedent`
  Leading and trailing lines will be trimmed, so you can write something like
  this and have it work as you expect:

    * how convenient it is
    * that I can use an indented list
        - and still have it do the right thing

  That's all.
`);
Leading and trailing lines will be trimmed, so you can write something like
this and have it work as you expect:

  * how convenient it is
  * that I can use an indented list
    - and still have it do the right thing

That's all.

console.log(dedent`
  Also works fine

  ${1}. With any kind of
  ${2}. Placeholders
`);
Also works fine

1. With any kind of
2. Placeholders

console.log(dedent(`
  Wait! I lied. Dedent can also be used as a function.
`);
Wait! I lied. Dedent can also be used as a function.

How it works

dedent runs the following steps on the string, in order:

  1. Strip a single leading line break. If the string starts with a newline (the common case when you put your first line of text below the opening backtick), that one newline is removed. A second blank line is kept, so you can intentionally start the output with an empty line.
  2. Strip a single trailing line break and its indentation. A trailing newline followed only by spaces/tabs is removed. This is what lets you put the closing backtick on its own indented line without adding a blank line to the result.
  3. Find the common indentation. For every line that contains actual content, dedent counts its leading spaces and tabs. The "common indentation" is the smallest of those counts — i.e. the line with the fewest leading whitespace characters. Blank lines are ignored, and a line that starts with a non-whitespace character counts as 0 (so if any content line has no indentation, nothing is stripped).
  4. Remove the common indentation from every line. Exactly that many leading whitespace characters are removed from the start of each line. Lines that were indented further keep the difference, so relative indentation (nested lists, code blocks, etc.) is preserved.
  5. Interpolate the placeholders. ${...} values are inserted. If a value is itself a multi-line string, every line after its first is re-indented to match the placeholder's position, so nested/multi-line values stay aligned.

So to answer the common question directly: it removes the indentation of the line with the fewest leading whitespace characters — not the first indented line — and applies that same removal to every line.

A few details worth knowing:

  • Indentation is counted in characters, where each tab and each space counts as one. Removal also works on raw characters, so mixing tabs and spaces in your leading whitespace can give surprising results — pick one and be consistent.
  • Only one leading and one trailing line break are trimmed. Additional blank lines at the very start or end are preserved.
// Lines indented by 4, 2, and 6 spaces. The minimum is 2, so 2 spaces are
// removed from every line:
console.log(dedent`
    four
  two
      six
`);
  four
two
    six

License

MIT

Based on

Changelog

See history for more details.