Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jridgewell/string-dedent
De-indents (dedents) passed in strings
https://github.com/jridgewell/string-dedent
Last synced: about 2 months ago
JSON representation
De-indents (dedents) passed in strings
- Host: GitHub
- URL: https://github.com/jridgewell/string-dedent
- Owner: jridgewell
- Created: 2020-05-30T11:00:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-09-27T10:07:41.000Z (3 months ago)
- Last Synced: 2024-10-19T18:30:54.456Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 277 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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.```js
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();
``````text
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
```sh
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:```js
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`:
```js
// Regular html usage:
const html = require('lit-html');
render(container, html`
The leading whitespace before this div tag will create a Text node in the output...
`);// Wrapped html usage:
const html = dedent(require('lit-html'));
render(container, html`
Leading whitespace before this div tag will not make it to HTML
`);
```Additionally, you may also call it like a function. This allows you to
interpolate expressions into your string, and have the full string dedented.```js
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
*/
```