What is dedent-js?
The dedent-js npm package is used to remove the common leading indentation from multi-line strings. This is particularly useful for maintaining readable code while working with template literals in JavaScript.
What are dedent-js's main functionalities?
Basic Dedentation
This feature allows you to remove the common leading whitespace from each line in a multi-line string, making it easier to maintain readable code.
const dedent = require('dedent-js');
const str = dedent(`
This is a multi-line string.
Each line will be dedented.
`);
console.log(str);
Dedentation with Variables
This feature demonstrates how dedent-js can be used with template literals that include variables, ensuring that the resulting string is properly formatted.
const dedent = require('dedent-js');
const name = 'John';
const str = dedent(`
Hello, ${name}!
Welcome to the dedent-js package.
`);
console.log(str);
Dedentation with Nested Templates
This feature shows how dedent-js handles nested template literals, ensuring that all levels of the template are properly dedented.
const dedent = require('dedent-js');
const inner = dedent(`
This is an inner template.
It will also be dedented.
`);
const outer = dedent(`
This is an outer template.
${inner}
`);
console.log(outer);
Other packages similar to dedent-js
dedent
The dedent package is another popular option for removing indentation from multi-line strings. It offers similar functionality to dedent-js, allowing you to maintain readable code while working with template literals.
strip-indent
The strip-indent package is used to strip leading whitespace from each line in a string. It is similar to dedent-js but focuses solely on removing leading whitespace without additional features.
outdent
The outdent package provides a similar functionality to dedent-js, allowing you to remove leading indentation from multi-line strings. It also offers additional features like custom indentation levels.
dedent-js
Removes indentation from multiline strings. Works with both tabs and spaces.
Installation
$ npm install dedent-js
Usage
expect(dedent`Line #1
Line #2
Line #3`).to.equal('Line #1\nLine #2\nLine #3');
expect(
dedent`
Line #1
Line #2
Line #3
`
).to.equal('Line #1\nLine #2\nLine #3');
expect(
dedent`
Line #1
Line #2
Line #3
`
).to.equal('\nLine #1\nLine #2\nLine #3\n');
expect(
dedent`
Line #1
Line #2
Line #3
`
).to.equal('Line #1\n\tLine #2\n\t\tLine #3');
expect(
function () {
return dedent`
Line #1
Line #2
Line #3
`;
}()
).to.equal('Line #1\nLine #2\nLine #3');
expect(
dedent`
\tLine #1
\tLine #2
\tLine #3
`
).to.equal('Line #1\nLine #2\nLine #3');
License
Copyright (c) 2015 Martin Kolárik. Released under the MIT license.