Last active
January 27, 2017 15:14
-
-
Save alexmccabe/08fd5e4a53abb60eb82eebd1bf1089eb to your computer and use it in GitHub Desktop.
Converts a string containing Moustache style curly braces into a proper string.
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
(function () { | |
'use strict'; | |
var textReplacement = { | |
/** | |
* Converts a string containing Moustache style curly-brace variables, | |
* into a coherent string. | |
* @param {string} string Input string containing | |
* {{ curlyBrace }} variables | |
* @param {object} replacements key / value pair object where the key | |
* is the variable in the string, and the | |
* value is the replacement string. | |
* @return {string} | |
*/ | |
replace: function (string, replacements) { | |
var key; | |
var regex; | |
for (key in replacements) { | |
if (replacements.hasOwnProperty(key)) { | |
regex = new RegExp('\\{\\{([\\s?' + key + '\\s?]*?)\\}\\}', 'g'); | |
string = string.replace(regex, replacements[key]); | |
} | |
} | |
key = null; | |
regex = null; | |
return string; | |
}, | |
}; | |
module.exports = textReplacement; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment