Simple micro templating library for JavaScript that enables string interpolation with placeholder substitution
npx @tessl/cli install tessl/npm-pupa@2.1.0Pupa is a simple micro templating library for JavaScript that enables string interpolation with placeholder substitution. It supports both object property access and array indexing through a clean brace syntax, includes automatic HTML entity encoding for security, and handles nested object property access through dot notation.
npm install pupaconst pupa = require('pupa');For TypeScript:
import pupa = require('pupa');const pupa = require('pupa');
// Object property interpolation
pupa('The mobile number of {name} is {phone.mobile}', {
name: 'Sindre',
phone: {
mobile: '609 24 363'
}
});
//=> 'The mobile number of Sindre is 609 24 363'
// Array index interpolation
pupa('I like {0} and {1}', ['๐ฆ', '๐ฎ']);
//=> 'I like ๐ฆ and ๐ฎ'
// HTML-safe interpolation (prevents XSS)
pupa('I like {{0}} and {{1}}', ['<br>๐ฆ</br>', '<i>๐ฎ</i>']);
//=> 'I like <br>๐ฆ</br> and <i>๐ฎ</i>'Main templating function that interpolates placeholders in templates with data values. Supports two interpolation modes: plain text replacement and HTML-escaped replacement for security.
/**
* Simple micro templating function for string interpolation
* @param {string} template - Text with placeholders for data properties
* @param {object|unknown[]} data - Data to interpolate into template
* @returns {string} The template with placeholders replaced by corresponding data values
* @throws {TypeError} If template is not a string
* @throws {TypeError} If data is not an object or array
*/
function pupa(template, data);Template Syntax:
{key} - Plain interpolation: replaces with string representation of value{{key}} - HTML-escaped interpolation: replaces with HTML entity-encoded value to prevent XSS{object.property} - Nested object property access using dot notation{0}, {1} - Array index access using numeric keysParameter Details:
template (string): Text containing placeholders in {key} or {{key}} format
{user.profile.name}{0}, {1}, etc.data (object | unknown[]): Data source for placeholder replacement
Return Value:
{{key}}){key})Error Handling:
TypeError if template parameter is not a stringTypeError if data parameter is not an object or arrayUsage Examples:
const pupa = require('pupa');
// Nested object properties
pupa('User {user.name} has email {user.contact.email}', {
user: {
name: 'Alice',
contact: {
email: 'alice@example.com'
}
}
});
//=> 'User Alice has email alice@example.com'
// Array access by index
pupa('Items: {0} and {1}', ['Widget', 'Gadget']);
//=> 'Items: Widget and Gadget'
// Object property access
pupa('Item: {name} costs ${price}', { name: 'Widget', price: 29.99 });
//=> 'Item: Widget costs $29.99'
// HTML escaping for security
pupa('Message: {{userInput}}', {
userInput: '<script>alert("xss")</script>'
});
//=> 'Message: <script>alert("xss")</script>'
// Missing properties become empty strings
pupa('Hello {name}, your score is {score}', { name: 'Bob' });
//=> 'Hello Bob, your score is '
// Type coercion examples
pupa('Count: {count}, Active: {active}', {
count: 42,
active: true
});
//=> 'Count: 42, Active: true'Key Features: