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

transplates

v1.0.0

Published

Simple string templates, might be useful for translation/i18n. (node.js module)

Downloads

1

Readme

transplates

Simple string templates, useful for translation. (node.js module)

Simple Example (Using TypeScript)

import * as Transplates from "transplates";

let languages = {
    en: {
        concat: ">1|*(0,-3)[${i}, ]${-2} and |${-1}",
        template: "Hello ${name}, your item+{s} -{is}+{are}: ${items}."
    },
    es: {
        concat: ">1|*(0,-3)[${i}, ]${-2} y |${-1}",
        template: "Hola ${name}, su+{s} item+{s} -{es}+{son}: ${items}."
    },
    ja: {
        concat: ">1|*(0,-2)[${i}と]|${-1}",
        template: "こんにちは、${name}の項目は${items}です."
    }
};

function listItems(name: string, items: string[]): void {
    for (let langK in languages) {
        let lang = languages[langK];
        let str = Transplates.fillIn(lang.template,
            { name: name, items: items, isPlural: items.length > 1 },
            { concat: lang.concat });
        console.log(`${langK}: ${str}`);
    }
}

let items = ["foo"];
listItems("John", items);
items.push("bar");
items.push("hoge");
listItems("John", items);

will output

en: Hello John, your item is: foo.
es: Hola John, su item es: foo.
ja: こんにちは、Johnの項目はfooです.
en: Hello John, your items are: foo, bar and hoge.
es: Hola John, sus items son: foo, bar y hoge.
ja: こんにちは、Johnの項目はfooとbarとhogeです.

Operation

The main function in transplates is:

fillIn(template: string, filler: Object, langHelper?: LangHelper): string ;

fillIn will find and replace tokens in the template string using the following syntax:

  • ${property}

    Will be replaced with filler.property or "" if filler.property is undefined.

    If filler.property is an array, it will be treated in the following way:

    • If array.length === 0 => ""

    • If array.length >= 1 =>

      It will use a list template to concatenate array parts, the list-template to use will be taken from langHelper.concat. If langHelper is undefined, it will just concatenate the array items with commas.

  • ?{property[; trueTS; falseTS]}

    If filler.property===true, it will be replaced with fillIn(trueTS,filler) else fillIn(falseTS,filler) will be used. Both parameters are optional, in which case, "" will be used when needed.

  • +{TS}, -{TS}

    These are syntax sugar version for ?{filler.isPlural;TS} and ?{filler.isPlural;;TS} , respectively

List templates

List templates are a special case of templates suited to generate human readable concatenation of items on an array, or in any case where items from an array need to be extracted. The following tokens are used

  • ${n} , ${-n}

    To be replaced with array[n] or array[array.length+n] respectively.

  • *([begin,end])[ loopExpr ]

    From begin to end, loopExpr will be joined and then replaced. ${i} inside loopExpr will contain the current position in the array. If begin is missing, 0 will be assumed. If begin is negative, array.length+begin will be used. If end is missing, array.length-1 will be assumed. If end is negative array.length+end will be used.

  • >n|str|,<n|str|

    This guards will be applied before parsing the template. If array.length > n the string inside the braces will be kept, otherwise it will be discarded. The same but with array.length < n for the '<' version.

For example, to make a concatenation of items in English:

`">1|*(0,-3)[${i}, ]${-2} and |${-1}"`

Spanish:

`">1|*(0,-3)[${i}, ]${-2} y |${-1}"`

And Japanese:

`">1|*(0,-2)[${i}と]|${-1}"`