indent-to-first-line

0.1.0 • Public • Published

indent-to-first-line

NPM Version codecov Build Status DUB Node Security

A simple utility function to make working with multiline strings defined in template strings much easier.

Install

yarn add indent-to-first-line

or

npm install indent-to-first-line

Usage

Template strings are great because you can easily define multiline strings and format them in a visually pleasing way. The problem comes when you want to work with them, for example you want to declare an expected string and test it for equality:

const example = `This is a string
                  - here is an indented line
                  - here is another
                    - and here is a doubly indented line`

Although this string looks like the string you might be expecting, when you try and compare it, you will realise that there are lots of additional spaces within the string. If you were to replace each space with an underscore you would see:

;`This is a string
__________________- here is an indented line
__________________- here is another
____________________- and here is a doubly indented line`

Which is probably not what you want.

indent-to-first-line helps you solve this problem by trimming the leading spaces of a string away so the string is how you expect. To do this it needs you to do one critical thing - you need to start the first line of the string on a new line so that it can detect the number of spaces before the first line and adjust the others accordingly:

const example = indentToFirstLine(
  `This is a string
                  - here is an indented line
                  - here is another
                    - and here is a doubly indented line`
)

This will give you a string like this:

;`This is a string
  - here is an indented line
  - here is another
    - and here is a doubly indented line`

This first empty line will be removed, as will any trailing lines that consist only of whitespace, so the following will result in exactly the same string:

const example = indentToFirstLine(
  `This is a string
                  - here is an indented line
                  - here is another
                    - and here is a doubly indented line
                `
)

Note: If you don't put the first line of chars on a new line, the function will have no effect as there will be no leading whitespace there, meaning it cannot decide how much whitespace to remove from subsequent lines.

Maintainance

Release

yarn run publish:patch

or

yarn run publish:minor

or

yarn run publish:major

Readme

Keywords

none

Package Sidebar

Install

npm i indent-to-first-line

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

11.1 kB

Total Files

9

Last publish

Collaborators

  • undistraction