New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

string-dedent

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-dedent

De-indents (dedents) passed in strings

  • 3.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
increased by10.03%
Maintainers
1
Weekly downloads
 
Created
Source

string-dedent

De-indents (dedents) passed in strings

Removes the leading whitespace from each line, allowing you to break the string into multiple lines with indentation. If lines have an uneven amount of indentation, then only the common whitespace is removed.

The opening and closing lines (which contain the ` marks) must be on their own line. The opening line must be empty, and the closing line may contain whitespace. The opening and closing line will be removed from the output, so that only the content in between remains.

const dedent = require('string-dedent');

function example() {
  console.log('Outputs:');
  console.log(dedent`
    This line will appear without any indentation.
      * This list will appear with 2 spaces more than previous line.
      * As will this line.

    Empty lines (like the one above) will not affect the common indentation.
  `);
}
example();
Outputs:
This line will appear without any indentation.
  * This list will appear with 2 spaces more than previous line.
  * As will this line.

Empty lines (like the one above) will not affect the common indentation.

Installation

npm install string-dedent

Usage

The most common way to dedent is to use it as a tagged template literal. It supports expression interpolation, where the expressions will not affect the dedenting:

const exp = 'expressions';
const threeSpaces = '   ';
console.log('Outputs:');
console.log(dedent`
  This supports ${exp} as you would expect.

  Only whitespace that appears here inside the tagged template literal
  will be dedented.
  ${threeSpaces}<- expression whitespace will not be removed
`);

/*
Outputs:
This supports expressions as you would expect.

Only whitespace that appears here inside the tagged template literal
will be dedented.
   <- expression whitespace will not be removed
*/

If you need to use a tagged template literal like html, you can wrap the tag with dedent:

// Regular html usage:
const html = require('lit-html');
render(container, html`
  <div>The leading whitespace before this div tag will create a Text node in the output...</div>
`);

// Wrapped html usage:
const html = dedent(require('lit-html'));
render(container, html`
  <div>Leading whitespace before this div tag will not make it to HTML</div>
`);

Additionally, you may also call it like a function. This allows you to interpolate expressions into your string, and have the full string dedented.

const threeSpaces = '   ';
const str = `
  Used as a function
${threeSpaces}<- expression whitespace will be removed
`;
console.log('Outputs:');
console.log(dedent(str));

/*
Outputs:
Used as a function
 <- expression whitespace will be removed
*/

Keywords

FAQs

Package last updated on 01 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc