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

strx

v1.0.3

Published

Yet another flexible template engine.

Downloads

3

Readme

strx

Yet another flexible template engine.

You can simply store and pass values to the produce the final string. Use templates in your string by using {}.

Install

npm i strx

However, you can install the package from any other package-manager (like yarn, bun, etc.) which forks from NPM Registry.

Basic

  • The program adds the data to the template brackets based on a predefined flow. The function will take it's first argument as the template string and then either return a function to pass the data or a string if all the templates have received the data.

  • The data is passed as normal arguments to the function, all non object data are added in an array whereas all the objects are combined together.

  • The indexes are greedy and will grab the data for themselves. You can defined same index and keys multiple times.

  • Indexes have a tendency of rolling, that is they will if the value is not available, then the index will roll towards zero until the value if found.

Escape Character

To escape the brackets and prevent yourself from errors, use {{ and }} and the engine will treat the brackets like normal characters.

strx("{~message~} {~name~} {{🙃}}", "Hello", "Node");
>> Hello Node {🙃}

Referencing

  • Empty

You can simply use {} where you want to substitute data in the string. The data will be picked and placed from left to right.

strx("{} {} {} {}", "A")("B", "C", "D")
>> A B C D
  • Comments

You can use comments in {} by writing the text between ~ for better readability. They will be treated same as empty brackets.

strx("{~message~} {~name~}", "Hello", "Node");
>> Hello Node

strx("{~resource~} {}", "Memory", "Stack Overflow");
>> Memory Stack Overflow
  • Indexes

Sometimes the order of passed elements does not match their order in the string. Use Indexes starting from 0 and the function will replace the passed Strings or Data from left to right as if they are in an array.

strx("{1} {3} {2} {0}", "A", "B", "C", "D")
>> B D C A
strx("{1} {3} {2} {0} {4[1]} {4[0]}", "A", "B", "C", "D", ["E", "F"])
>> B D C A E F
  • Keys

You can also pass valid JS keys between the {} and then pass the JS Object to with the same keys and their values. Values will be overridden from left to right.

strx("{message} {name}", { message: "Hello", name: "Nathan" })
>> Hello Nathan

Here you can also pass the key as a nested object.

strx("{message} {info.name.first}", { message: "Hello", info: { name: {
    first: "Nathan",
    last: "Drake"
}}})
>> Hello Nathan

With the nested objects, there will be arrays and you will be able to pass those array indexes too.

strx("{fruit[1]} is tasty 🤤.", { fruit: [ "Banana", "Apple", "Orange"] })
>> Apple is tasty 🤤.

Try combining it with chalk.js to see the different possibilities.

Rolling Index Example

Following is an example of rolling index.

strx("{1} {2} {0} {} {} {6} {7}")(1, 2, 3, 4, 5, 6, 7, 8)
>> 2 3 1 4 5 7 8

In the above example, as index 5 is not mentioned and there was a lack of empty brackets, value 6 was skipped.

Usage

The strx() function will take a string as it's first argument and optional ...args as data to place inside string. Until all the templates are satisfied, ie, have data value other than undefined. The function will return another function for the user to enter the values. Once all the templates have a value other than undefined, the function will return a string.


const strx = require("strx")

const consoleFormat = strx("{~LEVEL~}: {~TYPE~} {~detailed error message~}");

const warn = consoleFormat("WARN");
const error = consoleFormat("ERROR");


const stackOverflow = error("STACK_OVERFLOW")

console.log(stackOverflow("Maximum recursion stack is reached."))
// ERROR: STACK_OVERFLOW Maximum recursion stack is reached.

console.log(warn("UNUSED_VARIABLE","The stack is almost at it's limit."))
// WARN: UNUSED_VARIABLE The stack is almost at it's limit.

APIS

Current State

Current state of the templates in returned function:

const message = strx("{~message~} {~name~}", "Hello");
console.log(message.templates);
>> {
  empty: [
    {
      key: "~message~",
      start: 0,
      length: 11,
      value: "Hello",
    }, {
      key: "~name~",
      start: 12,
      length: 8,
      value: undefined,
    }
  ],
  array: [],
  key: [],
}