@catbee/utils 2.0.0-next.0 → 2.0.0-next.1
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/README.md +52 -16
- package/array/index.cjs +180 -71
- package/array/index.d.ts +293 -1
- package/array/index.mjs +171 -72
- package/async/index.cjs +92 -36
- package/async/index.d.ts +275 -1
- package/async/index.mjs +92 -36
- package/cache/index.cjs +1 -1
- package/cache/index.d.ts +155 -1
- package/cache/index.mjs +2 -2
- package/config/index.cjs +78 -64
- package/config/index.d.ts +64 -2
- package/config/index.mjs +76 -64
- package/context-store/index.d.ts +192 -1
- package/crypto/index.d.ts +163 -1
- package/date/index.cjs +46 -1
- package/date/index.d.ts +190 -1
- package/date/index.mjs +45 -2
- package/decorators/index.cjs +1156 -18
- package/decorators/index.d.ts +684 -1
- package/decorators/index.mjs +1156 -18
- package/dir/index.cjs +4 -3
- package/dir/index.d.ts +195 -1
- package/dir/index.mjs +4 -3
- package/env/index.cjs +10 -26
- package/env/index.d.ts +379 -1
- package/env/index.mjs +10 -26
- package/exception/index.d.ts +232 -1
- package/fs/index.cjs +70 -36
- package/fs/index.d.ts +205 -1
- package/fs/index.mjs +64 -34
- package/http-status-codes/index.d.ts +267 -1
- package/id/index.d.ts +37 -1
- package/index.cjs +3 -3
- package/index.d.ts +1 -1
- package/index.mjs +1 -1
- package/logger/index.cjs +11 -11
- package/logger/index.d.ts +189 -1
- package/logger/index.mjs +12 -12
- package/middleware/index.d.ts +103 -1
- package/obj/index.cjs +150 -162
- package/obj/index.d.ts +136 -1
- package/obj/index.mjs +150 -162
- package/package.json +11 -11
- package/performance/index.cjs +2 -2
- package/performance/index.d.ts +138 -1
- package/performance/index.mjs +2 -2
- package/request/index.cjs +1 -1
- package/request/index.d.ts +241 -2
- package/request/index.mjs +1 -1
- package/response/index.d.ts +318 -2
- package/server/index.cjs +27 -23
- package/server/index.d.ts +785 -4
- package/server/index.mjs +28 -23
- package/stream/index.d.ts +90 -1
- package/string/index.d.ts +102 -1
- package/type/index.cjs +1 -1
- package/type/index.d.ts +107 -1
- package/type/index.mjs +1 -1
- package/types/index.d.ts +774 -4
- package/url/index.cjs +2 -4
- package/url/index.d.ts +142 -1
- package/url/index.mjs +2 -4
- package/{validate → validation}/index.cjs +89 -42
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
- package/{validate → validation}/index.mjs +85 -42
- 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/decorators.utils.d.ts +0 -705
- package/dir/dir.utils.d.ts +0 -216
- 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/obj.utils.d.ts +0 -156
- 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/url/index.cjs
CHANGED
|
@@ -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();
|
package/url/index.d.ts
CHANGED
|
@@ -22,4 +22,145 @@
|
|
|
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
|
+
* Creates a URL builder for constructing URLs with a base URL.
|
|
96
|
+
*
|
|
97
|
+
* @param {string} baseUrl - The base URL to build upon.
|
|
98
|
+
* @returns {Object} URL builder methods.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const api = createUrlBuilder('https://api.example.com');
|
|
102
|
+
* api.path('/users', { active: true });
|
|
103
|
+
* // -> 'https://api.example.com/users?active=true'
|
|
104
|
+
*/
|
|
105
|
+
declare function createUrlBuilder(baseUrl: string): {
|
|
106
|
+
/**
|
|
107
|
+
* Creates a full URL with the given path and query parameters.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} path - The path to append to the base URL.
|
|
110
|
+
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
111
|
+
* @returns {string} The complete URL.
|
|
112
|
+
*/
|
|
113
|
+
path(path: string, params?: Record<string, any>): string;
|
|
114
|
+
/**
|
|
115
|
+
* Creates a full URL with query parameters but no additional path.
|
|
116
|
+
*
|
|
117
|
+
* @param {Record<string, any>} params - Query parameters to add.
|
|
118
|
+
* @returns {string} The complete URL with query parameters.
|
|
119
|
+
*/
|
|
120
|
+
query(params: Record<string, any>): string;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Extracts specific query parameters from a URL.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} url - The URL to extract parameters from.
|
|
126
|
+
* @param {string[]} paramNames - Names of parameters to extract.
|
|
127
|
+
* @returns {Record<string, string>} Object containing the extracted parameters.
|
|
128
|
+
*/
|
|
129
|
+
declare function extractQueryParams(url: string, paramNames: string[]): Record<string, string>;
|
|
130
|
+
/**
|
|
131
|
+
* Removes specified query parameters from a URL.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} url - The URL to modify.
|
|
134
|
+
* @param {string[]} paramsToRemove - Names of parameters to remove.
|
|
135
|
+
* @returns {string} URL with parameters removed.
|
|
136
|
+
*/
|
|
137
|
+
declare function removeQueryParams(url: string, paramsToRemove: string[]): string;
|
|
138
|
+
/**
|
|
139
|
+
* Gets the file extension from a URL path.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} url - The URL to examine.
|
|
142
|
+
* @returns {string} The file extension (without dot) or empty string if none found.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* getExtension('https://example.com/document.pdf?v=1');
|
|
146
|
+
* // -> 'pdf'
|
|
147
|
+
*/
|
|
148
|
+
declare function getExtension(url: string): string;
|
|
149
|
+
/**
|
|
150
|
+
* Parses URL query parameters into a strongly-typed object.
|
|
151
|
+
*
|
|
152
|
+
* @template T Expected type of the query parameters
|
|
153
|
+
* @param {string} url - The URL to parse.
|
|
154
|
+
* @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.
|
|
155
|
+
* @returns {Partial<T>} Typed query parameters.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {
|
|
159
|
+
* page: Number,
|
|
160
|
+
* q: String
|
|
161
|
+
* });
|
|
162
|
+
* // -> { page: 2, q: 'test' }
|
|
163
|
+
*/
|
|
164
|
+
declare function parseTypedQueryParams<T extends Record<string, any>>(url: string, converters?: Record<keyof T, (val: string) => any>): Partial<T>;
|
|
165
|
+
|
|
166
|
+
export { appendQueryParams, createUrlBuilder, extractQueryParams, getDomain, getExtension, isValidUrl, joinPaths, normalizeUrl, parseQueryString, parseTypedQueryParams, removeQueryParams };
|
package/url/index.mjs
CHANGED
|
@@ -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();
|
|
@@ -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;
|
|
@@ -53,60 +69,74 @@ function isEmail(str) {
|
|
|
53
69
|
if (label.startsWith("-") || label.endsWith("-")) return false;
|
|
54
70
|
}
|
|
55
71
|
const tld = labels[labels.length - 1];
|
|
56
|
-
if (
|
|
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.prototype.hasOwnProperty.call(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
|
|
|
@@ -31,63 +31,64 @@
|
|
|
31
31
|
* @param str - The input string or number.
|
|
32
32
|
* @returns True if valid port number, else false.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
declare function isPort(value: string | number): boolean;
|
|
35
35
|
/**
|
|
36
36
|
* Checks if a string is a valid email address.
|
|
37
37
|
*
|
|
38
38
|
* @param {string} str - The input string.
|
|
39
39
|
* @returns {boolean} True if valid email, else false.
|
|
40
40
|
*/
|
|
41
|
-
|
|
41
|
+
declare function isEmail(str: string): boolean;
|
|
42
42
|
/**
|
|
43
43
|
* Checks if a string is a valid UUID (versions 1-5).
|
|
44
44
|
*
|
|
45
45
|
* @param {string} str - The input string.
|
|
46
46
|
* @returns {boolean} True if valid UUID, else false.
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
declare function isUUID(str: string): boolean;
|
|
49
49
|
/**
|
|
50
50
|
* Checks if a string is a valid URL.
|
|
51
51
|
*
|
|
52
52
|
* @param {string} str - The input string.
|
|
53
|
+
* @param {string[]} [allowedProtocols=['http', 'https', 'ws', 'wss']] - Allowed URL protocols.
|
|
53
54
|
* @returns {boolean} True if valid URL, else false.
|
|
54
55
|
*/
|
|
55
|
-
|
|
56
|
+
declare function isURL(str: string, allowedProtocols?: string[]): boolean;
|
|
56
57
|
/**
|
|
57
58
|
* Checks if a string is a valid international phone number (E.164 or common patterns).
|
|
58
59
|
*
|
|
59
60
|
* @param {string} str - The input string.
|
|
60
61
|
* @returns {boolean} True if looks like a phone number.
|
|
61
62
|
*/
|
|
62
|
-
|
|
63
|
+
declare function isPhone(str: string): boolean;
|
|
63
64
|
/**
|
|
64
65
|
* Checks if a string is strictly alphanumeric (letters/numbers only).
|
|
65
66
|
*
|
|
66
67
|
* @param {string} str - The input string.
|
|
67
68
|
* @returns {boolean} True if alphanumeric.
|
|
68
69
|
*/
|
|
69
|
-
|
|
70
|
+
declare function isAlphanumeric(str: string): boolean;
|
|
70
71
|
/**
|
|
71
72
|
* Checks if a string or number can be safely parsed to a number.
|
|
72
73
|
*
|
|
73
74
|
* @param {string | number} value - The value to check.
|
|
74
75
|
* @returns {boolean} True if the value is numeric.
|
|
75
76
|
*/
|
|
76
|
-
|
|
77
|
+
declare function isNumeric(value: string | number): boolean;
|
|
77
78
|
/**
|
|
78
79
|
* Checks if a string is a valid hex color code (e.g. #FFF or #FFFFFF).
|
|
79
80
|
*
|
|
80
81
|
* @param {string} str - Input string.
|
|
81
82
|
* @returns {boolean}
|
|
82
83
|
*/
|
|
83
|
-
|
|
84
|
+
declare function isHexColor(str: string): boolean;
|
|
84
85
|
/**
|
|
85
86
|
* Checks if a string is a valid date string.
|
|
86
87
|
*
|
|
87
88
|
* @param {string} str - Input string.
|
|
88
89
|
* @returns {boolean}
|
|
89
90
|
*/
|
|
90
|
-
|
|
91
|
+
declare function isISODate(str: string): boolean;
|
|
91
92
|
/**
|
|
92
93
|
* Checks if a string matches a specified length range.
|
|
93
94
|
*
|
|
@@ -96,7 +97,7 @@ export declare function isISODate(str: string): boolean;
|
|
|
96
97
|
* @param {number} max - Maximum length (inclusive).
|
|
97
98
|
* @returns {boolean} True if string length is within range.
|
|
98
99
|
*/
|
|
99
|
-
|
|
100
|
+
declare function isLengthBetween(str: string, min: number, max: number): boolean;
|
|
100
101
|
/**
|
|
101
102
|
* Validates that a number is between specified min and max values (inclusive).
|
|
102
103
|
*
|
|
@@ -105,50 +106,56 @@ export declare function isLengthBetween(str: string, min: number, max: number):
|
|
|
105
106
|
* @param {number} max - Maximum value.
|
|
106
107
|
* @returns {boolean} True if number is within range.
|
|
107
108
|
*/
|
|
108
|
-
|
|
109
|
+
declare function isNumberBetween(value: number, min: number, max: number): boolean;
|
|
109
110
|
/**
|
|
110
111
|
* Checks if a string contains only alphabetic characters.
|
|
111
112
|
*
|
|
112
113
|
* @param {string} str - The input string.
|
|
113
114
|
* @returns {boolean} True if alphabetic only.
|
|
114
115
|
*/
|
|
115
|
-
|
|
116
|
+
declare function isAlpha(str: string): boolean;
|
|
116
117
|
/**
|
|
117
118
|
* Validates that a string contains at least one uppercase letter,
|
|
118
119
|
* one lowercase letter, one number, and one special character.
|
|
119
120
|
*
|
|
121
|
+
* - Minimum length of 8 characters.
|
|
122
|
+
* - At least one uppercase letter (A-Z).
|
|
123
|
+
* - At least one lowercase letter (a-z).
|
|
124
|
+
* - At least one digit (0-9).
|
|
125
|
+
* - At least one special character e.g. !@#$%^&*()_+-=[]{};':"\\|,.<>?/
|
|
126
|
+
*
|
|
120
127
|
* @param {string} str - The input string.
|
|
121
128
|
* @returns {boolean} True if string meets password complexity requirements.
|
|
122
129
|
*/
|
|
123
|
-
|
|
130
|
+
declare function isStrongPassword(str: string): boolean;
|
|
124
131
|
/**
|
|
125
132
|
* Checks if a string is a valid IPv4 address.
|
|
126
133
|
*
|
|
127
134
|
* @param {string} str - The input string.
|
|
128
135
|
* @returns {boolean} True if valid IPv4 address.
|
|
129
136
|
*/
|
|
130
|
-
|
|
137
|
+
declare function isIPv4(str: string): boolean;
|
|
131
138
|
/**
|
|
132
139
|
* Checks if a string is a valid IPv6 address.
|
|
133
140
|
*
|
|
134
141
|
* @param {string} str - The input string.
|
|
135
142
|
* @returns {boolean} True if valid IPv6 address.
|
|
136
143
|
*/
|
|
137
|
-
|
|
144
|
+
declare function isIPv6(str: string): boolean;
|
|
138
145
|
/**
|
|
139
146
|
* Validates a credit card number using the Luhn algorithm.
|
|
140
147
|
*
|
|
141
148
|
* @param {string} str - The credit card number string.
|
|
142
149
|
* @returns {boolean} True if valid credit card number.
|
|
143
150
|
*/
|
|
144
|
-
|
|
151
|
+
declare function isCreditCard(str: string): boolean;
|
|
145
152
|
/**
|
|
146
153
|
* Checks if a string is a valid JSON.
|
|
147
154
|
*
|
|
148
155
|
* @param {string} str - The input string.
|
|
149
156
|
* @returns {boolean} True if valid JSON.
|
|
150
157
|
*/
|
|
151
|
-
|
|
158
|
+
declare function isValidJSON(str: string): boolean;
|
|
152
159
|
/**
|
|
153
160
|
* Type guard that checks if a value is an array.
|
|
154
161
|
*
|
|
@@ -157,14 +164,14 @@ export declare function isValidJSON(str: string): boolean;
|
|
|
157
164
|
* @param {(item: unknown) => boolean} [itemGuard] - Optional function to validate each item.
|
|
158
165
|
* @returns {boolean} True if value is an array (with optional item validation).
|
|
159
166
|
*/
|
|
160
|
-
|
|
167
|
+
declare function isArray<T = unknown>(value: unknown, itemGuard?: (item: unknown) => item is T): value is T[];
|
|
161
168
|
/**
|
|
162
169
|
* Checks if a string is a valid base64 encoded string.
|
|
163
170
|
*
|
|
164
171
|
* @param {string} str - The input string.
|
|
165
172
|
* @returns {boolean} True if valid base64.
|
|
166
173
|
*/
|
|
167
|
-
|
|
174
|
+
declare function isBase64(str: string): boolean;
|
|
168
175
|
/**
|
|
169
176
|
* Validates that an object has all required properties.
|
|
170
177
|
*
|
|
@@ -172,7 +179,7 @@ export declare function isBase64(str: string): boolean;
|
|
|
172
179
|
* @param {string[]} requiredProps - Array of required property names.
|
|
173
180
|
* @returns {boolean} True if all required properties exist.
|
|
174
181
|
*/
|
|
175
|
-
|
|
182
|
+
declare function hasRequiredProps(obj: Record<string, unknown>, requiredProps: string[]): boolean;
|
|
176
183
|
/**
|
|
177
184
|
* Validates a date is within a specified range.
|
|
178
185
|
*
|
|
@@ -181,7 +188,7 @@ export declare function hasRequiredProps(obj: Record<string, unknown>, requiredP
|
|
|
181
188
|
* @param {Date} [maxDate] - Optional maximum date (inclusive).
|
|
182
189
|
* @returns {boolean} True if date is within range.
|
|
183
190
|
*/
|
|
184
|
-
|
|
191
|
+
declare function isDateInRange(date: Date, minDate?: Date, maxDate?: Date): boolean;
|
|
185
192
|
/**
|
|
186
193
|
* Validates a string matches a specific regular expression pattern.
|
|
187
194
|
*
|
|
@@ -189,7 +196,7 @@ export declare function isDateInRange(date: Date, minDate?: Date, maxDate?: Date
|
|
|
189
196
|
* @param {RegExp} pattern - Regular expression to test against.
|
|
190
197
|
* @returns {boolean} True if string matches pattern.
|
|
191
198
|
*/
|
|
192
|
-
|
|
199
|
+
declare function matchesPattern(str: string, pattern: RegExp): boolean;
|
|
193
200
|
/**
|
|
194
201
|
* Validates data against multiple constraints.
|
|
195
202
|
*
|
|
@@ -197,4 +204,6 @@ export declare function matchesPattern(str: string, pattern: RegExp): boolean;
|
|
|
197
204
|
* @param {Array<(value: unknown) => boolean>} validators - Array of validation functions.
|
|
198
205
|
* @returns {boolean} True if value passes all validations.
|
|
199
206
|
*/
|
|
200
|
-
|
|
207
|
+
declare function validateAll(value: unknown, validators: Array<(value: unknown) => boolean>): boolean;
|
|
208
|
+
|
|
209
|
+
export { hasRequiredProps, isAlpha, isAlphanumeric, isArray, isBase64, isCreditCard, isDateInRange, isEmail, isHexColor, isIPv4, isIPv6, isISODate, isLengthBetween, isNumberBetween, isNumeric, isPhone, isPort, isStrongPassword, isURL, isUUID, isValidJSON, matchesPattern, validateAll };
|