Created
October 28, 2011 04:27
-
-
Save jaseemabid/1321623 to your computer and use it in GitHub Desktop.
JavaScript supplants
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
/* Supplant for templates and data filling */ | |
if(typeof String.prototype.supplant !== 'function') { | |
String.prototype.supplant = function(o) { | |
return this.replace(/{([^{}]*)}/g, | |
function (a,b) { | |
var r = o[b]; | |
return typeof r === 'string' ? | |
r : a; | |
}); | |
}; | |
} | |
/* You need a template and data */ | |
template = "this is {data} " | |
data = {"data" : "value"} | |
result = template.supplant(data); | |
/* Result will be "this is value" */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment