-
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.
1. Configuration to ignore protocol 2. Test coverage 100%
- Loading branch information
1 parent
53c08c7
commit da9b108
Showing
7 changed files
with
144 additions
and
29 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
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
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
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,15 @@ | ||
interface LocalConfig { | ||
/** | ||
* default false. | ||
* | ||
* if `ignoreProtocol` is true, it will validate urls without https:// or http:// as fine. | ||
* | ||
* if `ignoreProtocol` is false (default), it will validate urls without https:// or http:// as not fine | ||
*/ | ||
ignoreProtocol: boolean; | ||
} | ||
|
||
/** | ||
* link validator configuration for custom validator | ||
*/ | ||
export interface LinkValidatorConfig extends Partial<LocalConfig> {} |
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,4 @@ | ||
export const REGEX_VALID_URL = | ||
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g; | ||
|
||
export const REGEX_PREPEND_PROTOCOL = /^https?:\/\//i; |
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
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 |
---|---|---|
@@ -1,20 +1,30 @@ | ||
import { REGEX_PREPEND_PROTOCOL, REGEX_VALID_URL } from './constants'; | ||
|
||
/** | ||
* Format a string url with URL class | ||
* | ||
* @param url url to be reformed | ||
* @param includeProtocol validate url with or without protocol | ||
* @returns {URL | null} URL formatted object | ||
*/ | ||
export function reformUrl(url: string): URL | null { | ||
export function reformUrl(url: string, includeProtocol?: boolean): URL | null { | ||
try { | ||
return new URL(url.trim()); | ||
let localUrl = url; | ||
if (typeof includeProtocol === 'boolean' && includeProtocol === true) { | ||
localUrl = !REGEX_PREPEND_PROTOCOL.test(localUrl) ? `http://${localUrl}` : localUrl; | ||
} | ||
return new URL(localUrl.trim()); | ||
} catch (_e) { | ||
return null; | ||
} | ||
} | ||
|
||
const REGEX_VALID_URL = | ||
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g; | ||
|
||
/** | ||
* validates a url using javascript regex | ||
* | ||
* @param url url to validate | ||
* @returns true / false whether a given url is valid | ||
*/ | ||
export function isValidUrl(url: string): boolean { | ||
return url.match(REGEX_VALID_URL) !== null; | ||
} |