Skip to content

Instantly share code, notes, and snippets.

@gabrielmancini
Created December 13, 2012 16:54
Show Gist options
  • Save gabrielmancini/4277891 to your computer and use it in GitHub Desktop.
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
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