Skip to content

Instantly share code, notes, and snippets.

@vgerbase
Last active July 4, 2019 10:16
Show Gist options
  • Save vgerbase/d98663dd8b49dd3368f9e258de1a2529 to your computer and use it in GitHub Desktop.
Save vgerbase/d98663dd8b49dd3368f9e258de1a2529 to your computer and use it in GitHub Desktop.
String.prototype.formatUnicorn = String.prototype.formatUnicorn ||
function () {
"use strict";
var str = this.toString();
if (arguments.length) {
var t = typeof arguments[0];
var key;
var args = ("string" === t || "number" === t) ?
Array.prototype.slice.call(arguments)
: arguments[0];
for (key in args) {
str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), args[key]);
}
}
return str;
};
@vgerbase
Copy link
Author

vgerbase commented Jul 4, 2019

Usage: "Hello, {name}, are you feeling {adjective}?".formatUnicorn({name:"Joe", adjective: "OK"});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment