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

ekma

v0.3.1

Published

Templating utilities for ES6 template strings

Downloads

5

Readme

ekma: templating for ES6

npm install ekma --save

There are lots of templating languages and view systems for JS. Handlebars, Jade, Nunjucks, and React's JSX are just a few, and there are more every day! All of them expose a domain-specific language (DSL) and introduce plenty of complexity when all you want is regular JavaScript.

The common untapped feature is ES6 template strings, which ekma aims to fix in a portable and flexible way. ekma works well whether you're running native ES6 or transpiling to ES5 via Babel, Traceur, or TypeScript!

ekma includes TypeScript typings out of the box.

Requirements

ekma requires an ES5 environment. That's it!

Examples

ekma templates are just functions that return string-like objects!

Use the $html template tag to automatically escape inputs to fit into HTML:

import { $html, $each, $if } from "ekma";

const myCollection = [5, 6, 7];

const template = () => $html`
	${ $if(myCollection.length === 0, () => `
		Nothing to show in the collection.
	`) }

	${ $each(myCollection, v => `
		Value: ${ v }
	`) }
`;

console.log(template().toString());

The return value of the template is an object with a toString method and the property safeInHTML set to true.

API

This API is using TypeScript syntax for expressing types. For additional details, check the source of the module.

In these annotations, Stringable is an object that has a toString method, or is an actual string.

  • escapeHTML(html: string)
    • escapes the given string as HTML
  • $html
    • A template string tag that marks the string as HTML
    • Use $${ value } to output unescaped values in substitutions
    • Nested strings will automatically have correct escaping
  • $times(count: number, method: Function | Stringable)
    • Outputs the given Function or Stringable count times.
  • $map(collection: Array, callback: Function | Stringable)
    • Equivalent to Array.prototype.map, but evaluates functions
  • $each(collection: Array, callback: Function | Stringable)
    • Iterates over the collection and joins the resulting array into a string
  • $alias(object: any, method: Function)
    • Syntax sugar for method(object), used to shorten access chains
    • Similar to old JavaScript with
  • $if(condition: any, pass: Function | Stringable, fail: Function | Stringable)
    • An if statement in function/expression form
    • Returns the result of pass if the condition is true
    • Otherwise returns the result of fail
  • $join(collection: Stringable[])
    • Joins a collection of elements that may be safe in HTML

Building

Building requires Node.js 5 or newer.

To build ekma from sources, run npm run build in the root of the project.

The main field specified in package.json is only valid after the library has been built, as compiled JS isn't checked into source control to reduce commit noise.

License

ekma is licensed under the MIT license. See LICENSE.md for more details.