Skip to content

Instantly share code, notes, and snippets.

@mike-pete
Last active February 20, 2024 17:52
Show Gist options
  • Save mike-pete/5dc3b185a909d2a1068bc50ea5698180 to your computer and use it in GitHub Desktop.
Save mike-pete/5dc3b185a909d2a1068bc50ea5698180 to your computer and use it in GitHub Desktop.
Aaaah!
// higher-order function that returns a tag function (closure) that replaces variables in a template literal with the value of "word"
const wordReplacer = (word) => (strings, ...values) => {
return strings.join(word)
}
// proxy handler that will intercept "get" and return a tag function
const proxyHandler = {
get(_, prop) {
return wordReplacer(prop)
},
};
const replaceWith = new Proxy({}, proxyHandler);
const Aaaah = replaceWith['Aaaah!']
const replaced = Aaaah`"${'Hello'}" said Bob to Alice.\n"${'Hi Bob'}" responded Alice.`
console.log(replaced)
// "Aaaah!" said Bob to Alice.
// "Aaaah!" responded Alice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment