
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
The pupa npm package is a simple string interpolation library. It allows you to replace placeholders in a string with actual values from an object or an array. It is useful for templating and customizing messages or configurations.
String interpolation with an object
This feature allows you to interpolate values from an object into a string. For example, if you have an object with properties 'name' and 'notifications', pupa can replace the placeholders in the string with the corresponding values from the object.
"Hello, {name}! You have {notifications} new notifications."
String interpolation with an array
This feature allows you to interpolate values from an array into a string. The placeholders are indexed based on the array's indices, allowing you to insert array elements into the string at the specified positions.
"Hello, {0}! You are number {1} in line."
Nested object interpolation
Pupa also supports nested object interpolation, where you can access nested properties within an object to replace placeholders in a string.
"Hello, {user.name}! Your role is {user.role}."
Escape interpolation syntax
If you need to include the interpolation syntax as a literal part of the string, you can escape it using a backslash. This tells pupa not to interpret it as a placeholder.
"This is a message with an escaped placeholder: \{escaped\}"
Mustache is a logic-less template syntax that can be used for HTML, config files, source code, etc. It is more feature-rich than pupa, supporting sections, inverted sections, and partials, making it suitable for more complex templating tasks.
Handlebars is a superset of Mustache that adds more power and flexibility. It includes features like block expressions and helpers, which allow for more dynamic and reusable templates. It is more complex than pupa but also more capable for extensive templating needs.
EJS, or Embedded JavaScript templates, is a templating engine that allows you to generate HTML markup with plain JavaScript. It offers greater control and flexibility than pupa, with features like includes, conditional statements, and loops.
Lodash's template function is part of the larger Lodash utility library. It provides a simple interpolation feature similar to pupa but also includes additional template features like evaluation of arbitrary JavaScript code within templates.
Simple micro templating
Useful when all you need is to fill in some placeholders.
$ npm install pupa
const pupa = require('pupa');
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'
pupa('I like {0} and {1}', ['🦄', '🐮']);
//=> 'I like 🦄 and 🐮'
// Double braces encodes the HTML entities to avoid code injection
pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
//=> 'I like <br>🦄</br> and <i>🐮</i>'
Type: string
Text with placeholders for data
properties.
Type: object | unknown[]
Data to interpolate into template
.
Template literals expand on creation. This module expands the template on execution, which can be useful if either or both template and data are lazily created or user-supplied.
FAQs
Simple micro templating
The npm package pupa receives a total of 3,591,961 weekly downloads. As such, pupa popularity was classified as popular.
We found that pupa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.