Skip to content

Instantly share code, notes, and snippets.

@mrmartineau
Last active December 31, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmartineau/7926397 to your computer and use it in GitHub Desktop.
Save mrmartineau/7926397 to your computer and use it in GitHub Desktop.
String.substitute and example used on the uk.thebar.com
String.prototype.substitute = function (object) {
return this.replace(/\{(.+?)\}/g, function (match, name) {
return name in object ? object[name] : match;
});
};
var query = this.options.queryUrl.substitute({
spirits: this.element.find('.ingredients-list-spirits .is-selected').map(function() {
return $(this).data('value');
}).get().join(),
mixers: this.element.find('.ingredients-list-mixers .is-selected').map(function() {
return $(this).data('value');
}).get().join(),
garnishes: this.element.find('.ingredients-list-garnishes .is-selected').map(function() {
return $(this).data('value');
}).get().join()
});
$(this).attr('href',query.substitute({ type : $(this).attr('data-type') }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment