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 🙏

© 2025 – Pkg Stats / Ryan Hefner

heredoc-ts

v1.0.0

Published

A heredoc implementation in TypeScript with support for nested heredocs.

Downloads

845

Readme

heredoc-ts

A TypeScript heredoc implementation with smart indentation handling and interpolation support.

Installation

npm install heredoc-ts

Usage

import { heredoc } from "heredoc-ts"

const message = heredoc`
  Hello, world!
  This is a multiline string
  with proper indentation handling.
`

console.log(message)
// "Hello, world!\nThis is a multiline string\nwith proper indentation handling."

Features

Smart Indentation

The heredoc function automatically detects and removes the common leading indentation from all lines:

const code = heredoc`
  function example() {
    console.log("Hello world")
  }
`
// `function example() {\n  console.log("Hello world")\n}`

Interpolation Support

Supports template literal interpolation with intelligent handling of multiline values. This automatically handles the leading spacing (i.e., each newline in the template value has the leading spacing prepended).

const items = ["apple", "banana", "orange"]
const list = heredoc`
  Shopping list:
  ${items.map((item) => `- ${item}`).join("\n")}
`
// "Shopping list:\n- apple\n- banana\n- orange"

Nested Heredocs

You can nest heredoc calls for complex formatting. This will process the indentation of the nested value first, allowing for indented code for readability while maintaining consistent outputs without excessive leading spaces.

const nested = heredoc`
  Outer content
  ${heredoc`
    Inner content
    with its own
    indentation
  `}
  Back to outer
`
// "Outer content\nInner content\nwith its own\nindentation\nBack to outer"

Type Safety

Fully typed with TypeScript, supporting string, number, boolean, null, and undefined interpolations.

API

heredoc(strings: TemplateStringsArray, ...values: Stringish[]): string

A template tag function that processes multiline strings:

  • Removes leading and trailing blank lines
  • Detects the smallest common indentation and strips it from all lines
  • Preserves relative indentation between lines
  • Intelligently handles interpolated values with newlines

Examples

Basic Usage

const text = heredoc`
  This text starts with proper indentation.
  It maintains relative indentation.
    Like this indented line.
`

With Interpolation

const name = "Alice"
const greeting = heredoc`
  Hello, ${name}!
  Welcome to our application.
`

Complex Interpolation

const data = ["one", "two", "three"]
const html = heredoc`
  <ul>
    ${data.map((item) => `<li>${item}</li>`).join("\n")}
  </ul>
`

Development

# Install dependencies
pnpm install

# Run tests
pnpm test

# Build the package
pnpm build

# Run linting
pnpm lint

# Type checking
pnpm typecheck

License

Copyright © 2025 Corey Ward. Available under the MIT license.

Credits

This package was originally based on the now-archived package theredoc by Justin Searls. This package began as a TypeScript port, and has had several additions. Thank you, Justin!