Created
November 24, 2014 15:02
-
-
Save shorttompkins/3eef62bb84bbab274bf4 to your computer and use it in GitHub Desktop.
String.fmt
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
/** | |
* 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