npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dedent-preserving-indents

v0.2.0

Published

[![NPM Version](https://img.shields.io/npm/v/dedent-preserving-indents.svg)](https://www.npmjs.com/package/dedent-preserving-indents) [![codecov](https://img.shields.io/codecov/c/github/Undistraction/dedent-preserving-indents.svg)](https://codecov.io/gh/U

Downloads

37

Readme

dedent-preserving-indents

NPM Version codecov Build Status DUB Node Security

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

There is already a library called dedent which strips leading whitespace, but it doesn't preserve indentation relative to the line with the least leading whitespace, but this one does.

Install

yarn add dedent-preserving-indents

or

npm install dedent-preserving-indents

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 from the indentation which includes both the indentation to get the lines to line up and any deliberate indentation included in your string. If you were to replace each space with a 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.

This lib helps you solve this problem by trimming the leading spaces of a string away so the string is how you expect. This library will check all lines and find the line(s) with the least leading spaces. It will then remove that many spaces from all lines. This results in the line(s) with the least leading whitespace having no leading whitespace, and any other indented lines being indented relative to this/these line(s)s

const example = dedent(
                  `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

Important Caveats

You will probably have noticed that the first line is empty in the examples above. 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 for that line, meaning no leading whitespace will be removed from the other lines. Always put the first line on a newline.

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 = dedent(
                  `This is a string
                    - here is an indented line
                    - here is another
                      - and here is a doubly indented line
                `)

Maintainance

Release

yarn run publish:patch

or

yarn run publish:minor

or

yarn run publish:major