Last active
August 29, 2015 14:25
-
-
Save JesusLeon/a1d55c126a7524576410 to your computer and use it in GitHub Desktop.
A small JS {string} parser
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 parse(template, data) { | |
var replacer = function (context) { | |
return function (placeholder, name) { | |
return context[name]; | |
}; | |
}; | |
return template.replace(/\{(\w+)\}/g, replacer(data)); | |
} | |
// | |
// Usage | |
var template = "Hello, {what}!"; | |
var data = {"what": "World"}; | |
console.log(parse(template, data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found somewhere on the net.