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

@impjs/process-template-literals

v0.0.1-alpha.2

Published

Template for NodeJS projects

Downloads

2

Readme

process-template-literals-expressions

A helper library that lets you process expressions within template literals, synchronously or asynchronously.

Coupled with omniformat, it opens the door to:

const createProcessorTag = require('process-template-literals');
const omniformat = require('@impjs/omniformat');
// Wrap omniformat to provide our custom configuration
function formatter(expression) {
  return omniformat(expression, {iteratorJoinString: ' '});
}
// Create our tag to process each expression with our formatter
const tag = createProcessorTag(({ strings, expressions }) => {
  const processedExpressions = expressions.map(formatter);
  // Handle that some of the formatted values might have been promises
  if (processedExpressions.some(v => v instanceof Promise)) {
    return Promise.all(processedExpressions).then(
      fullfilledProcessedExpressions => ({
        strings,
        expressions: fullfilledProcessedExpressions
      })
    );
  }
  // If none are Promises, we can keep things synchronous
  return { strings, expressions: processedExpressions };
});

// Running the function
tag`Look, ${() => 'a plane'}` // 'Look, a plane'
// Joining array elements
tag`Look, ${['a', 'plane']}` // 'Look, a plane'
// Stringifying objects
tag`Look, ${{a: 'a', plane: 'plane'}}` // 'Look, {"a": "a", "plane": "plane"}
// Awaiting promises
const promiseForAPlane = Promise.resolve('a plane');
tag`Look, ${promiseForAPlane}`.then(console.log) // Logs 'Look, a plane'
// Or async functions
tag`Look, ${async () => 'a plane'}`.then(console.log);

Lazy behaviour

By default tags will run as soon as invoked. If you prefer creating a function to invoke later, you can use the lazy option.

Processor signature

The processor function receives an object with the following keys:

  • strings: The list of strings from the template,
  • expressions: The list of expressions between the template strings.
  • data: Extra data passed to the

It is expected to return an object of the same structure.

Async behaviour

If the processor returns a Promise for any of the expression, the function will await their resolution before stitching the string back together.

Lazy evaluation

By default, the function will evaluate the template straight away. If you prefer it to create a function for later use, you can set {lazy:true} as an option.

Future

Sometimes you might not want to output a String from the tag. You can imagine wanting to pipe the result into a Stream. The stitch function should live outside the main function and be left to compose with other processors. This would allow more customization on the timing of the processing of strings and expressions regarding to their "stitching". In the case of a streamed output, it's likely you'd want to do process a string, send the string, process an expression, send the expression... rather than process strings and expressions, stitch and send everything. This would also leave more flexibility as to what's used for streaming: Node streams, Observables, Callbags (or similar functions).