Skip to content

Commit

Permalink
Merge pull request #43 from behzadam/42-add-url-utility-functions
Browse files Browse the repository at this point in the history
42 add url utility functions
  • Loading branch information
behzadam committed Jul 11, 2023
2 parents 829126b + 4654c30 commit 8e329b5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/url/get-domain-without-www.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getDomainWithoutWWW } from './get-domain-without-www';

describe('getDomainWithoutWWW', () => {
test.each`
input | expected
${'https://www.example.com'} | ${'example.com'}
${'http://google.com'} | ${'google.com'}
${'https://www.amazon.co.uk/books'} | ${'amazon.co.uk'}
${'https://'} | ${null}
`('returns $expected when input is: $input', ({ input, expected }) => {
expect(getDomainWithoutWWW(input)).toBe(expected);
});
});
13 changes: 13 additions & 0 deletions src/url/get-domain-without-www.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isValidUrl } from './is-valid-url';

/**
* Extracts domain name from a URL string.
* @param url - the given url
* @returns domain name string or null if url is not valid.
*/
export function getDomainWithoutWWW(url: string): string | null {
if (isValidUrl(url)) {
return new URL(url).hostname.replace(/^www\./, '');
}
return null;
}
2 changes: 2 additions & 0 deletions src/url/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './is-valid-url';
export * from './get-domain-without-www';
18 changes: 18 additions & 0 deletions src/url/is-valid-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isValidUrl } from './is-valid-url';

describe('isValidUrl', () => {
test.each`
input | expected
${'https://www.example.com'} | ${true}
${'http://google.com'} | ${true}
${'https://www.github.io'} | ${true}
${'https://www.amazon.co.uk/books'} | ${true}
${'http://localhost:3000'} | ${true}
${'example.com'} | ${false}
${'https://'} | ${false}
${'www.example.com'} | ${false}
${'http://256.0.0.1'} | ${false}
`('returns $expected when input is: $input', ({ input, expected }) => {
expect(isValidUrl(input)).toBe(expected);
});
});
21 changes: 21 additions & 0 deletions src/url/is-valid-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* A valid URL (Uniform Resource Locator) in the web typically consists of several components:
* 1. Protocol: This is the type of protocol used for communication, such as "http://" or "https://".
* 2. Domain: This is the unique address of a website, like "example.com" or "google.com".
* 3. Path: This is the specific location or file on the website, such as "/products" or "/about-us.html".
* 4. Query Parameters: These are optional parameters that can be added to a URL to provide additional information to the website, like "?category=books".
*
*
* @param url - the given url string
* @returns true if the URL is valid, false otherwise.
*
* @public
*/
export function isValidUrl(url: string): boolean {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}

0 comments on commit 8e329b5

Please sign in to comment.