@catbee/utils 1.1.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/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'use strict';
|
|
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
|
+
|
|
33
|
+
var __defProp = Object.defineProperty;
|
|
34
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
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}$/;
|
|
45
|
+
function isPort(value) {
|
|
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);
|
|
51
|
+
return Number.isInteger(port) && port > 0 && port <= 65535;
|
|
52
|
+
}
|
|
53
|
+
__name(isPort, "isPort");
|
|
54
|
+
function isEmail(str) {
|
|
55
|
+
if (!str || typeof str !== "string" || str.length > 254) return false;
|
|
56
|
+
const atIndex = str.indexOf("@");
|
|
57
|
+
if (atIndex === -1 || atIndex !== str.lastIndexOf("@")) return false;
|
|
58
|
+
const local = str.slice(0, atIndex);
|
|
59
|
+
const domain = str.slice(atIndex + 1);
|
|
60
|
+
if (!local || !domain) return false;
|
|
61
|
+
if (!DOT_ATOM_REGEX.test(local) && !QUOTED_LOCAL_REGEX.test(local)) return false;
|
|
62
|
+
if (domain.includes("..")) return false;
|
|
63
|
+
const labels = domain.split(".");
|
|
64
|
+
if (labels.some((l) => !l.length)) return false;
|
|
65
|
+
if (labels.length < 2) return false;
|
|
66
|
+
const labelRegex = /^[A-Za-z0-9-]{1,63}$/;
|
|
67
|
+
for (const label of labels) {
|
|
68
|
+
if (!labelRegex.test(label)) return false;
|
|
69
|
+
if (label.startsWith("-") || label.endsWith("-")) return false;
|
|
70
|
+
}
|
|
71
|
+
const tld = labels[labels.length - 1];
|
|
72
|
+
if (!TLD_REGEX.test(tld)) return false;
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
__name(isEmail, "isEmail");
|
|
76
|
+
function isUUID(str) {
|
|
77
|
+
return UUID_REGEX.test(str);
|
|
78
|
+
}
|
|
79
|
+
__name(isUUID, "isUUID");
|
|
80
|
+
function isURL(str, allowedProtocols = [
|
|
81
|
+
"http",
|
|
82
|
+
"https",
|
|
83
|
+
"ws",
|
|
84
|
+
"wss"
|
|
85
|
+
]) {
|
|
86
|
+
if (!str || typeof str !== "string") return false;
|
|
87
|
+
try {
|
|
88
|
+
const url = new URL(str);
|
|
89
|
+
return allowedProtocols.includes(url.protocol.replace(":", ""));
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
__name(isURL, "isURL");
|
|
95
|
+
function isPhone(str) {
|
|
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);
|
|
100
|
+
}
|
|
101
|
+
__name(isPhone, "isPhone");
|
|
102
|
+
function isAlphanumeric(str) {
|
|
103
|
+
if (!str || typeof str !== "string") return false;
|
|
104
|
+
return ALPHANUMERIC_REGEX.test(str);
|
|
105
|
+
}
|
|
106
|
+
__name(isAlphanumeric, "isAlphanumeric");
|
|
107
|
+
function isNumeric(value) {
|
|
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);
|
|
114
|
+
}
|
|
115
|
+
__name(isNumeric, "isNumeric");
|
|
116
|
+
function isHexColor(str) {
|
|
117
|
+
if (!str || typeof str !== "string") return false;
|
|
118
|
+
return HEX_COLOR_REGEX.test(str);
|
|
119
|
+
}
|
|
120
|
+
__name(isHexColor, "isHexColor");
|
|
121
|
+
function isISODate(str) {
|
|
122
|
+
if (!str || typeof str !== "string") return false;
|
|
123
|
+
if (!ISO_DATE_REGEX.test(str)) return false;
|
|
124
|
+
const date = new Date(str);
|
|
125
|
+
return !Number.isNaN(date.getTime());
|
|
126
|
+
}
|
|
127
|
+
__name(isISODate, "isISODate");
|
|
128
|
+
function isLengthBetween(str, min, max) {
|
|
129
|
+
if (typeof str !== "string") return false;
|
|
130
|
+
return str.length >= min && str.length <= max;
|
|
131
|
+
}
|
|
132
|
+
__name(isLengthBetween, "isLengthBetween");
|
|
133
|
+
function isNumberBetween(value, min, max) {
|
|
134
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return false;
|
|
135
|
+
return value >= min && value <= max;
|
|
136
|
+
}
|
|
137
|
+
__name(isNumberBetween, "isNumberBetween");
|
|
138
|
+
function isAlpha(str) {
|
|
139
|
+
if (!str || typeof str !== "string") return false;
|
|
140
|
+
return /^[a-zA-Z]+$/.test(str);
|
|
141
|
+
}
|
|
142
|
+
__name(isAlpha, "isAlpha");
|
|
143
|
+
function isStrongPassword(str) {
|
|
144
|
+
if (str.length < 8) return false;
|
|
145
|
+
const hasUpperCase = /[A-Z]/.test(str);
|
|
146
|
+
const hasLowerCase = /[a-z]/.test(str);
|
|
147
|
+
const hasNumbers = /[0-9]/.test(str);
|
|
148
|
+
const hasSpecialChar = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>?]/.test(str);
|
|
149
|
+
return hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChar;
|
|
150
|
+
}
|
|
151
|
+
__name(isStrongPassword, "isStrongPassword");
|
|
152
|
+
function isIPv4(str) {
|
|
153
|
+
if (!str || typeof str !== "string") return false;
|
|
154
|
+
const parts = str.split(".");
|
|
155
|
+
if (parts.length !== 4) return false;
|
|
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;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
__name(isIPv4, "isIPv4");
|
|
163
|
+
function isIPv6(str) {
|
|
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
|
+
}
|
|
170
|
+
}
|
|
171
|
+
__name(isIPv6, "isIPv6");
|
|
172
|
+
function isCreditCard(str) {
|
|
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;
|
|
176
|
+
let sum = 0;
|
|
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;
|
|
182
|
+
}
|
|
183
|
+
sum += d;
|
|
184
|
+
}
|
|
185
|
+
return sum % 10 === 0;
|
|
186
|
+
}
|
|
187
|
+
__name(isCreditCard, "isCreditCard");
|
|
188
|
+
function isValidJSON(str) {
|
|
189
|
+
if (!str || typeof str !== "string") return false;
|
|
190
|
+
try {
|
|
191
|
+
JSON.parse(str);
|
|
192
|
+
return true;
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
__name(isValidJSON, "isValidJSON");
|
|
198
|
+
function isArray(value, itemGuard) {
|
|
199
|
+
if (!Array.isArray(value)) return false;
|
|
200
|
+
return !itemGuard || value.every((item) => itemGuard(item));
|
|
201
|
+
}
|
|
202
|
+
__name(isArray, "isArray");
|
|
203
|
+
function isBase64(str) {
|
|
204
|
+
if (!str || typeof str !== "string") return false;
|
|
205
|
+
return BASE64_REGEX.test(str);
|
|
206
|
+
}
|
|
207
|
+
__name(isBase64, "isBase64");
|
|
208
|
+
function hasRequiredProps(obj, requiredProps) {
|
|
209
|
+
if (!obj || typeof obj !== "object") return false;
|
|
210
|
+
return requiredProps.every((prop) => Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] != null);
|
|
211
|
+
}
|
|
212
|
+
__name(hasRequiredProps, "hasRequiredProps");
|
|
213
|
+
function isDateInRange(date, minDate, maxDate) {
|
|
214
|
+
if (!(date instanceof Date) || !Number.isFinite(date.getTime())) return false;
|
|
215
|
+
if (minDate && date < minDate) return false;
|
|
216
|
+
if (maxDate && date > maxDate) return false;
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
__name(isDateInRange, "isDateInRange");
|
|
220
|
+
function matchesPattern(str, pattern) {
|
|
221
|
+
if (typeof str !== "string") return false;
|
|
222
|
+
return pattern.test(str);
|
|
223
|
+
}
|
|
224
|
+
__name(matchesPattern, "matchesPattern");
|
|
225
|
+
function validateAll(value, validators) {
|
|
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
|
+
});
|
|
234
|
+
}
|
|
235
|
+
__name(validateAll, "validateAll");
|
|
236
|
+
|
|
237
|
+
exports.hasRequiredProps = hasRequiredProps;
|
|
238
|
+
exports.isAlpha = isAlpha;
|
|
239
|
+
exports.isAlphanumeric = isAlphanumeric;
|
|
240
|
+
exports.isArray = isArray;
|
|
241
|
+
exports.isBase64 = isBase64;
|
|
242
|
+
exports.isCreditCard = isCreditCard;
|
|
243
|
+
exports.isDateInRange = isDateInRange;
|
|
244
|
+
exports.isEmail = isEmail;
|
|
245
|
+
exports.isHexColor = isHexColor;
|
|
246
|
+
exports.isIPv4 = isIPv4;
|
|
247
|
+
exports.isIPv6 = isIPv6;
|
|
248
|
+
exports.isISODate = isISODate;
|
|
249
|
+
exports.isLengthBetween = isLengthBetween;
|
|
250
|
+
exports.isNumberBetween = isNumberBetween;
|
|
251
|
+
exports.isNumeric = isNumeric;
|
|
252
|
+
exports.isPhone = isPhone;
|
|
253
|
+
exports.isPort = isPort;
|
|
254
|
+
exports.isStrongPassword = isStrongPassword;
|
|
255
|
+
exports.isURL = isURL;
|
|
256
|
+
exports.isUUID = isUUID;
|
|
257
|
+
exports.isValidJSON = isValidJSON;
|
|
258
|
+
exports.matchesPattern = matchesPattern;
|
|
259
|
+
exports.validateAll = validateAll;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Collection of common validation helpers for strings, numbers, email, UUID, etc.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Checks if a string is a valid port number.
|
|
30
|
+
*
|
|
31
|
+
* @param str - The input string or number.
|
|
32
|
+
* @returns True if valid port number, else false.
|
|
33
|
+
*/
|
|
34
|
+
declare function isPort(value: string | number): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Checks if a string is a valid email address.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} str - The input string.
|
|
39
|
+
* @returns {boolean} True if valid email, else false.
|
|
40
|
+
*/
|
|
41
|
+
declare function isEmail(str: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if a string is a valid UUID (versions 1-5).
|
|
44
|
+
*
|
|
45
|
+
* @param {string} str - The input string.
|
|
46
|
+
* @returns {boolean} True if valid UUID, else false.
|
|
47
|
+
*/
|
|
48
|
+
declare function isUUID(str: string): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Checks if a string is a valid URL.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} str - The input string.
|
|
53
|
+
* @param {string[]} [allowedProtocols=['http', 'https', 'ws', 'wss']] - Allowed URL protocols.
|
|
54
|
+
* @returns {boolean} True if valid URL, else false.
|
|
55
|
+
*/
|
|
56
|
+
declare function isURL(str: string, allowedProtocols?: string[]): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Checks if a string is a valid international phone number (E.164 or common patterns).
|
|
59
|
+
*
|
|
60
|
+
* @param {string} str - The input string.
|
|
61
|
+
* @returns {boolean} True if looks like a phone number.
|
|
62
|
+
*/
|
|
63
|
+
declare function isPhone(str: string): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if a string is strictly alphanumeric (letters/numbers only).
|
|
66
|
+
*
|
|
67
|
+
* @param {string} str - The input string.
|
|
68
|
+
* @returns {boolean} True if alphanumeric.
|
|
69
|
+
*/
|
|
70
|
+
declare function isAlphanumeric(str: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Checks if a string or number can be safely parsed to a number.
|
|
73
|
+
*
|
|
74
|
+
* @param {string | number} value - The value to check.
|
|
75
|
+
* @returns {boolean} True if the value is numeric.
|
|
76
|
+
*/
|
|
77
|
+
declare function isNumeric(value: string | number): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Checks if a string is a valid hex color code (e.g. #FFF or #FFFFFF).
|
|
80
|
+
*
|
|
81
|
+
* @param {string} str - Input string.
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
declare function isHexColor(str: string): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Checks if a string is a valid date string.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} str - Input string.
|
|
89
|
+
* @returns {boolean}
|
|
90
|
+
*/
|
|
91
|
+
declare function isISODate(str: string): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Checks if a string matches a specified length range.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} str - The input string.
|
|
96
|
+
* @param {number} min - Minimum length (inclusive).
|
|
97
|
+
* @param {number} max - Maximum length (inclusive).
|
|
98
|
+
* @returns {boolean} True if string length is within range.
|
|
99
|
+
*/
|
|
100
|
+
declare function isLengthBetween(str: string, min: number, max: number): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Validates that a number is between specified min and max values (inclusive).
|
|
103
|
+
*
|
|
104
|
+
* @param {number} value - The number to validate.
|
|
105
|
+
* @param {number} min - Minimum value.
|
|
106
|
+
* @param {number} max - Maximum value.
|
|
107
|
+
* @returns {boolean} True if number is within range.
|
|
108
|
+
*/
|
|
109
|
+
declare function isNumberBetween(value: number, min: number, max: number): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Checks if a string contains only alphabetic characters.
|
|
112
|
+
*
|
|
113
|
+
* @param {string} str - The input string.
|
|
114
|
+
* @returns {boolean} True if alphabetic only.
|
|
115
|
+
*/
|
|
116
|
+
declare function isAlpha(str: string): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Validates that a string contains at least one uppercase letter,
|
|
119
|
+
* one lowercase letter, one number, and one special character.
|
|
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
|
+
*
|
|
127
|
+
* @param {string} str - The input string.
|
|
128
|
+
* @returns {boolean} True if string meets password complexity requirements.
|
|
129
|
+
*/
|
|
130
|
+
declare function isStrongPassword(str: string): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Checks if a string is a valid IPv4 address.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} str - The input string.
|
|
135
|
+
* @returns {boolean} True if valid IPv4 address.
|
|
136
|
+
*/
|
|
137
|
+
declare function isIPv4(str: string): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Checks if a string is a valid IPv6 address.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} str - The input string.
|
|
142
|
+
* @returns {boolean} True if valid IPv6 address.
|
|
143
|
+
*/
|
|
144
|
+
declare function isIPv6(str: string): boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Validates a credit card number using the Luhn algorithm.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} str - The credit card number string.
|
|
149
|
+
* @returns {boolean} True if valid credit card number.
|
|
150
|
+
*/
|
|
151
|
+
declare function isCreditCard(str: string): boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Checks if a string is a valid JSON.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} str - The input string.
|
|
156
|
+
* @returns {boolean} True if valid JSON.
|
|
157
|
+
*/
|
|
158
|
+
declare function isValidJSON(str: string): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Type guard that checks if a value is an array.
|
|
161
|
+
*
|
|
162
|
+
* @template T Optional expected element type
|
|
163
|
+
* @param {unknown} value - The value to check.
|
|
164
|
+
* @param {(item: unknown) => boolean} [itemGuard] - Optional function to validate each item.
|
|
165
|
+
* @returns {boolean} True if value is an array (with optional item validation).
|
|
166
|
+
*/
|
|
167
|
+
declare function isArray<T = unknown>(value: unknown, itemGuard?: (item: unknown) => item is T): value is T[];
|
|
168
|
+
/**
|
|
169
|
+
* Checks if a string is a valid base64 encoded string.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} str - The input string.
|
|
172
|
+
* @returns {boolean} True if valid base64.
|
|
173
|
+
*/
|
|
174
|
+
declare function isBase64(str: string): boolean;
|
|
175
|
+
/**
|
|
176
|
+
* Validates that an object has all required properties.
|
|
177
|
+
*
|
|
178
|
+
* @param {object} obj - The object to validate.
|
|
179
|
+
* @param {string[]} requiredProps - Array of required property names.
|
|
180
|
+
* @returns {boolean} True if all required properties exist.
|
|
181
|
+
*/
|
|
182
|
+
declare function hasRequiredProps(obj: Record<string, unknown>, requiredProps: string[]): boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Validates a date is within a specified range.
|
|
185
|
+
*
|
|
186
|
+
* @param {Date} date - The date to validate.
|
|
187
|
+
* @param {Date} [minDate] - Optional minimum date (inclusive).
|
|
188
|
+
* @param {Date} [maxDate] - Optional maximum date (inclusive).
|
|
189
|
+
* @returns {boolean} True if date is within range.
|
|
190
|
+
*/
|
|
191
|
+
declare function isDateInRange(date: Date, minDate?: Date, maxDate?: Date): boolean;
|
|
192
|
+
/**
|
|
193
|
+
* Validates a string matches a specific regular expression pattern.
|
|
194
|
+
*
|
|
195
|
+
* @param {string} str - The input string.
|
|
196
|
+
* @param {RegExp} pattern - Regular expression to test against.
|
|
197
|
+
* @returns {boolean} True if string matches pattern.
|
|
198
|
+
*/
|
|
199
|
+
declare function matchesPattern(str: string, pattern: RegExp): boolean;
|
|
200
|
+
/**
|
|
201
|
+
* Validates data against multiple constraints.
|
|
202
|
+
*
|
|
203
|
+
* @param {unknown} value - The value to validate.
|
|
204
|
+
* @param {Array<(value: unknown) => boolean>} validators - Array of validation functions.
|
|
205
|
+
* @returns {boolean} True if value passes all validations.
|
|
206
|
+
*/
|
|
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 };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import net from 'net';
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
var PORT_REGEX = /^[0-9]{1,5}$/;
|
|
30
|
+
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;
|
|
31
|
+
var PHONE_ALLOWED_CHARS_REGEX = /^[+]?[0-9\s().-]+$/;
|
|
32
|
+
var ALPHANUMERIC_REGEX = /^[A-Za-z0-9]+$/;
|
|
33
|
+
var HEX_COLOR_REGEX = /^#([a-f0-9]{6}|[a-f0-9]{3})$/i;
|
|
34
|
+
var ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
35
|
+
var BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
36
|
+
var DOT_ATOM_REGEX = /^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
|
|
37
|
+
var QUOTED_LOCAL_REGEX = /^"([\s\x21\x23-\x5B\x5D-\x7E]|\\[\x20-\x7E])*"$/;
|
|
38
|
+
var TLD_REGEX = /^[A-Za-z]{2,63}$/;
|
|
39
|
+
function isPort(value) {
|
|
40
|
+
if (typeof value === "number") return Number.isInteger(value) && value > 0 && value <= 65535;
|
|
41
|
+
if (!value || typeof value !== "string") return false;
|
|
42
|
+
const str = value.trim();
|
|
43
|
+
if (!PORT_REGEX.test(str)) return false;
|
|
44
|
+
const port = Number(str);
|
|
45
|
+
return Number.isInteger(port) && port > 0 && port <= 65535;
|
|
46
|
+
}
|
|
47
|
+
__name(isPort, "isPort");
|
|
48
|
+
function isEmail(str) {
|
|
49
|
+
if (!str || typeof str !== "string" || str.length > 254) return false;
|
|
50
|
+
const atIndex = str.indexOf("@");
|
|
51
|
+
if (atIndex === -1 || atIndex !== str.lastIndexOf("@")) return false;
|
|
52
|
+
const local = str.slice(0, atIndex);
|
|
53
|
+
const domain = str.slice(atIndex + 1);
|
|
54
|
+
if (!local || !domain) return false;
|
|
55
|
+
if (!DOT_ATOM_REGEX.test(local) && !QUOTED_LOCAL_REGEX.test(local)) return false;
|
|
56
|
+
if (domain.includes("..")) return false;
|
|
57
|
+
const labels = domain.split(".");
|
|
58
|
+
if (labels.some((l) => !l.length)) return false;
|
|
59
|
+
if (labels.length < 2) return false;
|
|
60
|
+
const labelRegex = /^[A-Za-z0-9-]{1,63}$/;
|
|
61
|
+
for (const label of labels) {
|
|
62
|
+
if (!labelRegex.test(label)) return false;
|
|
63
|
+
if (label.startsWith("-") || label.endsWith("-")) return false;
|
|
64
|
+
}
|
|
65
|
+
const tld = labels[labels.length - 1];
|
|
66
|
+
if (!TLD_REGEX.test(tld)) return false;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
__name(isEmail, "isEmail");
|
|
70
|
+
function isUUID(str) {
|
|
71
|
+
return UUID_REGEX.test(str);
|
|
72
|
+
}
|
|
73
|
+
__name(isUUID, "isUUID");
|
|
74
|
+
function isURL(str, allowedProtocols = [
|
|
75
|
+
"http",
|
|
76
|
+
"https",
|
|
77
|
+
"ws",
|
|
78
|
+
"wss"
|
|
79
|
+
]) {
|
|
80
|
+
if (!str || typeof str !== "string") return false;
|
|
81
|
+
try {
|
|
82
|
+
const url = new URL(str);
|
|
83
|
+
return allowedProtocols.includes(url.protocol.replace(":", ""));
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
__name(isURL, "isURL");
|
|
89
|
+
function isPhone(str) {
|
|
90
|
+
if (!str || typeof str !== "string") return false;
|
|
91
|
+
const digits = str.replace(/\D/g, "");
|
|
92
|
+
if (digits.length < 6 || digits.length > 15) return false;
|
|
93
|
+
return PHONE_ALLOWED_CHARS_REGEX.test(str);
|
|
94
|
+
}
|
|
95
|
+
__name(isPhone, "isPhone");
|
|
96
|
+
function isAlphanumeric(str) {
|
|
97
|
+
if (!str || typeof str !== "string") return false;
|
|
98
|
+
return ALPHANUMERIC_REGEX.test(str);
|
|
99
|
+
}
|
|
100
|
+
__name(isAlphanumeric, "isAlphanumeric");
|
|
101
|
+
function isNumeric(value) {
|
|
102
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
103
|
+
if (!value || typeof value !== "string") return false;
|
|
104
|
+
const str = value.trim();
|
|
105
|
+
if (str === "" || str.length > 16) return false;
|
|
106
|
+
const num = Number(str);
|
|
107
|
+
return Number.isFinite(num);
|
|
108
|
+
}
|
|
109
|
+
__name(isNumeric, "isNumeric");
|
|
110
|
+
function isHexColor(str) {
|
|
111
|
+
if (!str || typeof str !== "string") return false;
|
|
112
|
+
return HEX_COLOR_REGEX.test(str);
|
|
113
|
+
}
|
|
114
|
+
__name(isHexColor, "isHexColor");
|
|
115
|
+
function isISODate(str) {
|
|
116
|
+
if (!str || typeof str !== "string") return false;
|
|
117
|
+
if (!ISO_DATE_REGEX.test(str)) return false;
|
|
118
|
+
const date = new Date(str);
|
|
119
|
+
return !Number.isNaN(date.getTime());
|
|
120
|
+
}
|
|
121
|
+
__name(isISODate, "isISODate");
|
|
122
|
+
function isLengthBetween(str, min, max) {
|
|
123
|
+
if (typeof str !== "string") return false;
|
|
124
|
+
return str.length >= min && str.length <= max;
|
|
125
|
+
}
|
|
126
|
+
__name(isLengthBetween, "isLengthBetween");
|
|
127
|
+
function isNumberBetween(value, min, max) {
|
|
128
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return false;
|
|
129
|
+
return value >= min && value <= max;
|
|
130
|
+
}
|
|
131
|
+
__name(isNumberBetween, "isNumberBetween");
|
|
132
|
+
function isAlpha(str) {
|
|
133
|
+
if (!str || typeof str !== "string") return false;
|
|
134
|
+
return /^[a-zA-Z]+$/.test(str);
|
|
135
|
+
}
|
|
136
|
+
__name(isAlpha, "isAlpha");
|
|
137
|
+
function isStrongPassword(str) {
|
|
138
|
+
if (str.length < 8) return false;
|
|
139
|
+
const hasUpperCase = /[A-Z]/.test(str);
|
|
140
|
+
const hasLowerCase = /[a-z]/.test(str);
|
|
141
|
+
const hasNumbers = /[0-9]/.test(str);
|
|
142
|
+
const hasSpecialChar = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>?]/.test(str);
|
|
143
|
+
return hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChar;
|
|
144
|
+
}
|
|
145
|
+
__name(isStrongPassword, "isStrongPassword");
|
|
146
|
+
function isIPv4(str) {
|
|
147
|
+
if (!str || typeof str !== "string") return false;
|
|
148
|
+
const parts = str.split(".");
|
|
149
|
+
if (parts.length !== 4) return false;
|
|
150
|
+
return parts.every((p) => {
|
|
151
|
+
if (p.length > 3 || p.startsWith("0") && p.length > 1) return false;
|
|
152
|
+
const n = Number(p);
|
|
153
|
+
return Number.isInteger(n) && n >= 0 && n <= 255;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
__name(isIPv4, "isIPv4");
|
|
157
|
+
function isIPv6(str) {
|
|
158
|
+
if (!str || typeof str !== "string" || str.length > 45) return false;
|
|
159
|
+
try {
|
|
160
|
+
return net.isIP(str) === 6;
|
|
161
|
+
} catch {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
__name(isIPv6, "isIPv6");
|
|
166
|
+
function isCreditCard(str) {
|
|
167
|
+
if (!str || typeof str !== "string") return false;
|
|
168
|
+
const s = str.replace(/\D/g, "");
|
|
169
|
+
if (s.length < 13 || s.length > 19) return false;
|
|
170
|
+
let sum = 0;
|
|
171
|
+
for (let i = 0, dbl = false; i < s.length; i++, dbl = !dbl) {
|
|
172
|
+
let d = s.charCodeAt(s.length - 1 - i) - 48;
|
|
173
|
+
if (dbl) {
|
|
174
|
+
d *= 2;
|
|
175
|
+
if (d > 9) d -= 9;
|
|
176
|
+
}
|
|
177
|
+
sum += d;
|
|
178
|
+
}
|
|
179
|
+
return sum % 10 === 0;
|
|
180
|
+
}
|
|
181
|
+
__name(isCreditCard, "isCreditCard");
|
|
182
|
+
function isValidJSON(str) {
|
|
183
|
+
if (!str || typeof str !== "string") return false;
|
|
184
|
+
try {
|
|
185
|
+
JSON.parse(str);
|
|
186
|
+
return true;
|
|
187
|
+
} catch {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
__name(isValidJSON, "isValidJSON");
|
|
192
|
+
function isArray(value, itemGuard) {
|
|
193
|
+
if (!Array.isArray(value)) return false;
|
|
194
|
+
return !itemGuard || value.every((item) => itemGuard(item));
|
|
195
|
+
}
|
|
196
|
+
__name(isArray, "isArray");
|
|
197
|
+
function isBase64(str) {
|
|
198
|
+
if (!str || typeof str !== "string") return false;
|
|
199
|
+
return BASE64_REGEX.test(str);
|
|
200
|
+
}
|
|
201
|
+
__name(isBase64, "isBase64");
|
|
202
|
+
function hasRequiredProps(obj, requiredProps) {
|
|
203
|
+
if (!obj || typeof obj !== "object") return false;
|
|
204
|
+
return requiredProps.every((prop) => Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] != null);
|
|
205
|
+
}
|
|
206
|
+
__name(hasRequiredProps, "hasRequiredProps");
|
|
207
|
+
function isDateInRange(date, minDate, maxDate) {
|
|
208
|
+
if (!(date instanceof Date) || !Number.isFinite(date.getTime())) return false;
|
|
209
|
+
if (minDate && date < minDate) return false;
|
|
210
|
+
if (maxDate && date > maxDate) return false;
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
__name(isDateInRange, "isDateInRange");
|
|
214
|
+
function matchesPattern(str, pattern) {
|
|
215
|
+
if (typeof str !== "string") return false;
|
|
216
|
+
return pattern.test(str);
|
|
217
|
+
}
|
|
218
|
+
__name(matchesPattern, "matchesPattern");
|
|
219
|
+
function validateAll(value, validators) {
|
|
220
|
+
if (!Array.isArray(validators)) return false;
|
|
221
|
+
return validators.every((fn) => {
|
|
222
|
+
try {
|
|
223
|
+
return fn(value);
|
|
224
|
+
} catch {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
__name(validateAll, "validateAll");
|
|
230
|
+
|
|
231
|
+
export { hasRequiredProps, isAlpha, isAlphanumeric, isArray, isBase64, isCreditCard, isDateInRange, isEmail, isHexColor, isIPv4, isIPv6, isISODate, isLengthBetween, isNumberBetween, isNumeric, isPhone, isPort, isStrongPassword, isURL, isUUID, isValidJSON, matchesPattern, validateAll };
|