When we code we write great code that can be used to save a TON of time for us down the line in other projects. So it only makes sense to break it out into clean util for easy use down the line!

That’s the purpose of useful util. I’ll try and share some utils I have in the projects I work on that find a reuse time and time again.

Without much delay, the util today I’m sharing is for transpiling a string. In other words, evaluating a string and replacing keywords with a variable value. Let’s look at some code!

(template: string, vars: { [x: string]: string | number } = {}): string => {
  const keys = Object.keys(vars);
  let newString: string = template;

  keys.forEach((key: string) => {
    newString = newString.replace(new RegExp(`{${key}}`, 'g'), String(vars[key]));

  return newString;

Use Case

This util method is useful to me because it allows for strings like this "Hello, this is a test string for {name}" to be dynamic anywhere. If you’re familiar with internationalization, it’s a pretty similar approach. Having a static string that can easily be dynamically updated is a powerful method that is also generic enough to be used in almost any project. That’s its power, and purposeful use potential.