Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Set of convinient pure functions to generate HTML on server-side via tagged template literals.
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.
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:
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.
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>
`
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)
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>
`
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'
})}
`
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>
Just remove HTML-comments () from string.
FAQs
Set of convinient pure functions to generate HTML on server-side via tagged template literals.
The npm package html-fns receives a total of 7 weekly downloads. As such, html-fns popularity was classified as not popular.
We found that html-fns demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.