Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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