Skip to content

Instantly share code, notes, and snippets.

@rmcvey
Created January 22, 2013 17:20
Show Gist options
  • Save rmcvey/4596417 to your computer and use it in GitHub Desktop.
Save rmcvey/4596417 to your computer and use it in GitHub Desktop.
personally, I feel like a library is frequently (but not always) overkill when all you want is variable substitution
String.prototype.supplant = function (o) {
o = o || {};
return this.replace(/{([^{}]*)}/g, function(bracketed, clean){
var object_value = o[clean];
return ['string', 'number'].indexOf((typeof object_value)) > -1 ? object_value : bracketed;
})
}
//example
var text = "{name} is feeling {feeling}".supplant({
name: "Rob",
feeling: "Good"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment