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

html-fns

v1.4.0

Published

Set of convinient pure functions to generate HTML on server-side via tagged template literals.

Downloads

23

Readme

Heplers for generating HTML via Tagged Templates

Set of convinient pure functions for generating HTML on server-side via tagged template literals without any template langueges.

It's sort of lit-html (https://github.com/polymer/lit-html) but for Node.js.

Why

I like to write small self-contained components (like in React). But I don't need SPA and I mostly generate HTML on a server. Server-side rendering in React is slow, expensive and it's overhead just for generating HTML.

I tried a few popular template language, like EJS, Nunjucs, Edge.js. They all great but I have a few problems with that:

  • You need to learn new API and language syntax.
  • Most of them create AST that becomes slow when you use many little components (partials) and had complex control flow.
  • Most of them (besides EJS) has very basic possibilities.
  • In most of them, you have to use a separate file for every component. If you have many tiny components it becomes hard to manage all these files.

With es6 we can use tagged templates and solve all these problems. It also very fast, because V8 really good at optimizing string manipulations. Moving from Edge.js to pure functions reduce template generation from 20-25 ms to less than 1 ms (caching in Edge.js edge.js was enabled).

These helpers make working with HTML in JS strings a bit easier.

Usage

1. html

Useful for syntax highlighting via this plugin https://github.com/mjbvz/vscode-lit-html.

Keep in mind that it doesn't escape HTML in interpolated values deliberately, because if you'll use many small components it's easier when this is default behaviour. In you need escape HTML use 'safeHtml' function.

import { html } from 'html-fns'

html`
  <div class="some-class">
    <h1>${title}</h1>
  </div>
	`

2. safeHtml

Works like 'html' function but escape all html in interpolated values. Can be used as normal function call and as tagged template.

import { safeHtml } from 'html-fns'

safeHtml`
  <div class="some-class">
    <h1>${inputUserData}</h1>
  </div>
  `
  // OR

  safeHtml(inputUserData)

3. $each

Iterate through Array or Object and return string (concatenaed results).

import { $each } from 'html-fns'

const data = { a: 1, b: 2 }

html`
	<div class="some-class">
		<h1>${$each(data, (item) => `hello ${item}`)}</h1>
	</div>
`

4. $if

Takes 2 arguments, both functions.

If function from 1 argument returns falsy-value or throws error - second function will not be executed.

If function from 1 argument returns truthy-value - second function will execute with argument that was returned from first function.

import { $if } from 'html-fns'

html`
  ${$if(() => obj.some.data, (data) => {
    return 'you'
  })}
`

5. tag

Declarative way for creating HTML tags.

import { tag } from 'html-fns'

tag('div', 'super-abstract-class', tag('p', false, 'hello'))
// -> <div class="super-abstract-class"><p>hello</p></div>

tag('div', { id: 'super-abstract-id' }, tag('div', 'list', [1, 2, 3, 4]))
// -> <div id="super-abstract-id"><div class="list">1 2 3 4</div></div>

6. removeHtmlComments

Just remove HTML-comments () from string.

Link and articles

  • https://github.com/vlucas/echotag
  • https://github.com/declandewet/common-tags
  • https://2ality.com/2015/01/template-strings-html.html
  • https://hackernoon.com/how-i-converted-my-react-app-to-vanillajs-and-whether-or-not-it-was-a-terrible-idea-4b14b1b2faff