Skip to content

Instantly share code, notes, and snippets.

@fsschmitt
Last active July 26, 2016 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsschmitt/b48db17397499282ff8c36d73a36a8af to your computer and use it in GitHub Desktop.
Save fsschmitt/b48db17397499282ff8c36d73a36a8af to your computer and use it in GitHub Desktop.
/*
* Crockford's Supplant method with support to string, numbers and booleans
*
* var myObj = { name: "John Doe", age: 27 }
* console.log("Hi my name is {name} and I am {age} years old.".supplant(myObj));
*
* // Output: Hi my name is John Doe and I am 27 years old.
*/
if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' || typeof r === 'boolean' ? r : a;
}
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment