Skip to content

Commit

Permalink
refactor: remove unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoaldamav committed Feb 6, 2024
1 parent b2ae53f commit 78f612d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 133 deletions.
74 changes: 1 addition & 73 deletions network/config/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,4 @@
import { parseUri, pickSettingByUrl } from './config';

describe('parseUri', () => {
it('should parse a simple config', () => {
const uri = 'https://example.com';
const expected = {
raw: 'https://example.com/',
parsed: new URL(uri),
nerf: '//example.com/',
host: 'example.com',
hostOnlyDomain: '//example.com/',
withoutPort: 'https://example.com/',
};
const actual = parseUri(uri);
expect(actual).toEqual(expected);
});

it('should parse a config with a port', () => {
const uri = 'https://example.com:8080';
const expected = {
raw: 'https://example.com:8080/',
parsed: new URL(uri),
nerf: '//example.com:8080/',
host: 'example.com',
hostOnlyDomain: '//example.com:8080/',
withoutPort: 'https://example.com/',
};
const actual = parseUri(uri);
expect(actual).toEqual(expected);
});

it('should parse a config with a path', () => {
const uri = 'https://example.com/path/to/file';
const expected = {
raw: 'https://example.com/path/to/file',
parsed: new URL(uri),
nerf: '//example.com/path/to/file/',
host: 'example.com',
hostOnlyDomain: '//example.com/',
withoutPort: 'https://example.com/path/to/file',
};
const actual = parseUri(uri);
expect(actual).toEqual(expected);
});

it('should parse a config with a query string', () => {
const uri = 'https://example.com?foo=bar';
const expected = {
raw: 'https://example.com/?foo=bar',
parsed: new URL(uri),
nerf: '//example.com/',
host: 'example.com',
hostOnlyDomain: '//example.com/',
withoutPort: 'https://example.com/?foo=bar',
};
const actual = parseUri(uri);
expect(actual).toEqual(expected);
});

it('should parse a config with a fragment identifier', () => {
const uri = 'https://example.com#fragment';
const expected = {
raw: 'https://example.com/#fragment',
parsed: new URL(uri),
nerf: '//example.com/',
host: 'example.com',
hostOnlyDomain: '//example.com/',
withoutPort: 'https://example.com/#fragment',
};
const actual = parseUri(uri);
expect(actual).toEqual(expected);
});
});
import { pickSettingByUrl } from './config';

describe('pickSettingByUrl', () => {
test('should return undefined if generic object is undefined', () => {
Expand Down
63 changes: 5 additions & 58 deletions network/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,5 @@
import nerfDart from 'nerf-dart';

export interface ParsedUri {
/**
* The config as string
*/
raw: string;
/**
* The parsed config
*/
parsed: URL;
/**
* The nerf dart of the config
* @example https://example.com -> //example.com/
* @example https://example.com:8080/path/to/file -> //example.com:8080/path/to/
*/
nerf: string;
/**
* The host of the config
* @example https://example.com -> example.com
*/
host: string;
/**
* The host of the config with port
* @example https://example.com:8080 -> //example.com:8080/
*/
hostOnlyDomain: string;
/**
* The config without port
* @example https://example.com:8080/path/to/file -> https://example.com/path/to/file
*/
withoutPort: string;
}

export function parseUri(uri: string): ParsedUri {
const parsed = new URL(uri);

if (!uri.endsWith('/')) {
uri += '/';
}

return {
raw: parsed.href,
parsed,
nerf: nerfDart(uri),
host: parsed.hostname,
hostOnlyDomain: convertToDomain(parsed),
withoutPort: removePort(parsed),
};
}

function getMaxParts(uris: string[]) {
return uris.reduce((max, uri) => {
const parts = uri.split('/').length;
Expand All @@ -62,7 +13,11 @@ export function pickSettingByUrl<T>(
): T | undefined {
if (!generic) return undefined;
if (generic[uri]) return generic[uri];
const { nerf, withoutPort } = parseUri(uri);
/* const { nerf, withoutPort } = parseUri(uri); */
const nerf = nerfDart(uri);
const withoutPort = removePort(new URL(uri));
if (generic[nerf]) return generic[nerf];
if (generic[withoutPort]) return generic[withoutPort];
const maxParts = getMaxParts(Object.keys(generic));
const parts = nerf.split('/');
for (let i = Math.min(parts.length, maxParts) - 1; i >= 3; i--) {
Expand All @@ -77,14 +32,6 @@ export function pickSettingByUrl<T>(
return undefined;
}

function convertToDomain(config: URL): string {
let result = `//${config.hostname}`;
if (config.port) {
result += `:${config.port}`;
}
return `${result}/`;
}

function removePort(config: URL): string {
if (config.port === '') return config.href;
config.port = '';
Expand Down
3 changes: 1 addition & 2 deletions network/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { parseUri, pickSettingByUrl } from './config';
export type { ParsedUri } from './config';
export { pickSettingByUrl } from './config';

0 comments on commit 78f612d

Please sign in to comment.