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

theredoc

v1.0.0

Published

A string template tag to strip leading indents from your heredoc-style multi-line strings

Downloads

347,929

Readme

theredoc

A little tag function that strips leading indent from multi-line ES string templates. It also strips the first and last lines if they're just whitespace.

Install

$ npm install theredoc

Usage

const theredoc = require('theredoc')

console.log(theredoc`
  I want to write multipline lines
  but don't want to mess up my indenting.
    Ok?
`)

Will output:

I want to write multipline lines
but don't want to mess up my indenting.
  Ok?

Questions

Why should I use this:

Because good messages are often long messages, but out-of-the-box, ES template strings don't format multi-line error and console messages gracefully.

If you're familiar with """-style heredocs from CoffeeScript or the <<~-style heredoc from Ruby 2.3, you might appreciate being able to write multi-line strings with an indentation level that matches the surrounding code listing without indenting the resulting string itself.

For example, if you want to print a multi-line error message without superfluous indentation in the following situation, an unadorned template string would have to be outdented awkwardly:

        //...
      } catch (e) {
        console.error(`
Something bad happened.

  Message: ${e.message}
        `)
      }
      //...

Additionally, to format the string neatly at all in the above will also add a extraneous leading and trailing line containing nothing but whitespace.

Instead, theredoc lets you write this:

        //...
      } catch (e) {
        console.error(theredoc`
          Something bad happened.

            Message: ${e.message}
        `)
      }
      //...

And the resulting console output will be:

Something bad happened.

  Message: LOL errors

With the leading 10 spaces stripped from each line and (since they only contain whitespace) stripping the first and last lines of the template string.

If you still don't think this is nifty, I don't know what to tell you!

Why not use common-tags?

The very cool and fancy library common-tags can accomplish the same thing with the stripIndent function it exports, however there are a few downsides:

  • It will trim all leading and trailing whitespace, which one might want to preserve
  • It has a runtime dependency on babel-runtime, which drastically increases the size of its install and, because babel-runtime depends on core-js, sets a global object that might be more appropriate for an app to set than an intermediate lib
  • While common-tags is really spiffy and you should check it out to learn about all the cool transforms you can do, 99.9% of the time I really just want indent-stripping heredocs

What about tabs (\t)?**

Theredoc only deals with space character indentation, sorry!

A pull request to support tabs would be considered if (a) it didn't drastically increase the module's complexity, and (b) deferred to counting spaces in the event that the given string had some lines indented with spaces and other with \t tabs.