Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Created April 2, 2024 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmorr/04270f49804d7fdaa336fa85146d9789 to your computer and use it in GitHub Desktop.
Save ryanmorr/04270f49804d7fdaa336fa85146d9789 to your computer and use it in GitHub Desktop.
Simple function to compile a tagged template string with a callback function
function compileTaggedTemplate(strings, values, callback) {
return strings.raw.reduce((acc, str, i) => acc + (callback(values[i - 1])) + str);
}
// Usage:
const tag = (strings, ...values) => compileTaggedTemplate(strings, values, (val) => val.toUpperCase());
console.log(tag`Capitalize ${'all'} ${'substitutions'}`); //=> Capitalize ALL SUBSTITUTIONS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment