Skip to content

Instantly share code, notes, and snippets.

@Josema
Created September 7, 2023 14:53
Show Gist options
  • Save Josema/44836b618bf088dea12ac7997224ab4a to your computer and use it in GitHub Desktop.
Save Josema/44836b618bf088dea12ac7997224ab4a to your computer and use it in GitHub Desktop.
supplant
function supplant(text, params) {
return text.replace(/\${([^{}]*)}/g, (a, b) => {
const r = params[b]
return typeof r === 'string' || typeof r === 'number' ? r : a
})
}
console.log(supplant("I'm ${age} years old!", { age: 29 }));
console.log(supplant("The ${a} says ${n}, ${n}, ${n}!", { a: 'cow', n: 'moo' }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment