Created
September 7, 2023 14:53
-
-
Save Josema/44836b618bf088dea12ac7997224ab4a to your computer and use it in GitHub Desktop.
supplant
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 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