Skip to content

Commit 0d4e23f

Browse files
update url validation regex
1 parent 17879e6 commit 0d4e23f

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@primoui/utils",
33
"description": "A lightweight set of utilities",
4-
"version": "1.3.0",
4+
"version": "1.3.1",
55
"license": "MIT",
66
"type": "module",
77
"author": {

src/http/http.test.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
import { describe, expect, it } from "bun:test"
22

33
import {
4-
// Legacy aliases
5-
addHttp,
6-
addProtocol,
7-
getBaseUrl,
8-
getDomain,
9-
getQueryParams,
10-
getUrlHostname,
11-
isExternalUrl,
12-
isLocalhostUrl,
13-
isValidUrl,
14-
joinUrlPaths,
15-
normalizeUrl,
16-
removeHttp,
17-
removeProtocol,
18-
removeQueryParams,
19-
removeTrailingSlash,
20-
setQueryParams,
21-
stripTrailingSlash,
22-
stripURLSubpath
4+
addHttp,
5+
addProtocol,
6+
getBaseUrl,
7+
getDomain,
8+
getQueryParams,
9+
getUrlHostname,
10+
isExternalUrl,
11+
isLocalhostUrl,
12+
isValidUrl,
13+
joinUrlPaths,
14+
normalizeUrl,
15+
removeHttp,
16+
removeProtocol,
17+
removeQueryParams,
18+
removeTrailingSlash,
19+
setQueryParams,
20+
stripTrailingSlash,
21+
stripURLSubpath,
2322
} from "./http"
2423

2524
describe("isValidUrl", () => {
@@ -29,6 +28,14 @@ describe("isValidUrl", () => {
2928
expect(isValidUrl("https://www.example.com/path?query=value#hash")).toBe(true)
3029
})
3130

31+
it("validates URLs with longer TLDs", () => {
32+
expect(isValidUrl("https://example.storage")).toBe(true)
33+
expect(isValidUrl("https://mysite.directory")).toBe(true)
34+
expect(isValidUrl("http://www.example.storage")).toBe(true)
35+
expect(isValidUrl("https://api.myapp.directory/path")).toBe(true)
36+
expect(isValidUrl("https://subdomain.example.storage?param=value")).toBe(true)
37+
})
38+
3239
it("rejects invalid URLs", () => {
3340
expect(isValidUrl("not a url")).toBe(false)
3441
expect(isValidUrl("example.com")).toBe(false)
@@ -111,41 +118,39 @@ describe("normalizeUrl", () => {
111118
expect(normalizeUrl()).toBe("")
112119
expect(normalizeUrl("")).toBe("")
113120
})
114-
})
115121

116-
describe("stripTrailingSlash", () => {
117122
it("removes trailing slash from URL", () => {
118123
const url = "https://example.com/"
119124
const expected = "https://example.com"
120-
expect(stripTrailingSlash(url)).toBe(expected)
125+
expect(normalizeUrl(url)).toBe(expected)
121126
})
122127

123128
it("removes trailing slash from URL with path", () => {
124129
const url = "https://example.com/path/"
125130
const expected = "https://example.com/path"
126-
expect(stripTrailingSlash(url)).toBe(expected)
131+
expect(normalizeUrl(url)).toBe(expected)
127132
})
128133

129134
it("does not remove slash from root URL", () => {
130135
const url = "/"
131136
const expected = "/"
132-
expect(stripTrailingSlash(url)).toBe(expected)
137+
expect(normalizeUrl(url)).toBe(expected)
133138
})
134139

135140
it("returns URL unchanged when no trailing slash", () => {
136141
const url = "https://example.com/path"
137142
const expected = "https://example.com/path"
138-
expect(stripTrailingSlash(url)).toBe(expected)
143+
expect(normalizeUrl(url)).toBe(expected)
139144
})
140145

141146
it("returns empty string for undefined input", () => {
142-
expect(stripTrailingSlash()).toBe("")
147+
expect(normalizeUrl()).toBe("")
143148
})
144149

145150
it("handles URLs with query parameters and trailing slash", () => {
146151
const url = "https://example.com/path/?param=value"
147152
const expected = "https://example.com/path?param=value"
148-
expect(stripTrailingSlash(url)).toBe(expected)
153+
expect(normalizeUrl(url)).toBe(expected)
149154
})
150155
})
151156

src/http/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Supports both regular domains and localhost/IP addresses
88
*/
99
const URL_REGEX =
10-
/^https?:\/\/(?:www\.)?(?:[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b|localhost(?::\d+)?|127\.0\.0\.1(?::\d+)?)(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)$/
10+
/^https?:\/\/(?:www\.)?(?:[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,63}\b|localhost(?::\d+)?|127\.0\.0\.1(?::\d+)?)(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)$/
1111

1212
/**
1313
* Checks if a URL is valid using both regex and URL constructor validation

0 commit comments

Comments
 (0)