Last active
December 16, 2015 04:49
-
-
Save vermilion1/5379524 to your computer and use it in GitHub Desktop.
Substitute
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
/** | |
* Substitute {d} with passed arguments | |
* @example substitute('key is {0} and value is {1}', 'some_key', 'some_value'); | |
* @param string {String} initial string | |
* @returns {String} modified string | |
*/ | |
var substitute = function (string) { | |
if (!string) { | |
return ''; | |
} | |
var values = _.rest(arguments); | |
var result = string; | |
_.each(values, function (value, index) { | |
var reg = new RegExp('\\{' + index + '\\}'); | |
result = result.replace(reg, value); | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment