Created
February 2, 2018 18:45
-
-
Save joelcardinal/7854bb3a3e7a1b1217644feb5fdc8271 to your computer and use it in GitHub Desktop.
Utility to populate templates for pre-ES2015 backticks
This file contains hidden or 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
/* | |
* str = 'I went to the {{location}} and found {{item}}'; | |
* obj = {location : 'store', item : 'milk'}; | |
*/ | |
function getFilledTplObj(str,obj){ | |
function replacer() { | |
return obj[arguments[1]]; | |
} | |
return str.replace(/{{([^}}]+)?}}/g, replacer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment