Skip to content

Commit

Permalink
refactor: remove url-toolkit dependency (#38)
Browse files Browse the repository at this point in the history
* refactor: remove url-toolkit dependency

* use https for default placeholder url
  • Loading branch information
mister-ben authored Jul 6, 2024
1 parent 61b8db9 commit e124470
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"global": "^4.4.0",
"url-toolkit": "^2.2.1"
"global": "^4.4.0"
},
"devDependencies": {
"@babel/cli": "^7.12.8",
Expand Down
36 changes: 12 additions & 24 deletions src/resolve-url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import URLToolkit from 'url-toolkit';
import window from 'global/window';

const DEFAULT_LOCATION = 'http://example.com';
const DEFAULT_LOCATION = 'https://example.com';

const resolveUrl = (baseUrl, relativeUrl) => {
// return early if we don't need to resolve
Expand All @@ -14,37 +13,26 @@ const resolveUrl = (baseUrl, relativeUrl) => {
baseUrl = window.location && window.location.href || '';
}

// IE11 supports URL but not the URL constructor
// feature detect the behavior we want
const nativeURL = typeof window.URL === 'function';

const protocolLess = (/^\/\//.test(baseUrl));
// remove location if window.location isn't available (i.e. we're in node)
// and if baseUrl isn't an absolute url
const removeLocation = !window.location && !(/\/\//i).test(baseUrl);

// if the base URL is relative then combine with the current location
if (nativeURL) {
baseUrl = new window.URL(baseUrl, window.location || DEFAULT_LOCATION);
} else if (!(/\/\//i).test(baseUrl)) {
baseUrl = URLToolkit.buildAbsoluteURL(window.location && window.location.href || '', baseUrl);
}
baseUrl = new window.URL(baseUrl, window.location || DEFAULT_LOCATION);

if (nativeURL) {
const newUrl = new URL(relativeUrl, baseUrl);
const newUrl = new URL(relativeUrl, baseUrl);

// if we're a protocol-less url, remove the protocol
// and if we're location-less, remove the location
// otherwise, return the url unmodified
if (removeLocation) {
return newUrl.href.slice(DEFAULT_LOCATION.length);
} else if (protocolLess) {
return newUrl.href.slice(newUrl.protocol.length);
}

return newUrl.href;
// if we're a protocol-less url, remove the protocol
// and if we're location-less, remove the location
// otherwise, return the url unmodified
if (removeLocation) {
return newUrl.href.slice(DEFAULT_LOCATION.length);
} else if (protocolLess) {
return newUrl.href.slice(newUrl.protocol.length);
}
return URLToolkit.buildAbsoluteURL(baseUrl, relativeUrl);

return newUrl.href;

};

Expand Down

0 comments on commit e124470

Please sign in to comment.