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 🙏

© 2026 – Pkg Stats / Ryan Hefner

template-str

v1.1.0

Published

A lightweight, type-safe template string utility for JavaScript and TypeScript.

Readme

template-str

A lightweight, type-safe template string utility for JavaScript and TypeScript.

template-str lets you define string templates with named placeholders and render them with provided data. It also supports default values, escaping and configuring the behavior if a value is missing.


Features

  • Named placeholders using [placeholder] syntax
  • Case-sensitive matching ([Name] != [name])
  • Type-safe templates in TypeScript
  • Default values
  • Escaping for literal brackets (\[name\])
  • Configurable behavior for missing placeholders

Installation

npm install template-str

or for yarn

yarn add template-str

Usage

Basic usage

To start using template strings, just import the TemplateString class from the template-str package and instantiate it with your template. Then just call render() on your instance and provide data for the placeholders.

import { TemplateString } from "template-str";

const template = new TemplateString("Hello [name]!");
const result = template.render({ name: "John" });

console.log(result); // "Hello John!"

Case-sensitive matching

Placeholder matching is case-sensitive by design, allowing you to pass different placeholders with the same semantic meaning, but different replacement values.

import { TemplateString } from "template-str";
const template = new TemplateString("Hello [Name] [name]");

template.render({ Name: "Alice", name: "Bob" }); // "Hello Alice Bob"

Default values

Default values allow you to specify fallback values before rendering. If you don't pass a value for a placeholder at render time, it'll be replaced with its default value if present

import { TemplateString } from "template-str";
const template = new TemplateString("Hello [name]", undefined, {
  name: "Guest",
});

template.render({}); // "Hello Guest"
template.render({ name: "Johnny" }); // "Hello Johnny"

The data provided to render() always overrides default values.

Options

replaceEmpty

The replaceEmpty option allows you to customize what happens when a placeholder value can't be found.

| Value | Behavior | | --------------- | -------------------------------------------------------------------- | | false (default) | The placeholder construct isn't replaced at all | | string | The placeholder construct is replaced with the provided string value |

import { TemplateString } from "template-str";

const templateReplaced = new TemplateString("Hello [name]", {
  replaceEmpty: "N/A",
});
templateReplaced.render({}); // "Hello N/A"

const template = new TemplateString("Hello [name]");
template.render({}); // "Hello [name]"

Escaping placeholders

To render a placeholder literal without replacing it, you can escape the template characters with backslashes.

import { TemplateString } from "template-str";

const templateReplaced = new TemplateString("Hello \\[name\\] [name]", {
  replaceEmpty: "N/A",
});
templateReplaced.render({ name: "Johnny" }); // "Hello [name] Johnny"

Supported placeholder characters

Placeholders may include

  • Lower and uppercase letters (a-z, A-Z)
  • Numbers (0-9)
  • Dashes and underscores (-, _)
import { TemplateString } from "template-str";

const template = new TemplateString("User [user_id-1] is [status_active]");

template.render({
  "user_id-1": 42,
  status_active: "online",
}); // "User 42 is online"

TypeScript support

template-str is fully typed and infers placeholder keys from the template string for a safer usage in TypeScript

import { TemplateString } from "template-str";

const template = new TemplateString("Hello [name] [age]");

// TypeScript infers the expected keys
template.render({
  name: "Alice",
  age: 30,
});

// Type error since "username" is not present in the template string
template.render({
  username: "Alice",
});

Contribution

Issues and pull requests are very welcome. Feel free to open a discussion in the linked repo, if you have any ideas or questions