halley
Version:
A bayeux client for modern browsers and node. Forked from Faye
78 lines (61 loc) • 2.1 kB
JavaScript
;
/* node-safe reference to the window */
var win = typeof window === 'object' && window; // jshint ignore:line
module.exports = {
parse: function(url) {
if (typeof url !== 'string') return url;
var uri = {}, parts, query, pairs, i, n, data;
var consume = function(name, pattern) {
url = url.replace(pattern, function(match) {
uri[name] = match;
return '';
});
uri[name] = uri[name] || '';
};
consume('protocol', /^[a-z]+\:/i);
consume('host', /^\/\/[^\/\?#]+/);
if (!/^\//.test(url) && !uri.host)
url = win.location.pathname.replace(/[^\/]*$/, '') + url;
consume('pathname', /^[^\?#]*/);
consume('search', /^\?[^#]*/);
consume('hash', /^#.*/);
uri.protocol = uri.protocol || win.location.protocol;
if (uri.host) {
uri.host = uri.host.substr(2);
parts = uri.host.split(':');
uri.hostname = parts[0];
uri.port = parts[1] || '';
} else {
uri.host = win.location.host;
uri.hostname = win.location.hostname;
uri.port = win.location.port;
}
uri.pathname = uri.pathname || '/';
uri.path = uri.pathname + uri.search;
query = uri.search.replace(/^\?/, '');
pairs = query ? query.split('&') : [];
data = {};
for (i = 0, n = pairs.length; i < n; i++) {
parts = pairs[i].split('=');
data[decodeURIComponent(parts[0] || '')] = decodeURIComponent(parts[1] || '');
}
uri.query = data;
uri.href = this.stringify(uri);
return uri;
},
stringify: function(uri) {
var string = uri.protocol + '//' + uri.hostname;
if (uri.port) string += ':' + uri.port;
string += uri.pathname + this.queryString(uri.query) + (uri.hash || '');
return string;
},
queryString: function(query) {
var pairs = [];
for (var key in query) {
if (!query.hasOwnProperty(key)) continue;
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(query[key]));
}
if (pairs.length === 0) return '';
return '?' + pairs.join('&');
}
};