Skip to content

Commit

Permalink
Extract the regex declaration outside the function (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
morellodev authored Aug 11, 2021
1 parent 8a882db commit 84ecee4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions index.js
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);
}

0 comments on commit 84ecee4

Please sign in to comment.