From 196a4c76681a6d33124aa1291d1c111a883a65aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20I=2E=20Silva?= Date: Sat, 5 Jan 2019 09:54:52 -0800 Subject: [PATCH] refactor: extract parseURL --- src/index.ts | 49 +----------------------------------------------- src/parse-url.ts | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 48 deletions(-) create mode 100644 src/parse-url.ts diff --git a/src/index.ts b/src/index.ts index 17cc35b..6dcf547 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,54 +2,7 @@ import self from './iife-self-placeholder'; 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 diff --git a/src/parse-url.ts b/src/parse-url.ts new file mode 100644 index 0000000..fe4b41c --- /dev/null +++ b/src/parse-url.ts @@ -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 || '') + }; +}