Socket
Socket
Sign inDemoInstall

pupa

Package Overview
Dependencies
1
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pupa

Simple micro templating


Version published
Weekly downloads
4.9M
increased by0.38%
Maintainers
1
Install size
14.9 kB
Created
Weekly downloads
 

Package description

What is pupa?

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.

What are pupa's main functionalities?

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\}"

Other packages similar to pupa

Readme

Source

pupa

Simple micro templating

Useful when all you need is to fill in some placeholders.

Install

$ npm install pupa

Usage

import pupa from '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 &lt;br&gt;🦄&lt;/br&gt; and &lt;i&gt;🐮&lt;/i&gt;'

API

pupa(template, data, options?)

template

Type: string

Text with placeholders for data properties.

data

Type: object | unknown[]

Data to interpolate into template.

options

Type: object

ignoreMissing

Type: boolean
Default: false

By default, Pupa throws a MissingValueError when a placeholder resolves to undefined. With this option set to true, it simply ignores it and leaves the placeholder as is.

transform

Type: ((data: {value: unknown; key: string}) => unknown) | undefined (default: ({value}) => value)

Performs arbitrary operation for each interpolation. If the returned value was undefined, it behaves differently depending on the ignoreMissing option. Otherwise, the returned value will be interpolated into a string (and escaped when double-braced) and embedded into the template.

MissingValueError

Exposed for instance checking.

FAQ

What about template literals?

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.

  • pupa-cli - CLI for this module

Keywords

FAQs

Last updated on 13 Sep 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc