-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the regex declaration outside the function (#16)
- Loading branch information
1 parent
8a882db
commit 84ecee4
Showing
1 changed file
with
9 additions
and
5 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 |
---|---|---|
@@ -1,14 +1,18 @@ | ||
// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1 | ||
// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3 | ||
const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/; | ||
|
||
// Windows paths like `c:\` | ||
const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/; | ||
|
||
export default function isAbsoluteUrl(url) { | ||
if (typeof url !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``); | ||
} | ||
|
||
// Don't match Windows paths `c:\`. | ||
if (/^[a-zA-Z]:\\/.test(url)) { | ||
if (WINDOWS_PATH_REGEX.test(url)) { | ||
return false; | ||
} | ||
|
||
// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1 | ||
// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3 | ||
return /^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(url); | ||
return ABSOLUTE_URL_REGEX.test(url); | ||
} |