fix: lib functions related to URL/links
This commit is contained in:
parent
f7ab85a108
commit
e8adbab368
@ -1,7 +1,7 @@
|
|||||||
export function normalizeURL(input: string): string {
|
export function normalizeURL(input: string, absolute: boolean = true): string {
|
||||||
try {
|
try {
|
||||||
// try to create a URL object
|
// try to create a URL object
|
||||||
const url = new URL(input, window.location.href);
|
const url = absolute ? new URL(input) : new URL(input, window.location.href);
|
||||||
// if the URL is valid, return it
|
// if the URL is valid, return it
|
||||||
return url.href;
|
return url.href;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -16,15 +16,15 @@ export default function validLink(link: string) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (finalURL.host.endsWith(".")) return false;
|
||||||
if (
|
if (
|
||||||
validTLD(finalURL.host) ||
|
validTLD(finalURL.host) ||
|
||||||
isValidIPv6(finalURL.host.slice(1, finalURL.host.length - 1)) ||
|
isValidIPv6(link.slice(1, finalURL.host.length - 1)) ||
|
||||||
isValidIPv4(finalURL.host)
|
isValidIPv4(link)
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validTLD(domain: string): boolean {
|
export function validTLD(domain: string): boolean {
|
||||||
|
80
test/validIP.test.ts
Normal file
80
test/validIP.test.ts
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
import { describe, expect, test } from "bun:test";
|
||||||
|
import { isValidIPv4, } from "../lib/url/validLink";
|
||||||
|
|
||||||
|
describe("Check if a string is a valid IPv4", () => {
|
||||||
|
test("Invalid IPv4 addresses - Missing part", () => {
|
||||||
|
expect(isValidIPv4("192.168.1")).toBe(false);
|
||||||
|
expect(isValidIPv4("1")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains non-digit characters", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.a")).toBe(false);
|
||||||
|
expect(isValidIPv4("192.168.1.1a")).toBe(false);
|
||||||
|
expect(isValidIPv4("192.168.1.1.1")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains empty parts", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.")).toBe(false);
|
||||||
|
expect(isValidIPv4("192..168.1")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains negative numbers", () => {
|
||||||
|
expect(isValidIPv4("192.168.-1.1")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains out-of-range numbers", () => {
|
||||||
|
expect(isValidIPv4("192.168.256.1")).toBe(false);
|
||||||
|
expect(isValidIPv4("192.168.1.256")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains multiple dots", () => {
|
||||||
|
expect(isValidIPv4("192.168..1.1")).toBe(false);
|
||||||
|
expect(isValidIPv4("192.168.1..1")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Empty string", () => {
|
||||||
|
expect(isValidIPv4("")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains special characters", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.1!")).toBe(false);
|
||||||
|
expect(isValidIPv4("192.168.1.1 ")).toBe(false);
|
||||||
|
expect(isValidIPv4("192.168.1.1\t")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains non-ASCII characters", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.1©")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains newline character", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.1\n")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains multiple IP addresses", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.1 192.168.1.2")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Invalid IPv4 addresses - Contains IPv6 address", () => {
|
||||||
|
expect(isValidIPv4("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Valid IPv4 addresses - Standard IP", () => {
|
||||||
|
expect(isValidIPv4("192.168.1.1")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Valid IPv4 addresses - All zeros", () => {
|
||||||
|
expect(isValidIPv4("0.0.0.0")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Valid IPv4 addresses - Maximum value", () => {
|
||||||
|
expect(isValidIPv4("255.255.255.255")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Valid IPv4 addresses - Leading zero but valid", () => {
|
||||||
|
expect(isValidIPv4("192.000.001.001")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Valid IPv4 addresses - Single digit parts", () => {
|
||||||
|
expect(isValidIPv4("1.2.3.4")).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
@ -26,6 +26,10 @@ describe("Check if a string is an accessible domain/URL/IP", () => {
|
|||||||
test("Invalid TLD with no protocol", () => {
|
test("Invalid TLD with no protocol", () => {
|
||||||
expect(validLink("www.example.notexist")).toBe(false);
|
expect(validLink("www.example.notexist")).toBe(false);
|
||||||
});
|
});
|
||||||
|
test("Dot suffix", () => {
|
||||||
|
expect(validLink(".")).toBe(false);
|
||||||
|
expect(validLink("anything.")).toBe(false);
|
||||||
|
});
|
||||||
test("IPv4 without protocol", () => {
|
test("IPv4 without protocol", () => {
|
||||||
expect(validLink("127.0.0.1")).toBe(true);
|
expect(validLink("127.0.0.1")).toBe(true);
|
||||||
});
|
});
|
||||||
@ -38,6 +42,9 @@ describe("Check if a string is an accessible domain/URL/IP", () => {
|
|||||||
test("Not a valid host/URL.", () => {
|
test("Not a valid host/URL.", () => {
|
||||||
expect(validLink("weather")).toBe(false);
|
expect(validLink("weather")).toBe(false);
|
||||||
});
|
});
|
||||||
|
test("Not a valid IP", () => {
|
||||||
|
expect(validLink("1")).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reference: https://www.iana.org/domains/root/db
|
// Reference: https://www.iana.org/domains/root/db
|
||||||
|
Loading…
Reference in New Issue
Block a user