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

ts-mustache

v0.0.4

Published

[Mustache][mustache] is a pretty good templating libary, but it doesn't play well with TypeScript. `ts-mustache` makes it better, adding:

Downloads

76

Readme

ts-mustache

Mustache is a pretty good templating libary, but it doesn't play well with TypeScript. ts-mustache makes it better, adding:

  1. type inference from existing Mustache templates
  2. facilities for declaring types and rendering templates

Test

$ npm i
$ npm test

Usage

import { DefaultLoader, Declarer, Renderer } from 'ts-mustache'

const loader = new DefaultLoader({
  dir: './templates',
})

// Generate typedefs
const declarer = new Declarer(loader)
declarer.declare()
  .then(types => fs.writeFileSync('./mustacheTypes.ts', types)

CLI

The same behavior is also available via a small CLI:

ts-mustache --dir=./templates -o ./templates/types.ts

Rendering typed templates

Once types exist in a TemplateMap, the Renderer class provides a convenient way to use them:

import { TemplateMap } from './mustacheTypes'

const loader = new DefaultLoader<TemplateMap>({
  dir: './templates',
})

const renderer = new Renderer<TemplateMap>(loader)

renderer.render('post', { title: 'Foobar' })
  .then(rendered => console.log(rendered))

Philosophy

Mustache goes to heroic efforts to resolve and/or handle missing data at runtime, recursing through previously-seen context to attempt to fill in references with something. Further, a template like {{ name }} implies that a variable called name is expected, but nothing about its type (integer? string? vegetable? mineral?) or nullability. Clearly, indeterminate behavior makes inference a challenge.

The Declarer class in this library attempts an imperfect balancing act between overly-restrictive and so-broad-as-to-be-functionally-useless type declarations based on the Mustache spec and common usage patterns seen in the wild.

Using ts-mustache with an existing template library is likely to turn up some cases where types can't be neatly inferred.

{{#items.length}}
<ul>
  {{#items}}
  <li>{{name}}</li>
  {{/items}}
</ul>
{{/items.length}}

A JavaScript programmer will easily recognize that the {{ items }} referenced in the template likely correspond to an array, and that the template is using type coercion from items.length to skip rendering an empty array.

This library isn't that smart.

Fortunately, most of these cases can be resolved by tweaking how data are prepared--and type-checking will make these changes relatively safe to enact!

const templateData = {
  arr: items.length ? items : undefined
}
{{#arr}}
<ul>
  {{#items}}
  <li>{{name}}</li>
  {{/items}}
</ul>
{{/arr}}

There are likely many other cases that can/should be resolved by this library, too--if you find one, please submit an issue or pull request!

License

ISC