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

littoral-templates

v0.3.0

Published

A small JavaScript/TypeScript framework to do templating comfortably using the template literal syntax in either JavaScript or TypeScript.

Downloads

158

Readme

littoral-templates

A small JavaScript/TypeScript framework to do templating comfortably using the template literal syntax in either JavaScript or TypeScript. It doesn't come with its own syntax, like frameworks like mustache, and handlebars do.

Instead, templates look as follows:

[
    `top-level`,
    [ `still top-level, but on a new line` ],
    indent(1)([
        `this is indented (1 level)`,
        indent(2)([
            `this is much more indented (3 levels)`
        ])
    ])
]

Instances of the Template type are arrays of strings nested to an arbitrary depth/level. (Depth 0 corresponds to a single string). To convert this to a proper string, use the asString function, as follows:

import {asString, indentWith} from "littoral-templates"

const indent = indentWith("    ")

console.log(
    asString(<the template from the listing above>)
)

This code produces the following text on the JavaScript console:

top-level
still top-level, but on a new line
    this is indented (1 level)
            this is even more indented (3 levels)

Iterating over collections

A common activity in templates is to iterate over a collection and map each item to text. These sub-texts would then have to be joined together, and taking care of correct indentation is then usually quite a hassle. Using the Template type, you can simply use the Array.map function, as follows:

import {asString, indentWith} from "littoral-templates"

const indent = indentWith("    ")

console.log(
    asString([
        `my items:`,
        indent(1)(
            ["foo", "bar"].map((item, index) => `item ${index + 1}: "${item}"`)
        )
    ])
)

This code produces the following text on the JavaScript console:

my items:
    item 1: foo
    item 2: bar

Including content conditionally

Another common activity in templates is to include some content conditionally. This package provides the convenient when function for that. An example of its usage is as follows:

[
    `foo`,
    `bar`,
    when(n === 3)([
        `lizard`,
        `sfdeljknesv`
    ])
]

After applying the asString, and assuming the variable n holds the value 3, this evaluates to:

foo
bar
lizard
sfdeljknesv

In case the argument to the function call after when(<some boolean condition>) has side effects, you want to turn that into a thunk, as follows:

let touched = 0
const template = [
    `foo`,
    `bar`,
    when(n === 3)(() => [
        `${++touched}lizard`,
        `sfdeljknesv`
    ])
]

Using side effects inside a template can be useful to store information that's needed elsewhere in the text-to-generate. An example of that would be to keep track of imports that have to appear before their usage.

Other convenience functions

Other convenience functions are:

  • withNewlineAppended: Wrap this around a template function (see definition below) to produce a template function that adds a newline after any item fed to the original template function.

    A template function is any function that produces an instance of Template.

    An example of the usage of withNewlineAppended is

    [1, 2, 3].map(withNewlineAppended((num) => `${num}`))

    which should produce the following text:

    1
      
    2
      
    3
      
  • commaSeparated: Given a list of strings, this function adds a comma after every string, except for the last one.

    E.g., commaSeparated(["1", "2", "3"]) becomes ["1,", "2,", "3"]. This is useful e.g. for rendering “pretty” import statements.

    Note that this only works on a list of strings, not an any instance of Template.

Package name

The repository's name is a play on "temporal literals" and "littoral", begin phonetically close to "literal". The latter word indicates the part of a body of water (lake, sea, or ocean) that's closest to shore, usually shallow, and possibly not always entirely submerged. The name tries to convey that implementing templates with this framework doesn't require you to "wade too far into the water".