normalize-indents
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

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

Package Sidebar

Install

npm i normalize-indents

Weekly Downloads

2

Version

0.1.2

License

MIT

Unpacked Size

5.56 kB

Total Files

4

Last publish

Collaborators

  • nykol4z