Created
August 22, 2017 19:26
-
-
Save catdad/f61e3950dbe6f2bb2aa3b414cccd0330 to your computer and use it in GitHub Desktop.
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 renderMustache(str, obj) { | |
return Object.keys(obj).reduce(function (memo, key) { | |
var value = obj[key]; | |
var regex = new RegExp('\\$\\{' + key + '\\}', 'g'); | |
return memo.replace(regex, value); | |
}, str); | |
}; | |
// usage | |
renderMustache('thing/${a}/stuff/${b}', { a:1, b:2 }); | |
// > 'thing/1/stuff/2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment