Created
December 13, 2012 16:54
-
-
Save gabrielmancini/4277891 to your computer and use it in GitHub Desktop.
to avoid contatenação strings (the idea is just to avoid the possibility of errors). follows an extension of the String Object
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) { | |
return this.replace(/{([^{}]*)}/g, | |
function (a, b) { | |
var r = o[b]; | |
return typeof r === 'string' || typeof r === 'number' ? r : a; | |
} | |
); | |
}; | |
// Ho to use: | |
var nome = 'lalala'; | |
var index += 1; | |
"{name}[{index}]".supplant({ name: nome, index: index}) | |
//Enjoy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment