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

@fnando/seagull

v0.0.8

Published

Minimal template engine with compiled output for JavaScript.

Downloads

15

Readme

@fnando/seagull

Tests NPM NPM

Minimal template engine with compiled output for JavaScript.

Installation

This package is available as a NPM package. To install it, use the following command:

npm install @fnando/seagull --save

If you're using Yarn (and you should):

yarn add @fnando/seagull

Usage

Syntax Highlighting

Precompiling templates

To compile templates into JavaScript, use the CLI:

# You can use either a file path (e.g. src/templates/hello.sea) or a glob
# pattern (e.g. src/**/*.sea) as the input source.
$ seagull compile --input hello.sea --output hello.js

# To export individual files, use a directory path without the `.js` extension.
$ seagull compile --input 'src/**/*.sea' --output 'src/helpers'

Compiling templates in runtime

import { compile } from "@fnando/seagull";

const render = compile("Hello there, {name}.");

render({ name: "John" });
//=> Hello there, John.

Syntax

Variables

Hello there, {name}.
Hello there, {person.name}.

Conditionals

{if isReady}
  Ready!
{/if}

{unless isReady}
  Pending!
{/unless}

{when status="ready"}Ready!{/when}
{when status='ready'}Ready!{/when}
{when status=readyStatus}Ready!{/when}
{when status=statuses.ready}Ready!{/when}

Iteration

Iterating arrays:

{each person in people}
  Hi there, {person.name}.
{/each}

{each person, index in people}
  {index}: {person.name}
{/each}

Iterating dictionaries (objects with key value):

{each id => person in peopleMap}
  Hello, {person.name}. Your id is {id}.
{/each}

{each id => person, index in peopleMap}
  {index}: {person.name} ({id})
{/each}

Helpers

Helpers that receive one single positional argument must be called by pipeling the parameter into the helper.

You're name in reverse is {name | upcase | reverse}.

Helpers need to be registered as part of the context when calling the rendering function. Seagull doesn't bundle any helpers.

template({
  name: "John",
  upcase: (input) => input.toUpperCase(),
  reverse: (input) =>
    input
      .split("")
      .reduce((memo, char) => [char].concat(memo), [])
      .join(""),
});

The if and unless blocks also accept helper piping.

{if emails | isEmpty}
  You have no mail!
{/if}
template({
  emails: [],
  isEmpty: (input) =>
    input &&
    typeof input !== "boolean" &&
    "length" in input &&
    input.length === 0,
});

Finally, you can also pipe strings to helpers.

{"seagull_is_nice" | i18n}
{'seagull_is_nice' | i18n}

If you're function requires multiple parameters, then you can use the named parameter call.

{i18n path="messages.hello" name=user.name}

This will translate to a call like i18n({path: "message.hello", name: user.name}).

HTML Escaping

All interpolations are escaped by default. If you need to decode the output for tests, you can import the decode function.

import { compile, decode } from "@fnando/seagull";

test("renders template", () => {
  const template = compile(`{message}`);
  const output = template({ message: "<script>alert(1);</script>" });

  expect(output).toEqual(
    "&#0060;script&#0062;alert&#0040;1&#0041;&#0059;&#0060;&#0047;script&#0062;",
  );
  expect(decode(output)).toEqual("<script>alert(1);</script>");
});

Using TypeScript

Seagull doesn't have direct TypeScript support, but that doesn't mean you can't use typed template functions.

The way I like to do it is by creating a file called templates.d.ts somewhere (e.g. if I export the templates to src/helpers/templates.js, then I use src/helpers/templates.d.ts). This file will hold all function types.

Let's say I have a template function that works like hello({name: "John"}); in this case, my module declaration would look like this:

declare module "src/helpers/templates" {
  export function hello(params: { firstName: string }): string;
  export function goodbye(params: { lastName: string }): string;
}

If you're using helpers, tem you can also type an intermediary template function.

import * as templates from "src/helpers/templates";

// Add your helpers here
// You don't have to inline them (e.g. use `const helpers = {helper}` instead).
const helpers = {};

export function template<
  T extends keyof typeof templates,
  P = Parameters<typeof templates[T]>[0],
>(name: T, params: P): string {
  // @ts-expect-error injecting helpers
  return templates[name]({ ...params, ...helpers });
}

To call the templates using this function:

import { template } from "src/helpers/template;

console.log(template("hello", {firstName: "John"}));
console.log(template("goodbye", {lastName: "Doe"}));

Maintainer

Contributors

Contributing

For more details about how to contribute, please read https://github.com/fnando/seagull/blob/main/CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/seagull/blob/main/LICENSE.md.

Code of Conduct

Everyone interacting in the seagull project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.