@catbee/utils 2.0.0-next.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/types/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
package/url/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -90,10 +90,8 @@ function joinPaths(...segments) {
|
|
|
90
90
|
__name(joinPaths, "joinPaths");
|
|
91
91
|
function normalizeUrl(url$1, base) {
|
|
92
92
|
try {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
const parsedUrl = base ? new url.URL(url$1, base) : new url.URL(url$1);
|
|
93
|
+
let normalized = url$1.startsWith("//") ? `https:${url$1}` : url$1;
|
|
94
|
+
const parsedUrl = base ? new url.URL(normalized, base) : new url.URL(normalized);
|
|
97
95
|
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+/g, "/");
|
|
98
96
|
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, "");
|
|
99
97
|
parsedUrl.hostname = parsedUrl.hostname.toLowerCase();
|
|
@@ -103,6 +101,16 @@ function normalizeUrl(url$1, base) {
|
|
|
103
101
|
}
|
|
104
102
|
}
|
|
105
103
|
__name(normalizeUrl, "normalizeUrl");
|
|
104
|
+
function updateQueryParam(url$1, key, value) {
|
|
105
|
+
try {
|
|
106
|
+
const urlObj = new url.URL(url$1);
|
|
107
|
+
urlObj.searchParams.set(key, String(value));
|
|
108
|
+
return urlObj.toString();
|
|
109
|
+
} catch {
|
|
110
|
+
return url$1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
__name(updateQueryParam, "updateQueryParam");
|
|
106
114
|
function createUrlBuilder(baseUrl) {
|
|
107
115
|
return {
|
|
108
116
|
/**
|
|
@@ -187,15 +195,65 @@ function parseTypedQueryParams(url$1, converters) {
|
|
|
187
195
|
}
|
|
188
196
|
}
|
|
189
197
|
__name(parseTypedQueryParams, "parseTypedQueryParams");
|
|
198
|
+
function getSubdomain(url$1) {
|
|
199
|
+
try {
|
|
200
|
+
const { hostname } = new url.URL(url$1);
|
|
201
|
+
const parts = hostname.split(".");
|
|
202
|
+
if (parts.length <= 2) return "";
|
|
203
|
+
return parts.slice(0, -2).join(".");
|
|
204
|
+
} catch {
|
|
205
|
+
return "";
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
__name(getSubdomain, "getSubdomain");
|
|
209
|
+
function isRelativeUrl(url$1) {
|
|
210
|
+
if (!url$1 || typeof url$1 !== "string") return false;
|
|
211
|
+
try {
|
|
212
|
+
new url.URL(url$1);
|
|
213
|
+
return false;
|
|
214
|
+
} catch {
|
|
215
|
+
return url$1.startsWith("/") || url$1.startsWith("./") || url$1.startsWith("../");
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
__name(isRelativeUrl, "isRelativeUrl");
|
|
219
|
+
function toAbsoluteUrl(relativeUrl, baseUrl) {
|
|
220
|
+
try {
|
|
221
|
+
return new url.URL(relativeUrl, baseUrl).toString();
|
|
222
|
+
} catch {
|
|
223
|
+
return relativeUrl;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
__name(toAbsoluteUrl, "toAbsoluteUrl");
|
|
227
|
+
function sanitizeUrl(url$1, allowedProtocols = [
|
|
228
|
+
"http",
|
|
229
|
+
"https"
|
|
230
|
+
]) {
|
|
231
|
+
try {
|
|
232
|
+
const parsedUrl = new url.URL(url$1);
|
|
233
|
+
const protocol = parsedUrl.protocol.replace(":", "");
|
|
234
|
+
if (!allowedProtocols.includes(protocol)) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
return parsedUrl.toString();
|
|
238
|
+
} catch {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
__name(sanitizeUrl, "sanitizeUrl");
|
|
190
243
|
|
|
191
244
|
exports.appendQueryParams = appendQueryParams;
|
|
192
245
|
exports.createUrlBuilder = createUrlBuilder;
|
|
193
246
|
exports.extractQueryParams = extractQueryParams;
|
|
194
247
|
exports.getDomain = getDomain;
|
|
195
248
|
exports.getExtension = getExtension;
|
|
249
|
+
exports.getSubdomain = getSubdomain;
|
|
250
|
+
exports.isRelativeUrl = isRelativeUrl;
|
|
196
251
|
exports.isValidUrl = isValidUrl;
|
|
197
252
|
exports.joinPaths = joinPaths;
|
|
198
253
|
exports.normalizeUrl = normalizeUrl;
|
|
199
254
|
exports.parseQueryString = parseQueryString;
|
|
200
255
|
exports.parseTypedQueryParams = parseTypedQueryParams;
|
|
201
256
|
exports.removeQueryParams = removeQueryParams;
|
|
257
|
+
exports.sanitizeUrl = sanitizeUrl;
|
|
258
|
+
exports.toAbsoluteUrl = toAbsoluteUrl;
|
|
259
|
+
exports.updateQueryParam = updateQueryParam;
|
package/url/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,202 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Appends query parameters to a given URL.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} url - The base URL to which query parameters will be appended.
|
|
29
|
+
* @param {Record<string, string | number>} params - Key-value pairs to add as query parameters.
|
|
30
|
+
* @returns {string} The new URL string with query parameters appended.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* appendQueryParams('https://example.com', { page: 1, limit: 10 });
|
|
34
|
+
* // -> 'https://example.com/?page=1&limit=10'
|
|
35
|
+
*/
|
|
36
|
+
declare function appendQueryParams(url: string, params: Record<string, string | number>): string;
|
|
37
|
+
/**
|
|
38
|
+
* Parses a query string into a key-value object.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} query - The query string (with or without leading '?').
|
|
41
|
+
* @returns {Record<string, string>} Object representing parsed query parameters.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* parseQueryString('?page=1&limit=10');
|
|
45
|
+
* // -> { page: '1', limit: '10' }
|
|
46
|
+
*/
|
|
47
|
+
declare function parseQueryString(query: string): Record<string, string>;
|
|
48
|
+
/**
|
|
49
|
+
* Validates if a string is a valid URL.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} url - The URL string to validate.
|
|
52
|
+
* @param {boolean} [requireHttps=false] - If true, requires the URL to use HTTPS.
|
|
53
|
+
* @returns {boolean} True if the URL is valid, false otherwise.
|
|
54
|
+
*/
|
|
55
|
+
declare function isValidUrl(url: string, requireHttps?: boolean): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Extracts the domain name from a URL.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} url - The URL to extract the domain from.
|
|
60
|
+
* @param {boolean} [removeSubdomains=false] - If true, removes subdomains (returns root domain only).
|
|
61
|
+
* @returns {string} The domain name.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* getDomain('https://api.example.com/path');
|
|
65
|
+
* // -> 'api.example.com'
|
|
66
|
+
*
|
|
67
|
+
* getDomain('https://api.example.com/path', true);
|
|
68
|
+
* // -> 'example.com'
|
|
69
|
+
*/
|
|
70
|
+
declare function getDomain(url: string, removeSubdomains?: boolean): string;
|
|
71
|
+
/**
|
|
72
|
+
* Joins URL paths properly handling slashes.
|
|
73
|
+
*
|
|
74
|
+
* @param {...string[]} segments - URL path segments to join.
|
|
75
|
+
* @returns {string} Joined URL path.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* joinPaths('https://example.com/', '/api/', '/users');
|
|
79
|
+
* // -> 'https://example.com/api/users'
|
|
80
|
+
*/
|
|
81
|
+
declare function joinPaths(...segments: string[]): string;
|
|
82
|
+
/**
|
|
83
|
+
* Normalizes a URL by resolving relative paths, handling protocol-relative URLs, etc.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} url - The URL to normalize.
|
|
86
|
+
* @param {string} [base] - Optional base URL for resolving relative URLs.
|
|
87
|
+
* @returns {string} Normalized URL.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* normalizeUrl('HTTP://Example.COM/foo/../bar');
|
|
91
|
+
* // -> 'http://example.com/bar'
|
|
92
|
+
*/
|
|
93
|
+
declare function normalizeUrl(url: string, base?: string): string;
|
|
94
|
+
/**
|
|
95
|
+
* Updates or sets a single query parameter in a URL.
|
|
96
|
+
*
|
|
97
|
+
* @param {string} url - The source URL.
|
|
98
|
+
* @param {string} key - Parameter key.
|
|
99
|
+
* @param {string | number} value - Parameter value.
|
|
100
|
+
* @returns {string} URL with updated parameter.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* updateQueryParam('https://example.com?page=1', 'page', 2); // 'https://example.com?page=2'
|
|
104
|
+
*/
|
|
105
|
+
declare function updateQueryParam(url: string, key: string, value: string | number): string;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a URL builder for constructing URLs with a base URL.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} baseUrl - The base URL to build upon.
|
|
110
|
+
* @returns {Object} URL builder methods.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* const api = createUrlBuilder('https://api.example.com');
|
|
114
|
+
* api.path('/users', { active: true });
|
|
115
|
+
* // -> 'https://api.example.com/users?active=true'
|
|
116
|
+
*/
|
|
117
|
+
declare function createUrlBuilder(baseUrl: string): {
|
|
118
|
+
/**
|
|
119
|
+
* Creates a full URL with the given path and query parameters.
|
|
120
|
+
*
|
|
121
|
+
* @param {string} path - The path to append to the base URL.
|
|
122
|
+
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
123
|
+
* @returns {string} The complete URL.
|
|
124
|
+
*/
|
|
125
|
+
path(path: string, params?: Record<string, any>): string;
|
|
126
|
+
/**
|
|
127
|
+
* Creates a full URL with query parameters but no additional path.
|
|
128
|
+
*
|
|
129
|
+
* @param {Record<string, any>} params - Query parameters to add.
|
|
130
|
+
* @returns {string} The complete URL with query parameters.
|
|
131
|
+
*/
|
|
132
|
+
query(params: Record<string, any>): string;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Extracts specific query parameters from a URL.
|
|
136
|
+
*
|
|
137
|
+
* @param {string} url - The URL to extract parameters from.
|
|
138
|
+
* @param {string[]} paramNames - Names of parameters to extract.
|
|
139
|
+
* @returns {Record<string, string>} Object containing the extracted parameters.
|
|
140
|
+
*/
|
|
141
|
+
declare function extractQueryParams(url: string, paramNames: string[]): Record<string, string>;
|
|
142
|
+
/**
|
|
143
|
+
* Removes specified query parameters from a URL.
|
|
144
|
+
*
|
|
145
|
+
* @param {string} url - The URL to modify.
|
|
146
|
+
* @param {string[]} paramsToRemove - Names of parameters to remove.
|
|
147
|
+
* @returns {string} URL with parameters removed.
|
|
148
|
+
*/
|
|
149
|
+
declare function removeQueryParams(url: string, paramsToRemove: string[]): string;
|
|
150
|
+
/**
|
|
151
|
+
* Gets the file extension from a URL path.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} url - The URL to examine.
|
|
154
|
+
* @returns {string} The file extension (without dot) or empty string if none found.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* getExtension('https://example.com/document.pdf?v=1');
|
|
158
|
+
* // -> 'pdf'
|
|
159
|
+
*/
|
|
160
|
+
declare function getExtension(url: string): string;
|
|
161
|
+
/**
|
|
162
|
+
* Parses URL query parameters into a strongly-typed object.
|
|
163
|
+
*
|
|
164
|
+
* @template T Expected type of the query parameters
|
|
165
|
+
* @param {string} url - The URL to parse.
|
|
166
|
+
* @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.
|
|
167
|
+
* @returns {Partial<T>} Typed query parameters.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {
|
|
171
|
+
* page: Number,
|
|
172
|
+
* q: String
|
|
173
|
+
* });
|
|
174
|
+
* // -> { page: 2, q: 'test' }
|
|
175
|
+
*/
|
|
176
|
+
declare function parseTypedQueryParams<T extends Record<string, any>>(url: string, converters?: Record<keyof T, (val: string) => any>): Partial<T>;
|
|
177
|
+
/**
|
|
178
|
+
* Extracts the subdomain from a URL.
|
|
179
|
+
*
|
|
180
|
+
* @param {string} url - The URL.
|
|
181
|
+
* @returns {string} Subdomain or empty string.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* getSubdomain('https://api.example.com'); // 'api'
|
|
185
|
+
* getSubdomain('https://www.blog.example.com'); // 'www.blog'
|
|
186
|
+
*/
|
|
187
|
+
declare function getSubdomain(url: string): string;
|
|
188
|
+
/**
|
|
189
|
+
* Checks if a URL is relative (not absolute).
|
|
190
|
+
*
|
|
191
|
+
* @param {string} url - The URL to check.
|
|
192
|
+
* @returns {boolean} True if relative.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* isRelativeUrl('/path/to/page'); // true
|
|
196
|
+
* isRelativeUrl('https://example.com/page'); // false
|
|
197
|
+
*/
|
|
198
|
+
declare function isRelativeUrl(url: string): boolean;
|
|
199
|
+
/**
|
|
200
|
+
* Converts a relative URL to an absolute URL using a base URL.
|
|
201
|
+
*
|
|
202
|
+
* @param {string} relativeUrl - The relative URL.
|
|
203
|
+
* @param {string} baseUrl - The base URL.
|
|
204
|
+
* @returns {string} Absolute URL.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* toAbsoluteUrl('/api/users', 'https://example.com'); // 'https://example.com/api/users'
|
|
208
|
+
*/
|
|
209
|
+
declare function toAbsoluteUrl(relativeUrl: string, baseUrl: string): string;
|
|
210
|
+
/**
|
|
211
|
+
* Sanitizes a URL by removing dangerous protocols and normalizing.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} url - The URL to sanitize.
|
|
214
|
+
* @param {string[]} [allowedProtocols=['http', 'https']] - Allowed protocols.
|
|
215
|
+
* @returns {string | null} Sanitized URL or null if unsafe.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* sanitizeUrl('javascript:alert(1)'); // null
|
|
219
|
+
* sanitizeUrl('https://example.com'); // 'https://example.com/'
|
|
220
|
+
*/
|
|
221
|
+
declare function sanitizeUrl(url: string, allowedProtocols?: string[]): string | null;
|
|
222
|
+
|
|
223
|
+
export { appendQueryParams, createUrlBuilder, extractQueryParams, getDomain, getExtension, getSubdomain, isRelativeUrl, isValidUrl, joinPaths, normalizeUrl, parseQueryString, parseTypedQueryParams, removeQueryParams, sanitizeUrl, toAbsoluteUrl, updateQueryParam };
|
package/url/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -88,10 +88,8 @@ function joinPaths(...segments) {
|
|
|
88
88
|
__name(joinPaths, "joinPaths");
|
|
89
89
|
function normalizeUrl(url, base) {
|
|
90
90
|
try {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
const parsedUrl = base ? new URL(url, base) : new URL(url);
|
|
91
|
+
let normalized = url.startsWith("//") ? `https:${url}` : url;
|
|
92
|
+
const parsedUrl = base ? new URL(normalized, base) : new URL(normalized);
|
|
95
93
|
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+/g, "/");
|
|
96
94
|
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, "");
|
|
97
95
|
parsedUrl.hostname = parsedUrl.hostname.toLowerCase();
|
|
@@ -101,6 +99,16 @@ function normalizeUrl(url, base) {
|
|
|
101
99
|
}
|
|
102
100
|
}
|
|
103
101
|
__name(normalizeUrl, "normalizeUrl");
|
|
102
|
+
function updateQueryParam(url, key, value) {
|
|
103
|
+
try {
|
|
104
|
+
const urlObj = new URL(url);
|
|
105
|
+
urlObj.searchParams.set(key, String(value));
|
|
106
|
+
return urlObj.toString();
|
|
107
|
+
} catch {
|
|
108
|
+
return url;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
__name(updateQueryParam, "updateQueryParam");
|
|
104
112
|
function createUrlBuilder(baseUrl) {
|
|
105
113
|
return {
|
|
106
114
|
/**
|
|
@@ -185,5 +193,50 @@ function parseTypedQueryParams(url, converters) {
|
|
|
185
193
|
}
|
|
186
194
|
}
|
|
187
195
|
__name(parseTypedQueryParams, "parseTypedQueryParams");
|
|
196
|
+
function getSubdomain(url) {
|
|
197
|
+
try {
|
|
198
|
+
const { hostname } = new URL(url);
|
|
199
|
+
const parts = hostname.split(".");
|
|
200
|
+
if (parts.length <= 2) return "";
|
|
201
|
+
return parts.slice(0, -2).join(".");
|
|
202
|
+
} catch {
|
|
203
|
+
return "";
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
__name(getSubdomain, "getSubdomain");
|
|
207
|
+
function isRelativeUrl(url) {
|
|
208
|
+
if (!url || typeof url !== "string") return false;
|
|
209
|
+
try {
|
|
210
|
+
new URL(url);
|
|
211
|
+
return false;
|
|
212
|
+
} catch {
|
|
213
|
+
return url.startsWith("/") || url.startsWith("./") || url.startsWith("../");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
__name(isRelativeUrl, "isRelativeUrl");
|
|
217
|
+
function toAbsoluteUrl(relativeUrl, baseUrl) {
|
|
218
|
+
try {
|
|
219
|
+
return new URL(relativeUrl, baseUrl).toString();
|
|
220
|
+
} catch {
|
|
221
|
+
return relativeUrl;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
__name(toAbsoluteUrl, "toAbsoluteUrl");
|
|
225
|
+
function sanitizeUrl(url, allowedProtocols = [
|
|
226
|
+
"http",
|
|
227
|
+
"https"
|
|
228
|
+
]) {
|
|
229
|
+
try {
|
|
230
|
+
const parsedUrl = new URL(url);
|
|
231
|
+
const protocol = parsedUrl.protocol.replace(":", "");
|
|
232
|
+
if (!allowedProtocols.includes(protocol)) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
return parsedUrl.toString();
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
__name(sanitizeUrl, "sanitizeUrl");
|
|
188
241
|
|
|
189
|
-
export { appendQueryParams, createUrlBuilder, extractQueryParams, getDomain, getExtension, isValidUrl, joinPaths, normalizeUrl, parseQueryString, parseTypedQueryParams, removeQueryParams };
|
|
242
|
+
export { appendQueryParams, createUrlBuilder, extractQueryParams, getDomain, getExtension, getSubdomain, isRelativeUrl, isValidUrl, joinPaths, normalizeUrl, parseQueryString, parseTypedQueryParams, removeQueryParams, sanitizeUrl, toAbsoluteUrl, updateQueryParam };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -24,25 +24,41 @@
|
|
|
24
24
|
|
|
25
25
|
'use strict';
|
|
26
26
|
|
|
27
|
+
var net = require('net');
|
|
28
|
+
|
|
29
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
30
|
+
|
|
31
|
+
var net__default = /*#__PURE__*/_interopDefault(net);
|
|
32
|
+
|
|
27
33
|
var __defProp = Object.defineProperty;
|
|
28
34
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
var PORT_REGEX = /^[0-9]{1,5}$/;
|
|
36
|
+
var UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
37
|
+
var PHONE_ALLOWED_CHARS_REGEX = /^[+]?[0-9\s().-]+$/;
|
|
38
|
+
var ALPHANUMERIC_REGEX = /^[A-Za-z0-9]+$/;
|
|
39
|
+
var HEX_COLOR_REGEX = /^#([a-f0-9]{6}|[a-f0-9]{3})$/i;
|
|
40
|
+
var ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
41
|
+
var BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
42
|
+
var DOT_ATOM_REGEX = /^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
|
|
43
|
+
var QUOTED_LOCAL_REGEX = /^"([\s\x21\x23-\x5B\x5D-\x7E]|\\[\x20-\x7E])*"$/;
|
|
44
|
+
var TLD_REGEX = /^[A-Za-z]{2,63}$/;
|
|
31
45
|
function isPort(value) {
|
|
32
|
-
|
|
46
|
+
if (typeof value === "number") return Number.isInteger(value) && value > 0 && value <= 65535;
|
|
47
|
+
if (!value || typeof value !== "string") return false;
|
|
48
|
+
const str = value.trim();
|
|
49
|
+
if (!PORT_REGEX.test(str)) return false;
|
|
50
|
+
const port = Number(str);
|
|
33
51
|
return Number.isInteger(port) && port > 0 && port <= 65535;
|
|
34
52
|
}
|
|
35
53
|
__name(isPort, "isPort");
|
|
36
54
|
function isEmail(str) {
|
|
37
|
-
if (typeof str !== "string" || str.length > 254) return false;
|
|
55
|
+
if (!str || typeof str !== "string" || str.length > 254) return false;
|
|
38
56
|
const atIndex = str.indexOf("@");
|
|
39
57
|
if (atIndex === -1 || atIndex !== str.lastIndexOf("@")) return false;
|
|
40
58
|
const local = str.slice(0, atIndex);
|
|
41
59
|
const domain = str.slice(atIndex + 1);
|
|
42
60
|
if (!local || !domain) return false;
|
|
43
|
-
|
|
44
|
-
const quotedLocal = /^"([\s\x21\x23-\x5B\x5D-\x7E]|\\[\x20-\x7E])*"$/;
|
|
45
|
-
if (!dotAtom.test(local) && !quotedLocal.test(local)) return false;
|
|
61
|
+
if (!DOT_ATOM_REGEX.test(local) && !QUOTED_LOCAL_REGEX.test(local)) return false;
|
|
46
62
|
if (domain.includes("..")) return false;
|
|
47
63
|
const labels = domain.split(".");
|
|
48
64
|
if (labels.some((l) => !l.length)) return false;
|
|
@@ -52,61 +68,75 @@ function isEmail(str) {
|
|
|
52
68
|
if (!labelRegex.test(label)) return false;
|
|
53
69
|
if (label.startsWith("-") || label.endsWith("-")) return false;
|
|
54
70
|
}
|
|
55
|
-
const tld = labels
|
|
56
|
-
if (
|
|
71
|
+
const tld = labels.at(-1) || "";
|
|
72
|
+
if (!TLD_REGEX.test(tld)) return false;
|
|
57
73
|
return true;
|
|
58
74
|
}
|
|
59
75
|
__name(isEmail, "isEmail");
|
|
60
76
|
function isUUID(str) {
|
|
61
|
-
return
|
|
77
|
+
return UUID_REGEX.test(str);
|
|
62
78
|
}
|
|
63
79
|
__name(isUUID, "isUUID");
|
|
64
|
-
function isURL(str
|
|
80
|
+
function isURL(str, allowedProtocols = [
|
|
81
|
+
"http",
|
|
82
|
+
"https",
|
|
83
|
+
"ws",
|
|
84
|
+
"wss"
|
|
85
|
+
]) {
|
|
86
|
+
if (!str || typeof str !== "string") return false;
|
|
65
87
|
try {
|
|
66
|
-
new URL(str);
|
|
67
|
-
return
|
|
88
|
+
const url = new URL(str);
|
|
89
|
+
return allowedProtocols.includes(url.protocol.replace(":", ""));
|
|
68
90
|
} catch {
|
|
69
91
|
return false;
|
|
70
92
|
}
|
|
71
93
|
}
|
|
72
94
|
__name(isURL, "isURL");
|
|
73
95
|
function isPhone(str) {
|
|
74
|
-
if (typeof str !== "string") return false;
|
|
75
|
-
const
|
|
76
|
-
if (
|
|
77
|
-
return
|
|
96
|
+
if (!str || typeof str !== "string") return false;
|
|
97
|
+
const digits = str.replace(/\D/g, "");
|
|
98
|
+
if (digits.length < 6 || digits.length > 15) return false;
|
|
99
|
+
return PHONE_ALLOWED_CHARS_REGEX.test(str);
|
|
78
100
|
}
|
|
79
101
|
__name(isPhone, "isPhone");
|
|
80
102
|
function isAlphanumeric(str) {
|
|
81
|
-
|
|
103
|
+
if (!str || typeof str !== "string") return false;
|
|
104
|
+
return ALPHANUMERIC_REGEX.test(str);
|
|
82
105
|
}
|
|
83
106
|
__name(isAlphanumeric, "isAlphanumeric");
|
|
84
107
|
function isNumeric(value) {
|
|
85
|
-
if (typeof value === "
|
|
86
|
-
|
|
87
|
-
|
|
108
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
109
|
+
if (!value || typeof value !== "string") return false;
|
|
110
|
+
const str = value.trim();
|
|
111
|
+
if (str === "" || str.length > 16) return false;
|
|
112
|
+
const num = Number(str);
|
|
113
|
+
return Number.isFinite(num);
|
|
88
114
|
}
|
|
89
115
|
__name(isNumeric, "isNumeric");
|
|
90
116
|
function isHexColor(str) {
|
|
91
|
-
|
|
117
|
+
if (!str || typeof str !== "string") return false;
|
|
118
|
+
return HEX_COLOR_REGEX.test(str);
|
|
92
119
|
}
|
|
93
120
|
__name(isHexColor, "isHexColor");
|
|
94
121
|
function isISODate(str) {
|
|
95
|
-
|
|
96
|
-
if (!
|
|
122
|
+
if (!str || typeof str !== "string") return false;
|
|
123
|
+
if (!ISO_DATE_REGEX.test(str)) return false;
|
|
97
124
|
const date = new Date(str);
|
|
98
|
-
return !isNaN(date.getTime())
|
|
125
|
+
return !Number.isNaN(date.getTime());
|
|
99
126
|
}
|
|
100
127
|
__name(isISODate, "isISODate");
|
|
101
128
|
function isLengthBetween(str, min, max) {
|
|
129
|
+
if (typeof str !== "string") return false;
|
|
102
130
|
return str.length >= min && str.length <= max;
|
|
103
131
|
}
|
|
104
132
|
__name(isLengthBetween, "isLengthBetween");
|
|
105
133
|
function isNumberBetween(value, min, max) {
|
|
134
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return false;
|
|
106
135
|
return value >= min && value <= max;
|
|
107
136
|
}
|
|
108
137
|
__name(isNumberBetween, "isNumberBetween");
|
|
109
138
|
function isAlpha(str) {
|
|
139
|
+
if (!str || typeof str !== "string") return false;
|
|
110
140
|
return /^[a-zA-Z]+$/.test(str);
|
|
111
141
|
}
|
|
112
142
|
__name(isAlpha, "isAlpha");
|
|
@@ -120,36 +150,43 @@ function isStrongPassword(str) {
|
|
|
120
150
|
}
|
|
121
151
|
__name(isStrongPassword, "isStrongPassword");
|
|
122
152
|
function isIPv4(str) {
|
|
153
|
+
if (!str || typeof str !== "string") return false;
|
|
123
154
|
const parts = str.split(".");
|
|
124
155
|
if (parts.length !== 4) return false;
|
|
125
|
-
return parts.every((
|
|
126
|
-
|
|
127
|
-
|
|
156
|
+
return parts.every((p) => {
|
|
157
|
+
if (p.length > 3 || p.startsWith("0") && p.length > 1) return false;
|
|
158
|
+
const n = Number(p);
|
|
159
|
+
return Number.isInteger(n) && n >= 0 && n <= 255;
|
|
128
160
|
});
|
|
129
161
|
}
|
|
130
162
|
__name(isIPv4, "isIPv4");
|
|
131
163
|
function isIPv6(str) {
|
|
132
|
-
|
|
164
|
+
if (!str || typeof str !== "string" || str.length > 45) return false;
|
|
165
|
+
try {
|
|
166
|
+
return net__default.default.isIP(str) === 6;
|
|
167
|
+
} catch {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
133
170
|
}
|
|
134
171
|
__name(isIPv6, "isIPv6");
|
|
135
172
|
function isCreditCard(str) {
|
|
136
|
-
|
|
137
|
-
|
|
173
|
+
if (!str || typeof str !== "string") return false;
|
|
174
|
+
const s = str.replace(/\D/g, "");
|
|
175
|
+
if (s.length < 13 || s.length > 19) return false;
|
|
138
176
|
let sum = 0;
|
|
139
|
-
let
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (digit > 9) digit -= 9;
|
|
177
|
+
for (let i = 0, dbl = false; i < s.length; i++, dbl = !dbl) {
|
|
178
|
+
let d = s.charCodeAt(s.length - 1 - i) - 48;
|
|
179
|
+
if (dbl) {
|
|
180
|
+
d *= 2;
|
|
181
|
+
if (d > 9) d -= 9;
|
|
145
182
|
}
|
|
146
|
-
sum +=
|
|
147
|
-
shouldDouble = !shouldDouble;
|
|
183
|
+
sum += d;
|
|
148
184
|
}
|
|
149
185
|
return sum % 10 === 0;
|
|
150
186
|
}
|
|
151
187
|
__name(isCreditCard, "isCreditCard");
|
|
152
188
|
function isValidJSON(str) {
|
|
189
|
+
if (!str || typeof str !== "string") return false;
|
|
153
190
|
try {
|
|
154
191
|
JSON.parse(str);
|
|
155
192
|
return true;
|
|
@@ -164,26 +201,36 @@ function isArray(value, itemGuard) {
|
|
|
164
201
|
}
|
|
165
202
|
__name(isArray, "isArray");
|
|
166
203
|
function isBase64(str) {
|
|
167
|
-
|
|
204
|
+
if (!str || typeof str !== "string") return false;
|
|
205
|
+
return BASE64_REGEX.test(str);
|
|
168
206
|
}
|
|
169
207
|
__name(isBase64, "isBase64");
|
|
170
208
|
function hasRequiredProps(obj, requiredProps) {
|
|
171
|
-
|
|
209
|
+
if (!obj || typeof obj !== "object") return false;
|
|
210
|
+
return requiredProps.every((prop) => Object.hasOwn(obj, prop) && obj[prop] != null);
|
|
172
211
|
}
|
|
173
212
|
__name(hasRequiredProps, "hasRequiredProps");
|
|
174
213
|
function isDateInRange(date, minDate, maxDate) {
|
|
175
|
-
if (!(date instanceof Date) ||
|
|
214
|
+
if (!(date instanceof Date) || !Number.isFinite(date.getTime())) return false;
|
|
176
215
|
if (minDate && date < minDate) return false;
|
|
177
216
|
if (maxDate && date > maxDate) return false;
|
|
178
217
|
return true;
|
|
179
218
|
}
|
|
180
219
|
__name(isDateInRange, "isDateInRange");
|
|
181
220
|
function matchesPattern(str, pattern) {
|
|
221
|
+
if (typeof str !== "string") return false;
|
|
182
222
|
return pattern.test(str);
|
|
183
223
|
}
|
|
184
224
|
__name(matchesPattern, "matchesPattern");
|
|
185
225
|
function validateAll(value, validators) {
|
|
186
|
-
|
|
226
|
+
if (!Array.isArray(validators)) return false;
|
|
227
|
+
return validators.every((fn) => {
|
|
228
|
+
try {
|
|
229
|
+
return fn(value);
|
|
230
|
+
} catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
187
234
|
}
|
|
188
235
|
__name(validateAll, "validateAll");
|
|
189
236
|
|