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

@egeria/temple

v0.9.0

Published

Dead simple templating functionality for Egeria

Downloads

14

Readme

Egeria Temple

NPM version Downloads

Egeria's temple The temple of Egeria, the SFW nymph, in Rome

NPM

Changelog

v0.7.1

  • Add cleanTemplate to remove tokens from a template and get the static part

v0.7.0

  • Export functions getFields, getValues, getEverything

v0.6.0

  • defaults now returns a wrapper, kinda like request does, so multiple defaults can be set across a project.

v0.5.0

  • Breaking changes
  • Exports two functions: temple is the old template function; defaults lets user change the default date format and provide a hook that temple will call when errors occur.
  • temple only returns the parsed text; no errors

About

Dead simple templating functionality for Egeria.

The module exports two functions.

The defaults function accepts an object with any of the following fields:

  • dateFormat sets the default format whenever temple renders a date token. The default is 'YYYYMMDD'. You can change it to anything supported by moment.
  • onError must be a function. When rendering a template, if any of the tokens are malformed then onError will be called with a string as its only argument. The string will contain a list of all of the errors. Every time temple is called, onError will be called either once or never.

defaults returns a wrapper; use that wrapper instead of temple.

The temple function expects the following arguments: template and data, in this order. template is always a string and data is either an object with one or more fields or a single value - Number, String, or anything else. If either is null, the output text will be ''.

A template is a single string that contains zero or more tokens. Text present between tokens won't be changed.

In a template, "||" and "!!" are reserved sequences. "|||", "||||" and so on are all illegal. The same goes for "!!" and "!!!", "!!!!" and so on. It's OK if those sequences are part of the value that will be rendered, but they must not appear in the template itself.

A token is written like this: {{PRE||FIELD!!FORMAT||POST}}

The order of the various elements must be respected:

PRE is a string that will precede the value.

FIELD is a field of the second argument, data. If data is not an object, there will be only one valid field called "arg", so your tokens can only reference "arg" (e.g. 'Joe had {{arg|| children and}} 1 mule').

FORMAT is the format that will be used to format the FIELD if and only if such value is a Date. It will be ignored otherwise.

POST is a string that will be appended to the value.

PRE, FORMAT and POST are optional. If the value is a Date, the default FORMAT is 'YYYYMMDD' (e.g. "20151124").

FIELD is mandatory, but it doesn't have to exist on data. If it's null, undefined or empty, the entire token will be replaced by an empty string, meaning that PRE and POST will not be rendered either.

Malformed tokens will remain untouched in the final output.

The smalles token you can possibly write is {{fieldName}}. The biggest is {{On day ||happeningDate!!DD-MM-YYYY|| something happened}}.

The function returns a string containing the result.

Example

var temple = require('egeria-temple').defaults({
    defaultDateFormat: 'DD/MM/YYYY',
    onError: console.log
});

var template = '{{title|| - }}{{on ||date|| - }}{{text}}{{fsdjhgsj^#||wrongField|| you won\'t see this}}';

var data = {
	title: 'one',
	date: new Date(),
	text: 'some long text'
}

console.log(temple(template, data).text);

//> "one - on 25/11/2015 - some long text"


template = '{{title|| - }}{{date!!MM/YYYY-DD|| - }}{{text}}';

console.log(temple(template, data).text);

//> "one - 11/2015-25 - some long text"