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

aldent

v0.0.6

Published

An al dente indentation tag function

Downloads

7

Readme

🍝 aldent

npm

An al dente indentation tag function inspired by dedent and endent.

Usage

Installation

npm install aldent

Removing superfluous indentation

Use the aldent tag function to remove superfluous indentation from a string:

import aldent from "aldent";

aldent`
    <div>
      Hello world
      <span>
        This is some indented text
      </span>
    </div>
`;

Resolves to:

<div>
  Hello world
  <span>
    This is some indented text
  </span>
</div>

Indentation during string interpolation

aldent also maintains correct indentation during string interpolation:

const innerElements = `<span>a</span>
<span>b</span>
<span>c</span>`;

aldent`
  <div>
    ${innerElements}
  </div>
`;

Resolves to:

<div>
  <span>a</span>
  <span>b</span>
  <span>c</span>
</div>

Notice here, that the innerElements variable does not contain any indentation, but is correctly indented in the final output. The resulting indentation is based on the position of the ${innerElements} syntax.

If we were to indent the ${innerElements} code further, you would get this:

const innerElements = `<span>a</span>
<span>b</span>
<span>c</span>`;

aldent`
  <div>
        ${innerElements}
  </div>
`;

Resolving to:

<div>
      <span>a</span>
      <span>b</span>
      <span>c</span>
</div>

This example also shows that aldent knows nothing about HTML, or any other language for that matter. It does not prettify code. We are only manipulating strings here. As such it isn't bound to any language, making it a very versatile tool when it comes to code templating/generation.

Nested template strings

In the above examples, innerElements has not had any indentation. Had it been indented, you would need to use aldent on the inner template string as well.

Below we also showcase the aldent(foo) syntax, which is equivalent to calling aldent`${foo}`:

const innerElements = `
      <span>a</span>
      <span>b</span>
      <span>c</span>
`;

aldent`
  <div>
    ${aldent(innerElements)}
  </div>
`;

Resolves to:

<div>
  <span>a</span>
  <span>b</span>
  <span>c</span>
</div>

Again, the position of the ${aldent(innerElements)} syntax determines the indentation level of the nested aldent call.

Comparison to dedent and endent

To understand where aldent is coming from, let's first compare existing solutions dedent and endent.

dedent vs endent

endent was built on top of dedent to support correct indentation during string interpolation, as shown an example of earlier in the usage section.

An example of dedent's behavior:

const innerElements = `<span>a</span>
<span>b</span>
<span>c</span>`;

dedent`
  <div>
    ${innerElements}
  </div>
`;

Resolves to:

<div>
<span>a</span>
<span>b</span>
<span>c</span>
</div>

That is, the inner elements are not indented correctly.

Running the same code with endent, would give you correctly indented code:

<div>
  <span>a</span>
  <span>b</span>
  <span>c</span>
</div>

endent vs aldent

While aldent is dependency-free, endent depends on three other packages, dedent, fast-json-parse, and objectorarray.

endent uses these dependencies to parse JSON-values in the template string. aldent does not concern itself with json-formatting, sees this as a separate concern, and leaves it up to the user to create a wrapper function for this purpose if needed.

Apart from being dependency-free and leaving formatting up to the user, aldent differs from endent mainly by not stripping the leading spaces of the first not-purely-whitespace line of text.

For example:

endent`
      </a>
    </div>
  </div>
`;

resolves to

</a>
  </div>
</div>

whereas

aldent`
      </a>
    </div>
  </div>
`;

resolves to

    </a>
  </div>
</div>

License

MIT