An open API service indexing awesome lists of open source software.

https://github.com/akd-io/aldent

An al dente indentation tag function
https://github.com/akd-io/aldent

dedent multi-line-string tag template-literal

Last synced: 4 months ago
JSON representation

An al dente indentation tag function

Awesome Lists containing this project

README

          

# 🍝 aldent

[![npm](https://img.shields.io/npm/v/aldent)](https://www.npmjs.com/package/aldent)

Aldent banner with usage example

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

## Usage

### Installation

```sh
npm install aldent
```

### Removing superfluous indentation

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

```ts
import aldent from "aldent";

aldent`


Hello world

This is some indented text


`;
```

Resolves to:

```


Hello world

This is some indented text


```

### Indentation during string interpolation

`aldent` also maintains correct indentation during string interpolation:

```ts
const innerElements = `a
b
c`;

aldent`


${innerElements}

`;
```

Resolves to:

```


a
b
c

```

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:

```ts
const innerElements = `a
b
c`;

aldent`


${innerElements}

`;
```

Resolving to:

```


a
b
c

```

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}` ``:

```ts
const innerElements = `
a
b
c
`;

aldent`


${aldent(innerElements)}

`;
```

Resolves to:

```


a
b
c

```

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](#usage).

An example of `dedent`'s behavior:

```ts
const innerElements = `a
b
c`;

dedent`


${innerElements}

`;
```

Resolves to:

```


a
b
c

```

That is, the inner elements are not indented correctly.

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

```


a
b
c

```

### `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:

```ts
endent`



`;
```

resolves to

```

```

whereas

```ts
aldent`



`;
```

resolves to

```

```

## License

[MIT](LICENSE)