Last active
July 26, 2016 14:39
-
-
Save fsschmitt/b48db17397499282ff8c36d73a36a8af to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* 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