Skip to content

Commit

Permalink
Merge pull request #246 from givanse/refactor-parse-url
Browse files Browse the repository at this point in the history
refactor: extract parseURL
  • Loading branch information
stefanpenner authored Apr 10, 2019
2 parents 9322347 + 196a4c7 commit 142e3f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
49 changes: 1 addition & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,7 @@
import RouteRecognizer from 'route-recognizer';
import FakeXMLHttpRequest from 'fake-xml-http-request';
import * as FakeFetch from 'whatwg-fetch';

/**
* parseURL - decompose a URL into its parts
* @param {String} url a URL
* @return {Object} parts of the URL, including the following
*
* 'https://www.yahoo.com:1234/mypage?test=yes#abc'
*
* {
* host: 'www.yahoo.com:1234',
* protocol: 'https:',
* search: '?test=yes',
* hash: '#abc',
* href: 'https://www.yahoo.com:1234/mypage?test=yes#abc',
* pathname: '/mypage',
* fullpath: '/mypage?test=yes'
* }
*/
function parseURL(url) {
// TODO: something for when document isn't present... #yolo
var anchor = document.createElement('a');
anchor.href = url;

if (!anchor.host) {
anchor.href = anchor.href; // IE: load the host and protocol
}

var pathname = anchor.pathname;
if (pathname.charAt(0) !== '/') {
pathname = '/' + pathname; // IE: prepend leading slash
}

var host = anchor.host;
if (anchor.port === '80' || anchor.port === '443') {
host = anchor.hostname; // IE: remove default port
}

return {
host: host,
protocol: anchor.protocol,
search: anchor.search,
hash: anchor.hash,
href: anchor.href,
pathname: pathname,
fullpath: pathname + (anchor.search || '') + (anchor.hash || '')
};
}

import parseURL from './parse-url';

/**
* Registry
Expand Down
46 changes: 46 additions & 0 deletions src/parse-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* parseURL - decompose a URL into its parts
* @param {String} url a URL
* @return {Object} parts of the URL, including the following
*
* 'https://www.yahoo.com:1234/mypage?test=yes#abc'
*
* {
* host: 'www.yahoo.com:1234',
* protocol: 'https:',
* search: '?test=yes',
* hash: '#abc',
* href: 'https://www.yahoo.com:1234/mypage?test=yes#abc',
* pathname: '/mypage',
* fullpath: '/mypage?test=yes'
* }
*/
export default function parseURL(url: string) {
// TODO: something for when document isn't present... #yolo
var anchor = document.createElement('a');
anchor.href = url;

if (!anchor.host) {
anchor.href = anchor.href; // IE: load the host and protocol
}

var pathname = anchor.pathname;
if (pathname.charAt(0) !== '/') {
pathname = '/' + pathname; // IE: prepend leading slash
}

var host = anchor.host;
if (anchor.port === '80' || anchor.port === '443') {
host = anchor.hostname; // IE: remove default port
}

return {
host: host,
protocol: anchor.protocol,
search: anchor.search,
hash: anchor.hash,
href: anchor.href,
pathname: pathname,
fullpath: pathname + (anchor.search || '') + (anchor.hash || '')
};
}

0 comments on commit 142e3f5

Please sign in to comment.