Created
January 22, 2013 17:20
-
-
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
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
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