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

templatar

v1.0.1

Published

A lightweight, flexible template placeholder replacement library for JavaScript with customizable delimiters.

Readme

Templatar

A lightweight, flexible template placeholder replacement library for JavaScript with customizable delimiters.

Features

  • 🎯 Simple and intuitive API
  • 🔄 Customizable delimiters (default: <{ and }>)
  • ⚡ Fast and efficient string replacement
  • 🛡️ Configurable missing value handling
  • 🔧 Value transformation support
  • 💪 TypeScript support
  • 🐛 Comprehensive error handling

Installation

npm install templatar

Usage

Basic Usage

import Templatar from 'templatar';

const replacer = new Templatar();
const template = 'Hello <{name}>, welcome to <{place}>!';
const data = { name: 'Alice', place: 'Wonderland' };

const result = replacer.replace(template, data);
console.log(result); // "Hello Alice, welcome to Wonderland!"

Custom Delimiters

const replacer = new Templatar({
  delimiters: {
    starting: '{{',
    closing: '}}',
  },
});

const template = 'Hello {{name}}, your ID is {{userId}}';
const data = { name: 'Bob', userId: '12345' };

const result = replacer.replace(template, data);
console.log(result); // "Hello Bob, your ID is 12345"

Handling Missing Values

By default, an error is thrown if a value is missing:

const replacer = new Templatar();
const template = 'User: <{name}>, Score: <{score}>';
const data = { name: 'Charlie' };

// This will throw an error
try {
  replacer.replace(template, data);
} catch (error) {
  console.error(error); // Error: Key "score" is not found and ignoreMissing is false
}

Allow and transform missing values:

const replacer = new Templatar({
  ignoreMissing: true,
  transform: ({ value, key }) => {
    return value || 'N/A';
  },
});

const template = 'User: <{name}>, Score: <{score}>';
const data = { name: 'Charlie' };

const result = replacer.replace(template, data);
console.log(result); // "User: Charlie, Score: N/A"

Custom Value Transformation

const replacer = new Templatar({
  transform: ({ value, key }) => {
    if (key === 'price') return `$${value.toFixed(2)}`;
    if (key === 'date') return new Date(value).toLocaleDateString();
    return String(value);
  },
});

const template = 'Item: <{item}>, Price: <{price}>, Date: <{date}>';
const data = {
  item: 'Widget',
  price: 19.99,
  date: '2024-01-15',
};

const result = replacer.replace(template, data);
console.log(result); // "Item: Widget, Price: $19.99, Date: 1/15/2024"

Using Detault Values

We can also easily define default values within our placeholders by using the syntax propName||defaultValue.

const template = 'User: <{name}>, Score: <{score||0}>';
const data = { name: 'Charlie' };

const result = replacer.replace(template, data);
console.log(result); // "User: Charlie, Score: 0"

Note: We do not need to set ignoreMissing to true as long as we have default values defined since any missing values use that default.

API Reference

Templatar

Constructor Options

interface TemplatarOptions {
  delimiters?: {
    starting: string;
    closing: string;
  };
  ignoreMissing?: boolean;
  transform?: (data: { value: unknown; key: string }) => string;
}
  • delimiters: Custom delimiters for template placeholders
    • starting: Starting delimiter (default: '<{')
    • closing: Closing delimiter (default: '}>')
  • ignoreMissing: Whether to allow missing values (default: false)
  • transform: Function to transform values before replacement

Methods

replace(template: string, data: Record<string, unknown>): string

Replaces placeholders in the template with corresponding values from the data object.

  • template: String containing placeholders
  • data: Object containing replacement values
  • Returns: String with all placeholders replaced
  • Throws: Error if keys are missing and ignoreMissing is false

Why the fuss?

So you might be wondering why this module is even useful when there are other template replacement modules out there.

Well I developed this for use with generating dynamic AI prompts using markdown templates. But new lines, spaces, symbols and tabs all have significant meaning in markdown. So we should be able to do crazy stuff like this.

- ACCESSORIES:
  <{accessories ||
  Any Observable:
  - Headgear
  - Footwear
  - Body drapes
  - Neckpieces
  - Eyewear
  - Hair accessories
  - Waist accessories
  - Hand accessories
  - Ear accessories
  - Wristwear
    }>

Now imagine the following template replacement data:

let accessories = `
  - Headgear
  - Footwear
  - Another bullet`;

const data = {
  accessories: `
  - Headgear
  - Footwear
  - Another bullet`,
};

const result = replacer.replace(template, data);
console.log(result); // "User: Charlie, Score: 0"

This yields :

ACCESSORIES:
- Headgear
- Footwear
- Another bullet

But if accessories is null or missing, then it defaults to:

ACCESSORIES: 
Any Observable:
- Headgear
- Footwear
- Body drapes
- Neckpieces
- Eyewear
- Hair accessories
- Waist accessories
- Hand accessories
- Ear accessories
- Wristwear

I hope you can now see how powerful this is and how it is useful, and different.

Error Handling

Templatar throws errors in the following cases:

  • Template is not a string
  • Data is not an object
  • Required key is missing in data object

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.