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

str-temp

v1.0.5

Published

A tool to combine your string and data more effectively.

Downloads

3

Readme

str-temp

This is a tool used to process and insert data inside a string in a proper format according to your needs. This uses template strings to make processing and storing flexible. Use {} in your string and pass the variable as parameters or pass them later by calling the returned function.

Basic

  • The program adds the data to the brackets based on a predefined flow. The function will take it's first argument as the template string and then the parameters.

  • The parameters, except Objects, are added to an array. If a multi dimensional array is passed as parameter, it flattens into a single dimensional array and is added with the other array containing data parameters.

  • The function will first place Index and Key values in the brackets and then it will fill the Empty or Comment brackets. The function will place the values from left to right brackets for the Empty or Comment brackets.

  • The called function will return a function until all the brackets are occupied. If all the brackets are occupied, the resultant string will be returned. If you want to see the current state of the string, Refer this Section.

Comments

You can use comments in {} by writing the text between ~ for better readability.

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

str("{~resource~} {}", "Memory", "Stack Overflow");
>> Memory Stack Overflow

Escape Character

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

str("{~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. Brackets with comments ( eg. {~message~}` ) will be also treated as empty brackets.

str("{} {} {} {}", "A")("B", "C", "D")
>> A B C D
  • 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.

str("{1} {3} {2} {0}", "A", "B", "C", "D")
>> B D C A
  • 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.

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

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

str("{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.

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

Pass the array rather together rather than passing the parameters individually. The values are extracted from the array and sorted out in 2 portions, ie. Strings and Numbers; and Objects. The Strings are then added from left to right. The Objects are evaluated in the same way.

str("{1} {3} {2} {0}", ["A", ["B", "C"]], ["D"])
>> B D C A

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

Current State

Current state of the string in returned function:

const message = str("{~message~} {~name~}", "Hello");
console.log(message.string);
>> Hello {~name~}

Current state of unused brackets in returned function:

const message = str("{~message~} {~name~}", "Hello");
console.log(message.unused);
>> [ { unused: '{~name~}', index: 6 } ]

Examples

CODE

Code Example

OUTPUT

Console Output

Note

In the case of strings like

str("{5}")

You will need to pass 6 arguments where the first 5 arguments will be dumped and the 6th one will be placed in the string and the resultant will be returned.