Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzal3x committed Jul 22, 2024
1 parent 39dfd2c commit a54f6d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions portal/src/helpers/domain_parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ const getSubdomainAndPathTestCases: [string, Path][] = [
['http://name.localhost:8080/', { subdomain: 'name', path: '/index.html' }],
['http://flatland.localhost:8080/', { subdomain: 'flatland', path: '/index.html' }],
['http://subname.suinsname.localhost:8080/',
{ subdomain: 'subname.suinsname', path: '/index.html' }],
{ subdomain: 'subname.suinsname', path: '/index.html' }],
['https://subsubname.subname.suinsname.portalname.co.uk/',
{ subdomain: 'subsubname.subname.suinsname', path: '/index.html' }],
{ subdomain: 'subsubname.subname.suinsname', path: '/index.html' }],
['http://docs.localhost/css/print.css', { subdomain: 'docs', path: '/css/print.css' }],
['http://docs.localhost/assets/index-a242f32b.js',
{ subdomain: 'docs', path: '/assets/index-a242f32b.js'}]
{ subdomain: 'docs', path: '/assets/index-a242f32b.js'}]
]

describe('getSubdomainAndPath', () => {
getSubdomainAndPathTestCases.forEach(
([input, path]) => {
test(`${input} ->
subdomain: ${path.subdomain ?? "null"},
path: ${path.path ?? "null"}`,
() => {
expect(getSubdomainAndPath(new URL(input))).toEqual(path);
});
});
getSubdomainAndPathTestCases.forEach(
([input, path]) => {
test(`${input} ->
subdomain: ${path.subdomain ?? "null"},
path: ${path.path ?? "null"}`,
() => {
expect(getSubdomainAndPath(new URL(input))).toEqual(path);
});
});
})
36 changes: 18 additions & 18 deletions portal/src/helpers/domain_parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import { Path } from "../types/index";
* @param orig_url The URL to extract the domain from. e.g. "https://example.com"
* @returns The domain of the URL. e.g. "example.com"
*/
export function getDomain(url: URL): string {
const parsed = parseDomain(url.hostname);
if (parsed.type === ParseResultType.Listed) {
const domain = parsed.domain + "." + parsed.topLevelDomains.join(".");
return domain;
} else if (parsed.type === ParseResultType.Reserved) {
return parsed.labels[parsed.labels.length - 1];
} else {
console.error("Error while parsing domain name:", parsed);
throw new Error("Error while parsing domain name");
}
}
export function getDomain(url: URL): string {
const parsed = parseDomain(url.hostname);
if (parsed.type === ParseResultType.Listed) {
const domain = parsed.domain + "." + parsed.topLevelDomains.join(".");
return domain;
} else if (parsed.type === ParseResultType.Reserved) {
return parsed.labels[parsed.labels.length - 1];
} else {
console.error("Error while parsing domain name:", parsed);
throw new Error("Error while parsing domain name");
}
}

/**
* Given a URL, returns the subdomain and path.
Expand All @@ -32,14 +32,14 @@ export function getSubdomainAndPath(url: URL): Path | null {
const parsed = parseDomain(url.hostname);
if (parsed.type === ParseResultType.Listed) {
return {
subdomain: parsed.subDomains.join("."),
path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname)
subdomain: parsed.subDomains.join("."),
path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname)
} as Path;
} else if ( parsed.type === ParseResultType.Reserved) {
return {
subdomain: parsed.labels.slice(0, parsed.labels.length-1).join('.'),
path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname)
} as Path;
return {
subdomain: parsed.labels.slice(0, parsed.labels.length-1).join('.'),
path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname)
} as Path;
}
return null;
}
Expand Down

0 comments on commit a54f6d9

Please sign in to comment.