Skip to content

Commit

Permalink
Greentest: matchesSavedMap with matchDomainOnly without host prefix s…
Browse files Browse the repository at this point in the history
…hould match url without path
  • Loading branch information
LoveIsGrief committed Oct 16, 2019
1 parent f4598b5 commit 53e24da
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ export const pathMatch = (url, map) => {
return true;
};

/**
*
* @param url {URL}
* @return {string}
*/
export const urlKeyFromUrl = (url) => {
const parsedUrl = new window.URL(url);
return punycode.toUnicode(parsedUrl.hostname.replace('www.', '')) + parsedUrl.pathname;
return punycode.toUnicode(url.hostname.replace('www.', '')) + url.pathname;
};

/**
Expand All @@ -72,8 +76,10 @@ export const urlKeyFromUrl = (url) => {
*/
export const matchesSavedMap = (url, matchDomainOnly, {host}) => {
let toMatch = url;
let urlO = new window.URL(url);
if (matchDomainOnly) {
toMatch = new window.URL(url).host;
toMatch = urlO.host;
urlO = new window.URL(`${urlO.protocol}//${urlO.host}`);
}

if (host[0] === PREFIX_REGEX) {
Expand All @@ -92,7 +98,7 @@ export const matchesSavedMap = (url, matchDomainOnly, {host}) => {
.replace(/\?/g, '.?'))
.test(toMatch);
} else {
const key = urlKeyFromUrl(toMatch);
const key = urlKeyFromUrl(urlO);
const _url = ((key.indexOf('/') === -1) ? key.concat('/') : key).toLowerCase();
const mapHost = ((host.indexOf('/') === -1) ? host.concat('/') : host).toLowerCase();
return domainMatch(_url, mapHost) && pathMatch(_url, mapHost);
Expand Down

0 comments on commit 53e24da

Please sign in to comment.