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

@jmcantrell/template-dialect

v0.1.0

Published

A class for creating simple template dialects

Readme

typescript-templater

A class for creating simple template dialects.

Usage

Browser or Deno

import Templater from "https://esm.sh/@jmcantrell/templater";

Node

npm install @jmcantrell/templater
import Templater from "@jmcantrell/templater";

Example

const dialect = new Templater("${", "}");
const greeting = dialect.compile("Hello, ${name}!");
console.log(greeting({ name: "you" })); // outputs: "Hello, you!"

Behavior

For this section, dialect is assumed to have the following definition:

const dialect = new Templater("${", "}");

The dialect above will render strings containing placeholders that look like ${name}:

const size = dialect.compile("${width}x${height}");
console.log(size(800, 600)); // outputs: "800x600"

Template strings with no placeholders will compile to a function that simply returns the string unchanged.

For example:

const greeting = dialect.compile("Hello!");

Is equivalent to:

const greeting = () => "Hello!";

As you might expect, multiple instances of the same placeholder will all be replaced:

const thrice = dialect.compile("${value} ${value} ${value}");
console.log(thrice({ value: "yeah!" })); // outputs: "yeah! yeah! yeah!"

When passing options to templates, any missing options will render those placeholders unchanged:

const greeting = dialect.compile("Hello, ${name}!");
console.log(greeting()); // outputs: "Hello, ${name}!"

Similarly, if any passed options don't have a corresponding placeholder, they will be ignored:

const greeting = dialect.compile("Hello, ${name}!");
console.log(greeting({ bogus: "foo" })); // outputs: "Hello, ${name}!"

You can give any placeholder a default value that will be used if no value is provided:

const greeting = dialect.compile("Hello, ${name=you}!");
console.log(greeting()); // outputs: "Hello, you!"

API

Creating a template dialect

const dialect = new Templater(prefix, suffix);

The constructor takes one argument that's expected to be an object with two properties, prefix and suffix.

The prefix parameter should be a string that will be the left side of a placeholder.

The suffix parameter should be a string that will be the right side of a placeholder.

For example, passing parameters like "{" and "}" will mean that placeholders in the template strings will be expected to look like {whatever}.

Compiling a template string

const compiled = dialect.compile(string);

Once a template dialect is created, the typical usage is compiling one or more template strings, and using those compiled templates to quickly render output many times over the life of your program. Compiling a template string means that all the hard work involved is only done once so that rendering is as quick as possible.

The returned compiled template is simply a function that accepts an object expected to contain placeholder values.

Rendering a template string without compilation

const rendered = dialect.render(string, options);

If you're testing a template string or know that it will only be used once, you can render a string directly, skipping the compile step.

For example, the following two pieces of code produce the same output string:

const greeting = dialect.compile("Hello, ${name}!");
console.log(greeting({ name: "you" }));
console.log(dialect.render("Hello, ${name}!", { name: "you" }));