Skip to content

Instantly share code, notes, and snippets.

@rbergman
Created September 2, 2010 16:47
Show Gist options
  • Save rbergman/562533 to your computer and use it in GitHub Desktop.
Save rbergman/562533 to your computer and use it in GitHub Desktop.
/*
* Copyright 2008, Robert Bergman.
* This code may be freely distributed under an MIT-style licence.
*
* A jQuery plugin to make URI parsing and creation from templates and values easy.
*
* Use:
* var uri = jQuery.uri();
* Creates a new, empty URI object
*
* var uri = jQuery.uri('http://example.com')
* Creates a new URI object parsed from the passed URI string
*
* var uri = jQuery.uri('http://example.com:8080/foo?bar={bar}', {bar: 'barvalue'})
* Creates a new URI object from the template string after replacing '{bar}' with 'barvalue'
*
* The following methods are available. The notation '[value]' indicates optional setter behavior:
*
* uri.protocol([value])
* uri.username([value])
* uri.password([value])
* uri.host([value])
* uri.port([value])
* uri.path([value])
* uri.query([value])
* uri.fragment([value])
* uri.param(key, [value])
* uri.params([value])
* uri.toString()
*/
jQuery.uri = (function () {
var uriRe = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+)?:?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/,
map = {protocol: 2, username: 4, password: 5, host: 6, port: 7, path: 8, query: 9, fragment: 10},
tos = '{$protocol://}{$username[:$password]@}{$host[:$port]}{$path}{?$query}{#$fragment}',
tosRes = [/(\[([^$]*)\$(\w+)(.*?)\])/g, /(\{([^$]*)\$(\w+)(.*?)\})/g],
tmplRe = /\{(\w+)\}/g;
return function (config, values) {
var f = {}, uri = {}, match;
if (typeof config === 'string') {
config = config.replace(tmplRe, function ($0, $1) {
return values ? (values[$1] ? values[$1] : '') : '';
});
if (!(match = uriRe.exec(config))) {
throw 'Invalid URI: ' + config;
}
}
jQuery.each(map, function (k, v) {
uri[k] = function (val) { return typeof val !== 'undefined' ? (f[k] = val) : f[k]; };
if (!uri[k]((match && match[v])) && config && config[k]) config[k]();
});
(uri.query = function (val) {
if (typeof val !== 'undefined') {
f.params = {};
if (val) {
jQuery.each(val.split('&'), function (i, p) {
var nv = p.split('='), dc = decodeURIComponent, n = dc(nv[0]), v = dc(nv[1]);
(f.params[n] = f.params[n] || []).push(v);
});
}
}
return val ? f.params : jQuery.param(f.params || {}, true);
})(f.query);
return jQuery.extend(uri, {
params: function (val) {
return typeof val !== 'undefined' ? (f.params = val) : f.params;
},
param: function (name, val) {
var vals = (f.params[name] = val ? f.params[name] || [] : []);
return typeof val !== 'undefined' ? vals[vals.length + 1] = val : vals[name];
},
toString: function () {
var s = tos;
jQuery.each(tosRes, function (i, tosRe) {
s = s.replace(tosRe, function ($0, $1, $2, $3, $4) {
var p = uri[$3]();
return p ? [($2 || ''), p, ($4 || '')].join('') : '';
});
});
return s;
}
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment