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

tagged-templates-io

v1.0.9

Published

Reusable dynamic tagged templates

Downloads

78

Readme

Read me

In most projects we use interpolations for several reasons. This could be simple html class-names, sql queries, or even more complicated strings that needs to be mutated on the fly. This is what this package is good for. You can easily create your own dynamic strings, with conditions, expressions or whatever you need.

API Reference

taggedTemplate(pattern, values, conditions); // string

| Pattern | Values | Conditions | Result | | ------------- |:---------- |----------- | -----| | string | number, string, [number or string] | boolean, string, number, null, [boolean, string, number, null] | string | | text {0} | 'val 1' | null | 'text val 1' | | text {0} and {0} | 'val 1' | null | 'text val 1 and val 1' | | text {0} and {1} | 'val 1' | null | 'text val 1 and val 1' | | text {0} and {1} | ['val 1', 'val 2'] | null | 'text val 1 and val 2' | | text {0} and {1} | [ ['val 1.0', 'val 1.1'], ['val 2.0', 'val 2.1'] ] | [ true, false ] | 'text val 1.0 and val 2.1' | | text {0} and {1} | [ ['val 1.0', 'val 1.1'], ['val 2.0', 'val 2.1'] ] | [ true, null ] | 'text val 1.0 and val 2.0' | | text {key1} and {key2} | { key1: ['val 1.1', 'val 1.2'], key2: ['val 2.1', 'val 2.2'] } | null | 'text val 1.1,val 1.2 and val 2.1,val 2.2' | | text {key1} and {key2} | { key1: ['val 1.1', 'val 1.2'], key2: ['val 2.1', 'val 2.2'] } | { key1: true, key2: false } | 'text val 1.1 and val 2.2' | | text {key1} and {key2} | { key1: ['val 1.1', 'val 1.2'], key2: ['val 2.1', 'val 2.2'] } | { key1: 'true', key2: null } | 'text val 1.1 and val 2.2' | | (key) => `text ${key}` | 'val 1' | null | 'text val 1' | | (key) => `text ${key}` | ['val 1'] | null | 'text val 1' | | (key) => `text ${key}` | ['val 1'] | true | 'text val 1' | | (key) => `text ${key}` | ['val 1'] | [true] | 'text val 1' | | (key) => `text ${key}` | ['val 1'] | [false] or false | 'text val 1' | | (key1, key2) => `text ${key1} and ${key2}` | [['val 1.1','val 1.2'],['val 2.1', 'val 2.2']] | [true, false] | 'text val 1.1 and val 2.2' |

Examples

Html class names

const isFirstNavItem = index === 0;

const className = taggedTemplate(
  `link__{1}--{0} link__type--{1}`,
  [['active',''], 'nav'],
  [isFirstNavItem, true]
);

<h1 :class="className">Title</h1> // VueJs Sample

// Output

// <h1 class="link__nav--active link__type--nav">Title</h1>

SQL

const isClient = true;
const as = 'age';
const from = ['clientTable', 'relationTable'];
const fromCondition = isClient;
const where = `${as} = ${42}`;
const start = 0, end = 10;

const a = taggedTemplate(`
  SELECT {select} AS {as}
  FROM {from}
  WHERE {where}
  LIMIT {start}, {end}
`,
  { select: 'age_col', as, from, where, start, end },
  { from: fromCondition },
)

Render HTML


const classHandler = (id, activeIndex, classes) => {
  if (!!classes) {
     return taggedTemplate(
       `class="{0}"`, 
       [[Array.from(new Set(classes)).join(' '), classes[0]]],
       [id === activeIndex]
     );
  }
};

const activeIndex = 1;

const result = taggedTemplate(
  (listWrapper, listItems, activeClass, activeIndex) => `
    <ul class="${listWrapper}">
        ${listItems.reduce((ul, {id, label}) => {
          return ul+`<li id="${id}" ${classHandler(id, activeIndex, activeClass)}>${label}</li>`;
        }, '')}
    </ul>
  `,
  [
    'list__wrapper',
    [
      { id: 1, label: 'Item 1' },
      { id: 2, label: 'Item 2' }
    ],
    ['list__item','list__item--active'],
    activeIndex
  ]
);

/** 
 * Outputs

<ul class="list__wrapper">
  <li id="1" class="list__item list__item--active">
    Item 1</li>
  <li id="2" class="list__item">
    Item 2</li>
</ul>

*/