New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

parse-template

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-template

A simple and light-weight template parser

  • 1.0.4-2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Parse Template

A very light weight library which allows you to process tokenised strings

Say you have a string like this:

const input = 'Hello ${firstName}'

You can parse it like this:

import { parseTemplate } from 'parse-template'

const meta = { firstName: 'Jess' }

parseTemplate(input, meta) // 'Hello Jess'

Installing

npm i --save parse-template

Features

Object syntax

const meta = { user: { firstName: 'Joe' } }

parseTemplate('Hello ${user.firstName}', meta) // 'Hello Joe'

Array syntax

const meta = { guests: [{ firstName: 'Alex'}] }

parseTemplate('Hello ${guests[0].firstName}', meta) // 'Hello Alex'

Fallback syntax

const meta = { nickName: null, firstName: 'Sam' }

parseTemplate('Hello ${nickname || firstName || "there"}', meta) // 'Hello Sam'

Empty value

Empty values are replaced with an empty string

const meta = { nickName: null }

parseTemplate('Hello ${nickname}', meta) // 'Hello '

Undefined value

Undefined values are replaced with an empty string

const meta = {}

parseTemplate('Hello ${nickname}', meta) // 'Hello '

Object unwrapping

The resolved "meta" value must be either a string or a number, otherwise it is ignored. So objects and other non string/number values are ommitted

const meta = { user: { nickName: 'Roger' } }

parseTemplate('Hello ${user}', meta) // 'Hello '

Contributing

Thanks for the help :) Please open a PR!

Local Development

npm i

npm test

npm run build

Keywords

FAQs

Package last updated on 27 Jul 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc