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 🙏

© 2024 – Pkg Stats / Ryan Hefner

x-replace

v0.1.0

Published

Tag-aware string templating

Downloads

2

Readme

x-replace

x-replace is a function that replaces simple key-value pairs with a template string. The primary difference between x-replace and other string templating libraries is that it can ignore the contents of specified html tags.

Features:

  • Ignores the contents of <template> tags by default (configurable) to be friendly with web components
  • Automatically wraps quotes around interpolated tag attribute values
  • Completely logic-less
  • Written in ES5, so can be used in both node.js and the browser.

Example Usage

By default, x-replace ignores the contents of html5 <template> tags:

var replace = require('x-replace')

replace("{{x}}<template>{{y}}</template>{{y}}", { x: 10, y: 20 })
//=> "10<template>{{y}}</template>20"

But you can also specify your own regular expression for which tags to ignore:

replace(/^(x\-|template)/, "<p>{{title}}</p><x-loop data={{ people }}>{{ name }}</x-loop>", {
  title: "Hi!",
  data: [{ name: 'Alice' }, { name: 'Bob' }],
  name: 'Not used',
})
//=> "<p>Hi!</p><x-loop data='[{"name":"Alice"},{"name":"Bob"}]'>{{ name }}</x-loop>"

XSS

By default, x-replace escapes html characters to prevent xss:

replace("<p>{{ userContent }}</p>", { userContent: '<script>alert("hi")</script>' })
//=> "<p>&lt;script&gt;alert('hi')&lt;/script&gt;</p>"

However, you can insert raw values when you need to by using an equal =:

replace("<p>{{= userContent }}</p>", { userContent: '<script>alert("hi")</script>' })
//=> "<p><script>alert("hi")</script></p>"

JSON in Tag Attributes

Sometimes you want to insert JSON into an attribute of a custom element:

<user-profile user={{ userObject }}></user-profile>

However, there is one caveat – in order to be valid HTML, single quotes ' are replaced by &#39;. For example:

var template = "<user-profile user={{ userObject }}></user-profile>"
var result = replace(template, { userObject: { name: "Mr. O'Brian" } })
//=> "<user-profile user='{"name":"Mr. O&#39;Brian"}'></user-profile>"

In order to get back to the original within your web components JS, you will need to use decodeHtmlAttr:

var jsonAttr = '{"name":"Mr. O&#39;Brian"}'
var decoded  = replace.decodeHtmlAttr( jsonAttr )
//=> '{"name":"Mr. O\'Brian"}'
JSON.parse(decoded)
//=> { name: "Mr. O'Brian" }

Why no loops / logic?

Web Components pave the way for handling custom presentation logic in the browser. In the same vein, Server Components will be ideal for handling such logic on the server-side.

In other words, if you need to loop, you should write or use a loop component. If you need to conditionally show/hide content, you should write / use an if component. This is the ideal way forward.