Skip to content

Instantly share code, notes, and snippets.

@friedemannsommer
Last active April 2, 2016 00:29
Show Gist options
  • Save friedemannsommer/455aa354d12fddb102cf30843f2c2294 to your computer and use it in GitHub Desktop.
Save friedemannsommer/455aa354d12fddb102cf30843f2c2294 to your computer and use it in GitHub Desktop.
function parseUrl(uri) {
// @source http://jsperf.com/url-parsing/34
var matches = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/.exec(uri);
return {
href: uri || '',
host: matches[10] || '',
hash: matches[17] || '',
port: matches[12] || '',
origin: (matches[4] || '') + (matches[5] || '') + (matches[10] || ''),
search: matches[16] || '',
protocol: matches[4] || '',
pathname: matches[13] || '',
password: matches[9] || '',
username: matches[8] || '',
hostname: matches[11] || ''
};
}
declare interface UrlParseResponseObject extends Object {
hash: string
search: string
pathname: string
port: string
hostname: string
host: string
password: string
username: string
protocol: string
origin: string
href: string
}
function parseUrl(uri:string):UrlParseResponseObject {
// @source http://jsperf.com/url-parsing/34
let matches:Array<string> = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/.exec(uri);
return {
href: uri || '',
host: matches[10] || '',
hash: matches[17] || '',
port: matches[12] || '',
origin: (matches[4] || '') + (matches[5] || '') + (matches[10] || ''),
search: matches[16] || '',
protocol: matches[4] || '',
pathname: matches[13] || '',
password: matches[9] || '',
username: matches[8] || '',
hostname: matches[11] || ''
};
}
export default parseUrl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment