diff --git a/src/url/get-domain-without-www.test.ts b/src/url/get-domain-without-www.test.ts new file mode 100644 index 0000000..536db16 --- /dev/null +++ b/src/url/get-domain-without-www.test.ts @@ -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); + }); +}); diff --git a/src/url/get-domain-without-www.ts b/src/url/get-domain-without-www.ts new file mode 100644 index 0000000..1476823 --- /dev/null +++ b/src/url/get-domain-without-www.ts @@ -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; +} diff --git a/src/url/index.ts b/src/url/index.ts new file mode 100644 index 0000000..ca88783 --- /dev/null +++ b/src/url/index.ts @@ -0,0 +1,2 @@ +export * from './is-valid-url'; +export * from './get-domain-without-www'; diff --git a/src/url/is-valid-url.test.ts b/src/url/is-valid-url.test.ts new file mode 100644 index 0000000..226433e --- /dev/null +++ b/src/url/is-valid-url.test.ts @@ -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); + }); +}); diff --git a/src/url/is-valid-url.ts b/src/url/is-valid-url.ts new file mode 100644 index 0000000..ebef255 --- /dev/null +++ b/src/url/is-valid-url.ts @@ -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; + } +}