Skip to content

Instantly share code, notes, and snippets.

@ryanseys
Created September 16, 2014 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanseys/856471207aa3f80ff614 to your computer and use it in GitHub Desktop.
Save ryanseys/856471207aa3f80ff614 to your computer and use it in GitHub Desktop.
util.format
/**
* 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