-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from behzadam/42-add-url-utility-functions
42 add url utility functions
- Loading branch information
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |