Created
September 16, 2014 23:59
-
-
Save ryanseys/856471207aa3f80ff614 to your computer and use it in GitHub Desktop.
util.format
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
/** | |
* Format a string with values from the provided object. | |
* | |
* @param {string} template - String with {} denoted keys. (See example) | |
* @param {object} args - Key/value pairs matching the holes in the template. | |
* @return {string} | |
* | |
* @example | |
* format('This is a {language} ({abbr}) codebase.', { | |
* language: 'JavaScript', | |
* abbr: 'JS' | |
* }); | |
* // 'This is a JavaScript (JS) codebase.' | |
*/ | |
function format(template, args) { | |
return template.replace(/{([^}]*)}/g, function(match, key) { | |
return args[key] || match; | |
}); | |
} | |
module.exports.format = format; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment