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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jrolfs/outdent

v0.4.0

Published

Remove leading indentation from ES6 template literals.

Downloads

5

Readme

outdent

Removes leading indentation from ES6 template strings

Build Status typings included

ES6 template strings are great, but they preserve everything between the backticks, including leading spaces. Sometimes I want to indent my template literals to make my code more readable without including all those spaces in the string.

Outdent will remove those leading spaces, as well as the leading and trailing newlines.

import outdent from 'outdent';
const markdown = outdent`
    # My Markdown File

    Here is some indented code:

        console.log("hello world!");
`;
console.log(markdown);
fs.writeFileSync('output.md', markdown);

The contents of output.md do not have the leading indentation:

# My Markdown File

Here is some indented code:

    console.log("hello world!");

As a Javascript string:

var markdown = '# My Markdown File\n' +
               '\n' +
               'Here is some indented code:' +
               '\n' +
               '    console.log("hello world!");';

You can pass options to outdent to control its behavior. They are explained in Options.

const output = outdent({trimLeadingNewline: false, trimTrailingNewline: false})`
    Hello world!
`;
assert(output === '\nHello world!\n');

You can explicitly specify the indentation level by passing outdent as the first interpolated value. Its position sets the indentation level and it is removed from the output.

const output = outdent`
      ${outdent}
        Yo
    12345
          Hello world
`;
assert(output === '  Yo\n345\n    Hello world');

Note: ${outdent} must be alone on its own line without anything before or after it. It cannot be preceded by any non-whitespace characters. If these conditions are not met, outdent will follow normal indentation-detection behavior.

Options

trimLeadingNewline

Default: true

trimTrailingNewline

Default: true

Whether or not outdent should remove the leading and/or trailing newline from your template string. For example:

var s = outdent({trimLeadingNewline: false})`
    Hello
`;
assert(s === '\nHello');
s = outdent({trimTrailingNewline: false})`
    Hello
`
assert(s === 'Hello\n');
s = outdent({trimLeadingNewline: false, trimTrailingNewline: false})`
    
`;
assert(s === '\n\n');

Gotchas

Start on a new line

Start the contents of your template string on a new line after the opening backtick. Otherwise, outdent has no choice but to detect indentation from the second line, which does not work in all situations.

// Bad
const output = outdent `* item 1
                          * sub-item
`;
// output === '* item 1\n* sub-item'; Indentation of sub-item is lost

// Good
const output = outdent `
    * item 1
      * sub-item
`;

Spaces and tabs

Spaces and tabs are treated identically. outdent does not verify that you are using spaces or tabs consistently; they are all treated as a single character for the purpose of removing indentation. Spaces, tabs, and smart tabs should all work correctly provided you use them consistently.

TypeScript declarations

This module includes TypeScript type declarations so you will get code completion and error-checking without installing anything else.

Questions or Bugs?

File an issue on Github: https://github.com/cspotcode/outdent/issues