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

@olsonpm/tedent

v0.2.2

Published

normalizes template string indentation like you'd expect

Downloads

4

Readme

Tedent

Keep your multi-line templated strings lookin' good :sunglasses:

Table of Contents

What is it?

  • A function similar to dedent just with different semantics

What does the name stand for?

  • Template string
  • indentation

names are hard

Why create it?

  • dedent didn't handle the following case like I wanted
//
// any multi-line indented string will do, but stringifying an object is the
//   common case for me
//
const boroughs = ['Brooklyn', 'Manhattan'],
  boroughsString = JSON.stringify(boroughs, null, 2)

console.log(
  dedent(`
    New York boroughs
    ${boroughs}
  `)
)

/*
expected:
New York boroughs
[
  "Brooklyn",
  "Manhattan"
]

actual:
New York boroughs
  [
"Brooklyn",
"Manhattan"
]
*/

Simple Usage

import tedent from 'tedent'

console.log(
  tedent(`
    This will be indented
      as you expect
  `)
)

// writes:
// This will be indented
//   as you expect

How the indentation works

The indentation logic is fairly convoluted in order to make the following work

const jstring = anObject => JSON.stringify(anObject, null, 2)

console.log(
  tedent(`
    header

      object 1: ${jstring(object1)}

      object 2: ${jstring(object2)}
  `)
)

//---------
// outputs
//---------
// header
//
//   object 1: {
//     ...properly indented object1 contents...
//   }
//
//   object 2: {
//     ...properly indented object2 contents...
//   }
//

Because the indentation logic is both young and convoluted, please refer to the code and tests for details. The library is not that big and if you have any questions please create a github issue.

Important Usage Notes

  • First of all, this library doesn't handle tabs. I will accept a PR with support

  • Secondly, if you always use tedent like the following

    tedent(`
      at least one line
    `)

    then you shouldn't run into any issues. However we all know input can be tricky so tedent has a few edge-cases built-in as well as input requirements

edge-cases and input requirements

  • if the first argument is anything but undefined or typeof 'string' then an error will be thrown
  • if you pass undefined an empty string is returned
  • if you pass a string with three or more lines, then
    • the first and last lines must contain only whitespace
    • the second line must contain a non-whitespace character
    • an error will be thrown if the above two conditions are not met
  • if you pass a string with fewer than 3 lines
    • if they only contain whitespace then an empty string is returned
    • otherwise an error is thrown
  • finally, all trailing whitespace from the result is trimmed

I didn't feel it necessary to explain the reasons for my choices in handling edge-cases, but if you have questions please ask via github issues.

Test

./run test