normalize-indents
v0.1.2
Published
Removes unwanted indentation from template literals strings.
Downloads
5
Maintainers
Readme
normalize-indents
Removes unwanted indentation from template literals strings.
Table of Contents
Background
Template literals are a great addition to JavaScript, allowing (between other features) the possibility to span your string between multiple lines. Unfortunately, this may lead to unwanted results regarding indentation.
Let's say you have the following code:
console.log(`
Hello
World
`);
/* CONSOLE OUTPUT
⏎
··Hello⏎
··World⏎
*/
Notice that the console is logging some unwanted new lines (represented with ⏎) and indents (represented with ·).
By using normalizeIndents the result is exactly what you'd expect:
console.log(
normalizeIndents(`
Hello
World
`)
);
/* CONSOLE OUTPUT
Hello⏎
World
*/
It's important to understand that this function won't simply remove all indentation. Instead, it will do it in a smart way, leaving only the indents that you actually want.
console.log(
normalizeIndents(`
Hello
World
`)
);
/* CONSOLE OUTPUT
Hello⏎
··World
*/
Install
npm i normalize-indents
Usage
// Import the package
import normalizeIndents from 'normalize-indents';
// Fix the indentation of the string
const correctedString = normalizeIndents(`
Lorem ipsum
Dolor sit amet
`);
// Use the string anywhere
console.log(correctedString);
/* CONSOLE OUTPUT
Lorem ipsum⏎
⏎
··Dolor sit amet
*/
Contributing
PRs accepted.
License
MIT © Nicolas Britos