Created
April 2, 2024 17:05
-
-
Save ryanmorr/04270f49804d7fdaa336fa85146d9789 to your computer and use it in GitHub Desktop.
Simple function to compile a tagged template string with a callback function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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