Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ig3/dedent
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ig3/dedent
- Owner: ig3
- Created: 2024-08-04T22:22:45.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-08-04T22:29:47.000Z (5 months ago)
- Last Synced: 2024-10-08T09:03:50.891Z (3 months ago)
- Language: JavaScript
- Size: 36.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @ig3/dedent
A plain JavaScript CJS module remove excess indent from multi-line strings,
without dependencies.When using template strings to create multi-line strings in an indented
code block, to maintain indent within the code block, the lines of the
string template might also be indented, but this produces a string with too
much indent.`dedent(str)` can be used to remove such excess indent.
## install
```
$ npm install @ig3/dedent
```## usage
```
const dedent = require('@ig3/dedent');function getString () {
return `
This is a multi-line string.
It should be indented by two spaces.
But because the string template is in an indented code block,
with each line indented by four spaces,
to maintain indent within the code block,
each line is indented by six spaces.
Using dedent() removes the extra indentation.
`;
}console.log(dedent(getString()));
```This produces:
```
This is a multi-line string.
It should be indented by two spaces.
But because the string template is in an indented code block,
with each line indented by four spaces,
to maintain indent within the code block,
each line is indented by six spaces.
Using dedent() removes the extra indentation.
```It handles a mix of tabs and spaces in the indent, as long as the indent is
consistent from line to line. If some lines use tabs and others use spaces
or the order of tabs and spaces is variable, then the result will be
incorrect.Leading '\n' and trailing line empty except for the indent are removed.
If the last line is not empty except for the indent, a heuristic is used to
determine the indent: testing each line of the string to find the shortest
(number of characters) prefix of spaces and/or tabs, and this prefix is
removed. Scanning all lines of the string takes additional time and might
give unexpected results.Single-line strings (i.e. without any '\n' character) are returned
unchanged.Throws an exception if the passed value is not a string;