urlMatches now supports an array

This commit is contained in:
2023-03-28 22:40:02 +10:00
parent 320516fd8d
commit 3a562005e5

View File

@@ -5,27 +5,71 @@ export const urlStripAttributes = (url: string): string => {
return urlObject.toString(); return urlObject.toString();
}; };
export const urlMatches = (fullUrl: string, testPath: string): boolean => { export const urlMatches = (
fullUrl: string,
testPath: string | string[]
): boolean | number => {
// Remove query string and fragment identifier from both URLs // Remove query string and fragment identifier from both URLs
const urlWithoutParams = fullUrl.split(/[?#]/)[0]; const urlWithoutParams = fullUrl.split(/[?#]/)[0];
const pathWithoutParams = testPath.split(/[?#]/)[0];
// Remove trailing slashes from both URLs if (Array.isArray(testPath)) {
const trimmedUrl = urlWithoutParams.replace(/\/$/, ""); // Iterate over the array of test paths and return the index of the first matching path
const trimmedPath = pathWithoutParams.replace(/\/$/, ""); for (let i = 0; i < testPath.length; i++) {
const pathWithoutParams = testPath[i].split(/[?#]/)[0];
// Remove trailing slashes from both URLs
const trimmedUrl = urlWithoutParams.replace(/\/$/, "");
const trimmedPath = pathWithoutParams.replace(/\/$/, "");
// Check if both URLs contain a domain and port
const hasDomainAndPort =
/^https?:\/\/[^/]+\//.test(trimmedUrl) &&
/^https?:\/\/[^/]+\//.test(trimmedPath);
// Check if both URLs contain a domain and port if (hasDomainAndPort) {
const hasDomainAndPort = // Do a full test with both URLs
/^https?:\/\/[^/]+\//.test(trimmedUrl) && if (trimmedUrl === trimmedPath) {
/^https?:\/\/[^/]+\//.test(trimmedPath); return i;
}
if (hasDomainAndPort) { } else {
// Do a full test with both URLs // Remove the domain and test the paths
return trimmedUrl === trimmedPath; const urlWithoutDomain = trimmedUrl.replace(
/^https?:\/\/[^/]+/,
""
);
const pathWithoutDomain = trimmedPath.replace(
/^https?:\/\/[^/]+/,
""
);
if (urlWithoutDomain === pathWithoutDomain) {
return i;
}
}
}
// If no matching path is found, return false
return false;
} else { } else {
// Remove the domain and test the paths const pathWithoutParams = testPath.split(/[?#]/)[0];
const urlWithoutDomain = trimmedUrl.replace(/^https?:\/\/[^/]+/, ""); // Remove trailing slashes from both URLs
const pathWithoutDomain = trimmedPath.replace(/^https?:\/\/[^/]+/, ""); const trimmedUrl = urlWithoutParams.replace(/\/$/, "");
return urlWithoutDomain === pathWithoutDomain; const trimmedPath = pathWithoutParams.replace(/\/$/, "");
// Check if both URLs contain a domain and port
const hasDomainAndPort =
/^https?:\/\/[^/]+\//.test(trimmedUrl) &&
/^https?:\/\/[^/]+\//.test(trimmedPath);
if (hasDomainAndPort) {
// Do a full test with both URLs
return trimmedUrl === trimmedPath;
} else {
// Remove the domain and test the paths
const urlWithoutDomain = trimmedUrl.replace(
/^https?:\/\/[^/]+/,
""
);
const pathWithoutDomain = trimmedPath.replace(
/^https?:\/\/[^/]+/,
""
);
return urlWithoutDomain === pathWithoutDomain;
}
} }
}; };