What is redent?
The npm package 'redent' is used to modify the indentation of a string. It can remove excess indentation and add desired indentation to each line of a given text. This is particularly useful when working with multi-line strings in programming environments where indentation matters, such as in source code or markdown files.
What are redent's main functionalities?
Remove excess indentation
This feature removes excess indentation from a text. The example shows how to use redent to dedent a string to the least indented line.
const redent = require('redent');
const text = ' foo\n bar';
const dedentedText = redent(text, 0);
console.log(dedentedText); // 'foo\n bar'
Add indentation
This feature adds a specified number of spaces to the beginning of each line in the text. The example demonstrates adding 4 spaces of indentation to each line of a string.
const redent = require('redent');
const text = 'foo\nbar';
const indentedText = redent(text, 4);
console.log(indentedText); // ' foo\n bar'
Other packages similar to redent
indent-string
Similar to redent, 'indent-string' allows adding or modifying the indentation of a string. However, it focuses more on adding indentation rather than both adding and removing like redent.
strip-indent
This package is similar to the dedenting aspect of redent. 'strip-indent' is used to remove the leading whitespace from every line in a string, which is useful for cleaning up multi-line strings.
redent
Strip redundant indentation and indent the string
Install
$ npm install --save redent
Usage
const redent = require('redent');
redent('\n foo\n bar\n', 1);
API
redent(input, [count], [indent])
input
Type: string
count
Type: number
Default: 0
How many times you want indent
repeated.
indent
Type: string
Default: ' '
String to use for the indent.
License
MIT © Sindre Sorhus