-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaa30e4
commit 10d5dc3
Showing
2 changed files
with
25 additions
and
1 deletion.
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 +1,6 @@ | ||
export const isInternalURL = (url: string): boolean => /^\/[^\/]/.test(url); | ||
export const isInternalURL = (url: string): boolean => { | ||
const isInternal = /^(\/(?!\/)|#)/.test(url); | ||
const isSpecialLink = !isInternal && !/^https?:\/\//.test(url); | ||
|
||
return isInternal && !isSpecialLink; | ||
}; |
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,19 @@ | ||
import test from "ava"; | ||
|
||
import { isInternalURL } from "../src/lib/isInternalURL"; | ||
|
||
test("returns true for internal URLs", (t) => { | ||
t.true(isInternalURL("/")); | ||
t.true(isInternalURL("/internal")); | ||
t.true(isInternalURL("#anchor")); | ||
}); | ||
|
||
test("returns false for external URLs", (t) => { | ||
t.false(isInternalURL("//example.com")); | ||
t.false(isInternalURL("//example.com/image.png")); | ||
t.false(isInternalURL("//example.com#anchor")); | ||
t.false(isInternalURL("https://example.com")); | ||
t.false(isInternalURL("mailto:example.com")); | ||
t.false(isInternalURL("tel:example.com")); | ||
t.false(isInternalURL("ftp:example.com")); | ||
}); |