Skip to content

Instantly share code, notes, and snippets.

@shorttompkins
Created November 24, 2014 15:02
Show Gist options
  • Save shorttompkins/3eef62bb84bbab274bf4 to your computer and use it in GitHub Desktop.
Save shorttompkins/3eef62bb84bbab274bf4 to your computer and use it in GitHub Desktop.
String.fmt
/**
* String.fmt
*
* String prototype for interpolating positioned arguments into a string.
*
* @param {Array} args List of strings to be inserted.
* @return {String}
*/
String.prototype.fmt = function String_fmt() {
var args = arguments,
formatted = this.toString(),
ordered = (formatted.indexOf('%@1') >= 0),
replace;
for (var i = 0, len = args.length; i < len; i += 1) {
replace = (ordered) ? '%@'+(i+1) : '%@';
formatted = formatted.replace(replace, args[i]);
}
return formatted;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment