@catbee/utils 0.0.8-next.0 → 0.0.8-rc.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.
Files changed (284) hide show
  1. package/README.md +1 -2
  2. package/build/esm/config.d.ts +122 -0
  3. package/build/esm/config.js +133 -0
  4. package/build/esm/config.js.map +1 -0
  5. package/build/esm/index.d.ts +27 -0
  6. package/build/esm/index.js +50 -0
  7. package/build/esm/index.js.map +1 -0
  8. package/build/esm/servers/server.builder.d.ts +508 -0
  9. package/build/esm/servers/server.builder.js +702 -0
  10. package/build/esm/servers/server.builder.js.map +1 -0
  11. package/build/esm/servers/server.d.ts +256 -0
  12. package/build/esm/servers/server.js +1248 -0
  13. package/build/esm/servers/server.js.map +1 -0
  14. package/build/esm/types/api-response.d.ts +152 -0
  15. package/build/esm/types/api-response.js +34 -0
  16. package/build/esm/types/api-response.js.map +1 -0
  17. package/build/esm/types/index.d.ts +125 -0
  18. package/build/esm/types/index.js +25 -0
  19. package/build/esm/types/index.js.map +1 -0
  20. package/build/esm/types/server.d.ts +268 -0
  21. package/build/esm/types/server.js +25 -0
  22. package/build/esm/types/server.js.map +1 -0
  23. package/build/esm/utils/array.utils.d.ts +168 -0
  24. package/build/esm/utils/array.utils.js +382 -0
  25. package/build/esm/utils/array.utils.js.map +1 -0
  26. package/build/esm/utils/async.utils.d.ts +265 -0
  27. package/build/esm/utils/async.utils.js +896 -0
  28. package/build/esm/utils/async.utils.js.map +1 -0
  29. package/build/esm/utils/cache.utils.d.ts +153 -0
  30. package/build/esm/utils/cache.utils.js +499 -0
  31. package/build/esm/utils/cache.utils.js.map +1 -0
  32. package/build/esm/utils/context-store.utils.d.ts +189 -0
  33. package/build/esm/utils/context-store.utils.js +307 -0
  34. package/build/esm/utils/context-store.utils.js.map +1 -0
  35. package/build/esm/utils/crypto.utils.d.ts +160 -0
  36. package/build/esm/utils/crypto.utils.js +375 -0
  37. package/build/esm/utils/crypto.utils.js.map +1 -0
  38. package/build/esm/utils/date.utils.d.ts +159 -0
  39. package/build/esm/utils/date.utils.js +389 -0
  40. package/build/esm/utils/date.utils.js.map +1 -0
  41. package/build/esm/utils/decorators.utils.d.ts +508 -0
  42. package/build/esm/utils/decorators.utils.js +1057 -0
  43. package/build/esm/utils/decorators.utils.js.map +1 -0
  44. package/build/esm/utils/dir.utils.d.ts +196 -0
  45. package/build/esm/utils/dir.utils.js +998 -0
  46. package/build/esm/utils/dir.utils.js.map +1 -0
  47. package/build/esm/utils/env.utils.d.ts +377 -0
  48. package/build/esm/utils/env.utils.js +873 -0
  49. package/build/esm/utils/env.utils.js.map +1 -0
  50. package/build/esm/utils/exception.utils.d.ts +230 -0
  51. package/build/esm/utils/exception.utils.js +559 -0
  52. package/build/esm/utils/exception.utils.js.map +1 -0
  53. package/build/esm/utils/fs.utils.d.ts +164 -0
  54. package/build/esm/utils/fs.utils.js +580 -0
  55. package/build/esm/utils/fs.utils.js.map +1 -0
  56. package/build/esm/utils/http-status-codes.d.ts +266 -0
  57. package/build/esm/utils/http-status-codes.js +295 -0
  58. package/build/esm/utils/http-status-codes.js.map +1 -0
  59. package/build/esm/utils/id.utils.d.ts +36 -0
  60. package/build/esm/utils/id.utils.js +87 -0
  61. package/build/esm/utils/id.utils.js.map +1 -0
  62. package/build/esm/utils/logger.utils.d.ts +160 -0
  63. package/build/esm/utils/logger.utils.js +341 -0
  64. package/build/esm/utils/logger.utils.js.map +1 -0
  65. package/build/esm/utils/middleware.utils.d.ts +100 -0
  66. package/build/esm/utils/middleware.utils.js +325 -0
  67. package/build/esm/utils/middleware.utils.js.map +1 -0
  68. package/build/esm/utils/obj.utils.d.ts +124 -0
  69. package/build/esm/utils/obj.utils.js +519 -0
  70. package/build/esm/utils/obj.utils.js.map +1 -0
  71. package/build/esm/utils/performance.utils.d.ts +136 -0
  72. package/build/esm/utils/performance.utils.js +376 -0
  73. package/build/esm/utils/performance.utils.js.map +1 -0
  74. package/build/esm/utils/request.utils.d.ts +86 -0
  75. package/build/esm/utils/request.utils.js +223 -0
  76. package/build/esm/utils/request.utils.js.map +1 -0
  77. package/build/esm/utils/response.utils.d.ts +163 -0
  78. package/build/esm/utils/response.utils.js +284 -0
  79. package/build/esm/utils/response.utils.js.map +1 -0
  80. package/build/esm/utils/stream.utils.d.ts +88 -0
  81. package/build/esm/utils/stream.utils.js +284 -0
  82. package/build/esm/utils/stream.utils.js.map +1 -0
  83. package/build/esm/utils/string.utils.d.ts +93 -0
  84. package/build/esm/utils/string.utils.js +169 -0
  85. package/build/esm/utils/string.utils.js.map +1 -0
  86. package/build/esm/utils/type.utils.d.ts +90 -0
  87. package/build/esm/utils/type.utils.js +190 -0
  88. package/build/esm/utils/type.utils.js.map +1 -0
  89. package/build/esm/utils/url.utils.d.ts +141 -0
  90. package/build/esm/utils/url.utils.js +381 -0
  91. package/build/esm/utils/url.utils.js.map +1 -0
  92. package/build/esm/utils/validate.utils.d.ts +177 -0
  93. package/build/esm/utils/validate.utils.js +336 -0
  94. package/build/esm/utils/validate.utils.js.map +1 -0
  95. package/build/esnext/config.d.ts +122 -0
  96. package/build/esnext/config.js +133 -0
  97. package/build/esnext/config.js.map +1 -0
  98. package/build/esnext/index.d.ts +27 -0
  99. package/build/esnext/index.js +50 -0
  100. package/build/esnext/index.js.map +1 -0
  101. package/build/esnext/servers/server.builder.d.ts +508 -0
  102. package/build/esnext/servers/server.builder.js +657 -0
  103. package/build/esnext/servers/server.builder.js.map +1 -0
  104. package/build/esnext/servers/server.d.ts +256 -0
  105. package/build/esnext/servers/server.js +955 -0
  106. package/build/esnext/servers/server.js.map +1 -0
  107. package/build/esnext/types/api-response.d.ts +152 -0
  108. package/build/esnext/types/api-response.js +34 -0
  109. package/build/esnext/types/api-response.js.map +1 -0
  110. package/build/esnext/types/index.d.ts +125 -0
  111. package/build/esnext/types/index.js +25 -0
  112. package/build/esnext/types/index.js.map +1 -0
  113. package/build/esnext/types/server.d.ts +268 -0
  114. package/build/esnext/types/server.js +25 -0
  115. package/build/esnext/types/server.js.map +1 -0
  116. package/build/esnext/utils/array.utils.d.ts +168 -0
  117. package/build/esnext/utils/array.utils.js +345 -0
  118. package/build/esnext/utils/array.utils.js.map +1 -0
  119. package/build/esnext/utils/async.utils.d.ts +265 -0
  120. package/build/esnext/utils/async.utils.js +620 -0
  121. package/build/esnext/utils/async.utils.js.map +1 -0
  122. package/build/esnext/utils/cache.utils.d.ts +153 -0
  123. package/build/esnext/utils/cache.utils.js +293 -0
  124. package/build/esnext/utils/cache.utils.js.map +1 -0
  125. package/build/esnext/utils/context-store.utils.d.ts +189 -0
  126. package/build/esnext/utils/context-store.utils.js +289 -0
  127. package/build/esnext/utils/context-store.utils.js.map +1 -0
  128. package/build/esnext/utils/crypto.utils.d.ts +160 -0
  129. package/build/esnext/utils/crypto.utils.js +276 -0
  130. package/build/esnext/utils/crypto.utils.js.map +1 -0
  131. package/build/esnext/utils/date.utils.d.ts +159 -0
  132. package/build/esnext/utils/date.utils.js +384 -0
  133. package/build/esnext/utils/date.utils.js.map +1 -0
  134. package/build/esnext/utils/decorators.utils.d.ts +508 -0
  135. package/build/esnext/utils/decorators.utils.js +877 -0
  136. package/build/esnext/utils/decorators.utils.js.map +1 -0
  137. package/build/esnext/utils/dir.utils.d.ts +196 -0
  138. package/build/esnext/utils/dir.utils.js +477 -0
  139. package/build/esnext/utils/dir.utils.js.map +1 -0
  140. package/build/esnext/utils/env.utils.d.ts +377 -0
  141. package/build/esnext/utils/env.utils.js +785 -0
  142. package/build/esnext/utils/env.utils.js.map +1 -0
  143. package/build/esnext/utils/exception.utils.d.ts +230 -0
  144. package/build/esnext/utils/exception.utils.js +381 -0
  145. package/build/esnext/utils/exception.utils.js.map +1 -0
  146. package/build/esnext/utils/fs.utils.d.ts +164 -0
  147. package/build/esnext/utils/fs.utils.js +349 -0
  148. package/build/esnext/utils/fs.utils.js.map +1 -0
  149. package/build/esnext/utils/http-status-codes.d.ts +266 -0
  150. package/build/esnext/utils/http-status-codes.js +295 -0
  151. package/build/esnext/utils/http-status-codes.js.map +1 -0
  152. package/build/esnext/utils/id.utils.d.ts +36 -0
  153. package/build/esnext/utils/id.utils.js +84 -0
  154. package/build/esnext/utils/id.utils.js.map +1 -0
  155. package/build/esnext/utils/logger.utils.d.ts +160 -0
  156. package/build/esnext/utils/logger.utils.js +300 -0
  157. package/build/esnext/utils/logger.utils.js.map +1 -0
  158. package/build/esnext/utils/middleware.utils.d.ts +100 -0
  159. package/build/esnext/utils/middleware.utils.js +236 -0
  160. package/build/esnext/utils/middleware.utils.js.map +1 -0
  161. package/build/esnext/utils/obj.utils.d.ts +124 -0
  162. package/build/esnext/utils/obj.utils.js +413 -0
  163. package/build/esnext/utils/obj.utils.js.map +1 -0
  164. package/build/esnext/utils/performance.utils.d.ts +136 -0
  165. package/build/esnext/utils/performance.utils.js +271 -0
  166. package/build/esnext/utils/performance.utils.js.map +1 -0
  167. package/build/esnext/utils/request.utils.d.ts +86 -0
  168. package/build/esnext/utils/request.utils.js +186 -0
  169. package/build/esnext/utils/request.utils.js.map +1 -0
  170. package/build/esnext/utils/response.utils.d.ts +163 -0
  171. package/build/esnext/utils/response.utils.js +247 -0
  172. package/build/esnext/utils/response.utils.js.map +1 -0
  173. package/build/esnext/utils/stream.utils.d.ts +88 -0
  174. package/build/esnext/utils/stream.utils.js +210 -0
  175. package/build/esnext/utils/stream.utils.js.map +1 -0
  176. package/build/esnext/utils/string.utils.d.ts +93 -0
  177. package/build/esnext/utils/string.utils.js +165 -0
  178. package/build/esnext/utils/string.utils.js.map +1 -0
  179. package/build/esnext/utils/type.utils.d.ts +90 -0
  180. package/build/esnext/utils/type.utils.js +187 -0
  181. package/build/esnext/utils/type.utils.js.map +1 -0
  182. package/build/esnext/utils/url.utils.d.ts +141 -0
  183. package/build/esnext/utils/url.utils.js +304 -0
  184. package/build/esnext/utils/url.utils.js.map +1 -0
  185. package/build/esnext/utils/validate.utils.d.ts +177 -0
  186. package/build/esnext/utils/validate.utils.js +320 -0
  187. package/build/esnext/utils/validate.utils.js.map +1 -0
  188. package/build/src/config.d.ts +122 -0
  189. package/build/src/config.js +138 -0
  190. package/build/src/config.js.map +1 -0
  191. package/build/src/index.d.ts +27 -0
  192. package/build/src/index.js +69 -0
  193. package/build/src/index.js.map +1 -0
  194. package/build/src/servers/server.builder.d.ts +508 -0
  195. package/build/src/servers/server.builder.js +661 -0
  196. package/build/src/servers/server.builder.js.map +1 -0
  197. package/build/src/servers/server.d.ts +256 -0
  198. package/build/src/servers/server.js +962 -0
  199. package/build/src/servers/server.js.map +1 -0
  200. package/build/src/types/api-response.d.ts +152 -0
  201. package/build/src/types/api-response.js +37 -0
  202. package/build/src/types/api-response.js.map +1 -0
  203. package/build/src/types/index.d.ts +125 -0
  204. package/build/src/types/index.js +26 -0
  205. package/build/src/types/index.js.map +1 -0
  206. package/build/src/types/server.d.ts +268 -0
  207. package/build/src/types/server.js +26 -0
  208. package/build/src/types/server.js.map +1 -0
  209. package/build/src/utils/array.utils.d.ts +168 -0
  210. package/build/src/utils/array.utils.js +364 -0
  211. package/build/src/utils/array.utils.js.map +1 -0
  212. package/build/src/utils/async.utils.d.ts +265 -0
  213. package/build/src/utils/async.utils.js +641 -0
  214. package/build/src/utils/async.utils.js.map +1 -0
  215. package/build/src/utils/cache.utils.d.ts +153 -0
  216. package/build/src/utils/cache.utils.js +297 -0
  217. package/build/src/utils/cache.utils.js.map +1 -0
  218. package/build/src/utils/context-store.utils.d.ts +189 -0
  219. package/build/src/utils/context-store.utils.js +296 -0
  220. package/build/src/utils/context-store.utils.js.map +1 -0
  221. package/build/src/utils/crypto.utils.d.ts +160 -0
  222. package/build/src/utils/crypto.utils.js +293 -0
  223. package/build/src/utils/crypto.utils.js.map +1 -0
  224. package/build/src/utils/date.utils.d.ts +159 -0
  225. package/build/src/utils/date.utils.js +396 -0
  226. package/build/src/utils/date.utils.js.map +1 -0
  227. package/build/src/utils/decorators.utils.d.ts +508 -0
  228. package/build/src/utils/decorators.utils.js +896 -0
  229. package/build/src/utils/decorators.utils.js.map +1 -0
  230. package/build/src/utils/dir.utils.d.ts +196 -0
  231. package/build/src/utils/dir.utils.js +501 -0
  232. package/build/src/utils/dir.utils.js.map +1 -0
  233. package/build/src/utils/env.utils.d.ts +377 -0
  234. package/build/src/utils/env.utils.js +789 -0
  235. package/build/src/utils/env.utils.js.map +1 -0
  236. package/build/src/utils/exception.utils.d.ts +230 -0
  237. package/build/src/utils/exception.utils.js +407 -0
  238. package/build/src/utils/exception.utils.js.map +1 -0
  239. package/build/src/utils/fs.utils.d.ts +164 -0
  240. package/build/src/utils/fs.utils.js +372 -0
  241. package/build/src/utils/fs.utils.js.map +1 -0
  242. package/build/src/utils/http-status-codes.d.ts +266 -0
  243. package/build/src/utils/http-status-codes.js +298 -0
  244. package/build/src/utils/http-status-codes.js.map +1 -0
  245. package/build/src/utils/id.utils.d.ts +36 -0
  246. package/build/src/utils/id.utils.js +91 -0
  247. package/build/src/utils/id.utils.js.map +1 -0
  248. package/build/src/utils/logger.utils.d.ts +160 -0
  249. package/build/src/utils/logger.utils.js +346 -0
  250. package/build/src/utils/logger.utils.js.map +1 -0
  251. package/build/src/utils/middleware.utils.d.ts +100 -0
  252. package/build/src/utils/middleware.utils.js +244 -0
  253. package/build/src/utils/middleware.utils.js.map +1 -0
  254. package/build/src/utils/obj.utils.d.ts +124 -0
  255. package/build/src/utils/obj.utils.js +429 -0
  256. package/build/src/utils/obj.utils.js.map +1 -0
  257. package/build/src/utils/performance.utils.d.ts +136 -0
  258. package/build/src/utils/performance.utils.js +278 -0
  259. package/build/src/utils/performance.utils.js.map +1 -0
  260. package/build/src/utils/request.utils.d.ts +86 -0
  261. package/build/src/utils/request.utils.js +195 -0
  262. package/build/src/utils/request.utils.js.map +1 -0
  263. package/build/src/utils/response.utils.d.ts +163 -0
  264. package/build/src/utils/response.utils.js +260 -0
  265. package/build/src/utils/response.utils.js.map +1 -0
  266. package/build/src/utils/stream.utils.d.ts +88 -0
  267. package/build/src/utils/stream.utils.js +218 -0
  268. package/build/src/utils/stream.utils.js.map +1 -0
  269. package/build/src/utils/string.utils.d.ts +93 -0
  270. package/build/src/utils/string.utils.js +179 -0
  271. package/build/src/utils/string.utils.js.map +1 -0
  272. package/build/src/utils/type.utils.d.ts +90 -0
  273. package/build/src/utils/type.utils.js +196 -0
  274. package/build/src/utils/type.utils.js.map +1 -0
  275. package/build/src/utils/url.utils.d.ts +141 -0
  276. package/build/src/utils/url.utils.js +317 -0
  277. package/build/src/utils/url.utils.js.map +1 -0
  278. package/build/src/utils/validate.utils.d.ts +177 -0
  279. package/build/src/utils/validate.utils.js +345 -0
  280. package/build/src/utils/validate.utils.js.map +1 -0
  281. package/package.json +66 -12
  282. package/build/index.cjs +0 -6888
  283. package/build/index.d.ts +0 -5242
  284. package/build/index.mjs +0 -6615
@@ -0,0 +1,304 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/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
+ import { URL, URLSearchParams } from 'url';
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
+ export function appendQueryParams(url, params) {
37
+ const urlObj = new URL(url);
38
+ for (const [key, value] of Object.entries(params)) {
39
+ urlObj.searchParams.set(key, value.toString());
40
+ }
41
+ return urlObj.toString();
42
+ }
43
+ /**
44
+ * Parses a query string into a key-value object.
45
+ *
46
+ * @param {string} query - The query string (with or without leading '?').
47
+ * @returns {Record<string, string>} Object representing parsed query parameters.
48
+ *
49
+ * @example
50
+ * parseQueryString('?page=1&limit=10');
51
+ * // → { page: '1', limit: '10' }
52
+ */
53
+ export function parseQueryString(query) {
54
+ const cleanQuery = query.startsWith('?') ? query.slice(1) : query;
55
+ // Object.fromEntries guarantees string values in result
56
+ return Object.fromEntries(new URLSearchParams(cleanQuery));
57
+ }
58
+ /**
59
+ * Validates if a string is a valid URL.
60
+ *
61
+ * @param {string} url - The URL string to validate.
62
+ * @param {boolean} [requireHttps=false] - If true, requires the URL to use HTTPS.
63
+ * @returns {boolean} True if the URL is valid, false otherwise.
64
+ */
65
+ export function isValidUrl(url, requireHttps = false) {
66
+ try {
67
+ const parsedUrl = new URL(url);
68
+ if (!parsedUrl.protocol || !parsedUrl.hostname)
69
+ return false;
70
+ if (requireHttps && parsedUrl.protocol !== 'https:')
71
+ return false;
72
+ return true;
73
+ }
74
+ catch (_a) {
75
+ return false;
76
+ }
77
+ }
78
+ /**
79
+ * Extracts the domain name from a URL.
80
+ *
81
+ * @param {string} url - The URL to extract the domain from.
82
+ * @param {boolean} [removeSubdomains=false] - If true, removes subdomains (returns root domain only).
83
+ * @returns {string} The domain name.
84
+ *
85
+ * @example
86
+ * getDomain('https://api.example.com/path');
87
+ * // → 'api.example.com'
88
+ *
89
+ * getDomain('https://api.example.com/path', true);
90
+ * // → 'example.com'
91
+ */
92
+ export function getDomain(url, removeSubdomains = false) {
93
+ try {
94
+ const { hostname } = new URL(url);
95
+ if (!removeSubdomains) {
96
+ return hostname;
97
+ }
98
+ // Extract root domain (remove subdomains)
99
+ const parts = hostname.split('.');
100
+ if (parts.length <= 2)
101
+ return hostname;
102
+ // Handle special cases like co.in, com.au
103
+ const secondLevelDomains = ['co', 'com', 'org', 'net', 'gov', 'edu', 'ac'];
104
+ const sld = parts[parts.length - 2];
105
+ if (secondLevelDomains.includes(sld) && parts.length > 2) {
106
+ return parts.slice(-3).join('.');
107
+ }
108
+ return parts.slice(-2).join('.');
109
+ }
110
+ catch (_a) {
111
+ return '';
112
+ }
113
+ }
114
+ /**
115
+ * Joins URL paths properly handling slashes.
116
+ *
117
+ * @param {...string[]} segments - URL path segments to join.
118
+ * @returns {string} Joined URL path.
119
+ *
120
+ * @example
121
+ * joinPaths('https://example.com/', '/api/', '/users');
122
+ * // → 'https://example.com/api/users'
123
+ */
124
+ export function joinPaths(...segments) {
125
+ let result = segments
126
+ .filter(s => s !== undefined && s !== null)
127
+ .map((segment, index) => {
128
+ if (index === 0)
129
+ return segment.replace(/\/+$/, '');
130
+ return segment.replace(/^\/+|\/+$/g, '');
131
+ })
132
+ .filter(Boolean)
133
+ .join('/');
134
+ if (segments[0] === '' && !result.startsWith('/'))
135
+ result = '/' + result;
136
+ return result;
137
+ }
138
+ /**
139
+ * Normalizes a URL by resolving relative paths, handling protocol-relative URLs, etc.
140
+ *
141
+ * @param {string} url - The URL to normalize.
142
+ * @param {string} [base] - Optional base URL for resolving relative URLs.
143
+ * @returns {string} Normalized URL.
144
+ *
145
+ * @example
146
+ * normalizeUrl('HTTP://Example.COM/foo/../bar');
147
+ * // → 'http://example.com/bar'
148
+ */
149
+ export function normalizeUrl(url, base) {
150
+ try {
151
+ // Handle protocol-relative URLs
152
+ if (url.startsWith('//')) {
153
+ url = `https:${url}`;
154
+ }
155
+ // Resolve relative URLs against a base
156
+ const parsedUrl = base ? new URL(url, base) : new URL(url);
157
+ // Normalize
158
+ parsedUrl.pathname = parsedUrl.pathname.replace(/\/+/g, '/'); // Collapse multiple slashes
159
+ parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, ''); // Remove trailing slash
160
+ parsedUrl.hostname = parsedUrl.hostname.toLowerCase();
161
+ return parsedUrl.toString();
162
+ }
163
+ catch (_a) {
164
+ return url; // Return original if invalid
165
+ }
166
+ }
167
+ /**
168
+ * Creates a URL builder for constructing URLs with a base URL.
169
+ *
170
+ * @param {string} baseUrl - The base URL to build upon.
171
+ * @returns {Object} URL builder methods.
172
+ *
173
+ * @example
174
+ * const api = createUrlBuilder('https://api.example.com');
175
+ * api.path('/users', { active: true });
176
+ * // → 'https://api.example.com/users?active=true'
177
+ */
178
+ export function createUrlBuilder(baseUrl) {
179
+ return {
180
+ /**
181
+ * Creates a full URL with the given path and query parameters.
182
+ *
183
+ * @param {string} path - The path to append to the base URL.
184
+ * @param {Record<string, any>} [params] - Query parameters to add.
185
+ * @returns {string} The complete URL.
186
+ */
187
+ path(path, params) {
188
+ const url = joinPaths(baseUrl, path);
189
+ return params ? appendQueryParams(url, params) : url;
190
+ },
191
+ /**
192
+ * Creates a full URL with query parameters but no additional path.
193
+ *
194
+ * @param {Record<string, any>} params - Query parameters to add.
195
+ * @returns {string} The complete URL with query parameters.
196
+ */
197
+ query(params) {
198
+ return appendQueryParams(baseUrl, params);
199
+ }
200
+ };
201
+ }
202
+ /**
203
+ * Extracts specific query parameters from a URL.
204
+ *
205
+ * @param {string} url - The URL to extract parameters from.
206
+ * @param {string[]} paramNames - Names of parameters to extract.
207
+ * @returns {Record<string, string>} Object containing the extracted parameters.
208
+ */
209
+ export function extractQueryParams(url, paramNames) {
210
+ try {
211
+ const parsedUrl = new URL(url);
212
+ const result = {};
213
+ for (const name of paramNames) {
214
+ const value = parsedUrl.searchParams.get(name);
215
+ if (value !== null) {
216
+ result[name] = value;
217
+ }
218
+ }
219
+ return result;
220
+ }
221
+ catch (_a) {
222
+ return {};
223
+ }
224
+ }
225
+ /**
226
+ * Removes specified query parameters from a URL.
227
+ *
228
+ * @param {string} url - The URL to modify.
229
+ * @param {string[]} paramsToRemove - Names of parameters to remove.
230
+ * @returns {string} URL with parameters removed.
231
+ */
232
+ export function removeQueryParams(url, paramsToRemove) {
233
+ try {
234
+ const parsedUrl = new URL(url);
235
+ for (const param of paramsToRemove) {
236
+ parsedUrl.searchParams.delete(param);
237
+ }
238
+ return parsedUrl.toString();
239
+ }
240
+ catch (_a) {
241
+ return url;
242
+ }
243
+ }
244
+ /**
245
+ * Gets the file extension from a URL path.
246
+ *
247
+ * @param {string} url - The URL to examine.
248
+ * @returns {string} The file extension (without dot) or empty string if none found.
249
+ *
250
+ * @example
251
+ * getExtension('https://example.com/document.pdf?v=1');
252
+ * // → 'pdf'
253
+ */
254
+ export function getExtension(url) {
255
+ try {
256
+ const { pathname } = new URL(url);
257
+ const match = pathname.match(/\.([^./\\?#]+)$/);
258
+ return match ? match[1].toLowerCase() : '';
259
+ }
260
+ catch (_a) {
261
+ // If URL parsing fails, try a simpler approach
262
+ const match = url.match(/\.([^./\\?#]+)$/);
263
+ return match ? match[1].toLowerCase() : '';
264
+ }
265
+ }
266
+ /**
267
+ * Parses URL query parameters into a strongly-typed object.
268
+ *
269
+ * @template T Expected type of the query parameters
270
+ * @param {string} url - The URL to parse.
271
+ * @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.
272
+ * @returns {Partial<T>} Typed query parameters.
273
+ *
274
+ * @example
275
+ * parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {
276
+ * page: Number,
277
+ * q: String
278
+ * });
279
+ * // → { page: 2, q: 'test' }
280
+ */
281
+ export function parseTypedQueryParams(url, converters) {
282
+ try {
283
+ const { searchParams } = new URL(url);
284
+ const result = {};
285
+ for (const [key, value] of searchParams.entries()) {
286
+ if (converters && key in converters) {
287
+ try {
288
+ result[key] = converters[key](value);
289
+ }
290
+ catch (_a) {
291
+ // Skip on conversion error
292
+ }
293
+ }
294
+ else {
295
+ result[key] = value;
296
+ }
297
+ }
298
+ return result;
299
+ }
300
+ catch (_b) {
301
+ return {};
302
+ }
303
+ }
304
+ //# sourceMappingURL=url.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.utils.js","sourceRoot":"","sources":["../../../src/utils/url.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,GAAG,EAAE,eAAe,EAAE,MAAM,KAAK,CAAC;AAE3C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAW,EAAE,MAAuC;IACpF,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAClD,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC3B,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,MAAM,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAClE,wDAAwD;IACxD,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,eAAe,CAAC,UAAU,CAAC,CAA2B,CAAC;AACvF,CAAC;AAED;;;;;;GAMG;AACH,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AAG,CAAC,GAAG,CAAC,CAAC;QACtC,MAAM,MAAM,GAAG,EAAgB,CAAC;QAEhC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC;YAClD,IAAI,UAAU,IAAI,GAAG,IAAI,UAAU,EAAE,CAAC;gBACpC,IAAI,CAAC;oBACH,MAAM,CAAC,GAAc,CAAC,GAAG,UAAU,CAAC,GAAc,CAAC,CAAC,KAAK,CAAC,CAAC;gBAC7D,CAAC;gBAAC,WAAM,CAAC;oBACP,2BAA2B;gBAC7B,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,GAAc,CAAC,GAAG,KAAY,CAAC;YACxC,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { URL, URLSearchParams } from 'url';\n\n/**\n * Appends query parameters to a given URL.\n *\n * @param {string} url - The base URL to which query parameters will be appended.\n * @param {Record<string, string | number>} params - Key-value pairs to add as query parameters.\n * @returns {string} The new URL string with query parameters appended.\n *\n * @example\n * appendQueryParams('https://example.com', { page: 1, limit: 10 });\n * // → 'https://example.com/?page=1&limit=10'\n */\nexport function appendQueryParams(url: string, params: Record<string, string | number>): string {\n const urlObj = new URL(url);\n for (const [key, value] of Object.entries(params)) {\n urlObj.searchParams.set(key, value.toString());\n }\n return urlObj.toString();\n}\n\n/**\n * Parses a query string into a key-value object.\n *\n * @param {string} query - The query string (with or without leading '?').\n * @returns {Record<string, string>} Object representing parsed query parameters.\n *\n * @example\n * parseQueryString('?page=1&limit=10');\n * // → { page: '1', limit: '10' }\n */\nexport function parseQueryString(query: string): Record<string, string> {\n const cleanQuery = query.startsWith('?') ? query.slice(1) : query;\n // Object.fromEntries guarantees string values in result\n return Object.fromEntries(new URLSearchParams(cleanQuery)) as Record<string, string>;\n}\n\n/**\n * Validates if a string is a valid URL.\n *\n * @param {string} url - The URL string to validate.\n * @param {boolean} [requireHttps=false] - If true, requires the URL to use HTTPS.\n * @returns {boolean} True if the URL is valid, false otherwise.\n */\nexport function isValidUrl(url: string, requireHttps: boolean = false): boolean {\n try {\n const parsedUrl = new URL(url);\n if (!parsedUrl.protocol || !parsedUrl.hostname) return false;\n if (requireHttps && parsedUrl.protocol !== 'https:') return false;\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Extracts the domain name from a URL.\n *\n * @param {string} url - The URL to extract the domain from.\n * @param {boolean} [removeSubdomains=false] - If true, removes subdomains (returns root domain only).\n * @returns {string} The domain name.\n *\n * @example\n * getDomain('https://api.example.com/path');\n * // → 'api.example.com'\n *\n * getDomain('https://api.example.com/path', true);\n * // → 'example.com'\n */\nexport function getDomain(url: string, removeSubdomains: boolean = false): string {\n try {\n const { hostname } = new URL(url);\n\n if (!removeSubdomains) {\n return hostname;\n }\n\n // Extract root domain (remove subdomains)\n const parts = hostname.split('.');\n if (parts.length <= 2) return hostname;\n\n // Handle special cases like co.in, com.au\n const secondLevelDomains = ['co', 'com', 'org', 'net', 'gov', 'edu', 'ac'];\n const sld = parts[parts.length - 2];\n\n if (secondLevelDomains.includes(sld) && parts.length > 2) {\n return parts.slice(-3).join('.');\n }\n\n return parts.slice(-2).join('.');\n } catch {\n return '';\n }\n}\n\n/**\n * Joins URL paths properly handling slashes.\n *\n * @param {...string[]} segments - URL path segments to join.\n * @returns {string} Joined URL path.\n *\n * @example\n * joinPaths('https://example.com/', '/api/', '/users');\n * // → 'https://example.com/api/users'\n */\nexport function joinPaths(...segments: string[]): string {\n let result = segments\n .filter(s => s !== undefined && s !== null)\n .map((segment, index) => {\n if (index === 0) return segment.replace(/\\/+$/, '');\n return segment.replace(/^\\/+|\\/+$/g, '');\n })\n .filter(Boolean)\n .join('/');\n if (segments[0] === '' && !result.startsWith('/')) result = '/' + result;\n return result;\n}\n\n/**\n * Normalizes a URL by resolving relative paths, handling protocol-relative URLs, etc.\n *\n * @param {string} url - The URL to normalize.\n * @param {string} [base] - Optional base URL for resolving relative URLs.\n * @returns {string} Normalized URL.\n *\n * @example\n * normalizeUrl('HTTP://Example.COM/foo/../bar');\n * // → 'http://example.com/bar'\n */\nexport function normalizeUrl(url: string, base?: string): string {\n try {\n // Handle protocol-relative URLs\n if (url.startsWith('//')) {\n url = `https:${url}`;\n }\n\n // Resolve relative URLs against a base\n const parsedUrl = base ? new URL(url, base) : new URL(url);\n\n // Normalize\n parsedUrl.pathname = parsedUrl.pathname.replace(/\\/+/g, '/'); // Collapse multiple slashes\n parsedUrl.pathname = parsedUrl.pathname.replace(/\\/+$/, ''); // Remove trailing slash\n parsedUrl.hostname = parsedUrl.hostname.toLowerCase();\n\n return parsedUrl.toString();\n } catch {\n return url; // Return original if invalid\n }\n}\n\n/**\n * Creates a URL builder for constructing URLs with a base URL.\n *\n * @param {string} baseUrl - The base URL to build upon.\n * @returns {Object} URL builder methods.\n *\n * @example\n * const api = createUrlBuilder('https://api.example.com');\n * api.path('/users', { active: true });\n * // → 'https://api.example.com/users?active=true'\n */\nexport function createUrlBuilder(baseUrl: string) {\n return {\n /**\n * Creates a full URL with the given path and query parameters.\n *\n * @param {string} path - The path to append to the base URL.\n * @param {Record<string, any>} [params] - Query parameters to add.\n * @returns {string} The complete URL.\n */\n path(path: string, params?: Record<string, any>): string {\n const url = joinPaths(baseUrl, path);\n return params ? appendQueryParams(url, params as Record<string, string | number>) : url;\n },\n\n /**\n * Creates a full URL with query parameters but no additional path.\n *\n * @param {Record<string, any>} params - Query parameters to add.\n * @returns {string} The complete URL with query parameters.\n */\n query(params: Record<string, any>): string {\n return appendQueryParams(baseUrl, params as Record<string, string | number>);\n }\n };\n}\n\n/**\n * Extracts specific query parameters from a URL.\n *\n * @param {string} url - The URL to extract parameters from.\n * @param {string[]} paramNames - Names of parameters to extract.\n * @returns {Record<string, string>} Object containing the extracted parameters.\n */\nexport function extractQueryParams(url: string, paramNames: string[]): Record<string, string> {\n try {\n const parsedUrl = new URL(url);\n const result: Record<string, string> = {};\n\n for (const name of paramNames) {\n const value = parsedUrl.searchParams.get(name);\n if (value !== null) {\n result[name] = value;\n }\n }\n\n return result;\n } catch {\n return {};\n }\n}\n\n/**\n * Removes specified query parameters from a URL.\n *\n * @param {string} url - The URL to modify.\n * @param {string[]} paramsToRemove - Names of parameters to remove.\n * @returns {string} URL with parameters removed.\n */\nexport function removeQueryParams(url: string, paramsToRemove: string[]): string {\n try {\n const parsedUrl = new URL(url);\n\n for (const param of paramsToRemove) {\n parsedUrl.searchParams.delete(param);\n }\n\n return parsedUrl.toString();\n } catch {\n return url;\n }\n}\n\n/**\n * Gets the file extension from a URL path.\n *\n * @param {string} url - The URL to examine.\n * @returns {string} The file extension (without dot) or empty string if none found.\n *\n * @example\n * getExtension('https://example.com/document.pdf?v=1');\n * // → 'pdf'\n */\nexport function getExtension(url: string): string {\n try {\n const { pathname } = new URL(url);\n const match = pathname.match(/\\.([^./\\\\?#]+)$/);\n return match ? match[1].toLowerCase() : '';\n } catch {\n // If URL parsing fails, try a simpler approach\n const match = url.match(/\\.([^./\\\\?#]+)$/);\n return match ? match[1].toLowerCase() : '';\n }\n}\n\n/**\n * Parses URL query parameters into a strongly-typed object.\n *\n * @template T Expected type of the query parameters\n * @param {string} url - The URL to parse.\n * @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.\n * @returns {Partial<T>} Typed query parameters.\n *\n * @example\n * parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {\n * page: Number,\n * q: String\n * });\n * // → { page: 2, q: 'test' }\n */\nexport function parseTypedQueryParams<T extends Record<string, any>>(\n url: string,\n converters?: Record<keyof T, (val: string) => any>\n): Partial<T> {\n try {\n const { searchParams } = new URL(url);\n const result = {} as Partial<T>;\n\n for (const [key, value] of searchParams.entries()) {\n if (converters && key in converters) {\n try {\n result[key as keyof T] = converters[key as keyof T](value);\n } catch {\n // Skip on conversion error\n }\n } else {\n result[key as keyof T] = value as any;\n }\n }\n\n return result;\n } catch {\n return {};\n }\n}\n"]}
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Collection of common validation helpers for strings, numbers, email, UUID, etc.
3
+ */
4
+ /**
5
+ * Checks if a string is a valid port number.
6
+ *
7
+ * @param str - The input string or number.
8
+ * @returns True if valid port number, else false.
9
+ */
10
+ export declare function isPort(value: string | number): boolean;
11
+ /**
12
+ * Checks if a string is a valid email address.
13
+ *
14
+ * @param {string} str - The input string.
15
+ * @returns {boolean} True if valid email, else false.
16
+ */
17
+ export declare function isEmail(str: string): boolean;
18
+ /**
19
+ * Checks if a string is a valid UUID (versions 1-5).
20
+ *
21
+ * @param {string} str - The input string.
22
+ * @returns {boolean} True if valid UUID, else false.
23
+ */
24
+ export declare function isUUID(str: string): boolean;
25
+ /**
26
+ * Checks if a string is a valid URL.
27
+ *
28
+ * @param {string} str - The input string.
29
+ * @returns {boolean} True if valid URL, else false.
30
+ */
31
+ export declare function isURL(str: string): boolean;
32
+ /**
33
+ * Checks if a string is a valid international phone number (E.164 or common patterns).
34
+ *
35
+ * @param {string} str - The input string.
36
+ * @returns {boolean} True if looks like a phone number.
37
+ */
38
+ export declare function isPhone(str: string): boolean;
39
+ /**
40
+ * Checks if a string is strictly alphanumeric (letters/numbers only).
41
+ *
42
+ * @param {string} str - The input string.
43
+ * @returns {boolean} True if alphanumeric.
44
+ */
45
+ export declare function isAlphanumeric(str: string): boolean;
46
+ /**
47
+ * Checks if a string or number can be safely parsed to a number.
48
+ *
49
+ * @param {string | number} value - The value to check.
50
+ * @returns {boolean} True if the value is numeric.
51
+ */
52
+ export declare function isNumeric(value: string | number): boolean;
53
+ /**
54
+ * Checks if a string is a valid hex color code (e.g. #FFF or #FFFFFF).
55
+ *
56
+ * @param {string} str - Input string.
57
+ * @returns {boolean}
58
+ */
59
+ export declare function isHexColor(str: string): boolean;
60
+ /**
61
+ * Checks if a string is a valid date string.
62
+ *
63
+ * @param {string} str - Input string.
64
+ * @returns {boolean}
65
+ */
66
+ export declare function isISODate(str: string): boolean;
67
+ /**
68
+ * Checks if a string matches a specified length range.
69
+ *
70
+ * @param {string} str - The input string.
71
+ * @param {number} min - Minimum length (inclusive).
72
+ * @param {number} max - Maximum length (inclusive).
73
+ * @returns {boolean} True if string length is within range.
74
+ */
75
+ export declare function isLengthBetween(str: string, min: number, max: number): boolean;
76
+ /**
77
+ * Validates that a number is between specified min and max values (inclusive).
78
+ *
79
+ * @param {number} value - The number to validate.
80
+ * @param {number} min - Minimum value.
81
+ * @param {number} max - Maximum value.
82
+ * @returns {boolean} True if number is within range.
83
+ */
84
+ export declare function isNumberBetween(value: number, min: number, max: number): boolean;
85
+ /**
86
+ * Checks if a string contains only alphabetic characters.
87
+ *
88
+ * @param {string} str - The input string.
89
+ * @returns {boolean} True if alphabetic only.
90
+ */
91
+ export declare function isAlpha(str: string): boolean;
92
+ /**
93
+ * Validates that a string contains at least one uppercase letter,
94
+ * one lowercase letter, one number, and one special character.
95
+ *
96
+ * @param {string} str - The input string.
97
+ * @returns {boolean} True if string meets password complexity requirements.
98
+ */
99
+ export declare function isStrongPassword(str: string): boolean;
100
+ /**
101
+ * Checks if a string is a valid IPv4 address.
102
+ *
103
+ * @param {string} str - The input string.
104
+ * @returns {boolean} True if valid IPv4 address.
105
+ */
106
+ export declare function isIPv4(str: string): boolean;
107
+ /**
108
+ * Checks if a string is a valid IPv6 address.
109
+ *
110
+ * @param {string} str - The input string.
111
+ * @returns {boolean} True if valid IPv6 address.
112
+ */
113
+ export declare function isIPv6(str: string): boolean;
114
+ /**
115
+ * Validates a credit card number using the Luhn algorithm.
116
+ *
117
+ * @param {string} str - The credit card number string.
118
+ * @returns {boolean} True if valid credit card number.
119
+ */
120
+ export declare function isCreditCard(str: string): boolean;
121
+ /**
122
+ * Checks if a string is a valid JSON.
123
+ *
124
+ * @param {string} str - The input string.
125
+ * @returns {boolean} True if valid JSON.
126
+ */
127
+ export declare function isValidJSON(str: string): boolean;
128
+ /**
129
+ * Type guard that checks if a value is an array.
130
+ *
131
+ * @template T Optional expected element type
132
+ * @param {unknown} value - The value to check.
133
+ * @param {(item: unknown) => boolean} [itemGuard] - Optional function to validate each item.
134
+ * @returns {boolean} True if value is an array (with optional item validation).
135
+ */
136
+ export declare function isArray<T = unknown>(value: unknown, itemGuard?: (item: unknown) => item is T): value is T[];
137
+ /**
138
+ * Checks if a string is a valid base64 encoded string.
139
+ *
140
+ * @param {string} str - The input string.
141
+ * @returns {boolean} True if valid base64.
142
+ */
143
+ export declare function isBase64(str: string): boolean;
144
+ /**
145
+ * Validates that an object has all required properties.
146
+ *
147
+ * @param {object} obj - The object to validate.
148
+ * @param {string[]} requiredProps - Array of required property names.
149
+ * @returns {boolean} True if all required properties exist.
150
+ */
151
+ export declare function hasRequiredProps(obj: Record<string, unknown>, requiredProps: string[]): boolean;
152
+ /**
153
+ * Validates a date is within a specified range.
154
+ *
155
+ * @param {Date} date - The date to validate.
156
+ * @param {Date} [minDate] - Optional minimum date (inclusive).
157
+ * @param {Date} [maxDate] - Optional maximum date (inclusive).
158
+ * @returns {boolean} True if date is within range.
159
+ */
160
+ export declare function isDateInRange(date: Date, minDate?: Date, maxDate?: Date): boolean;
161
+ /**
162
+ * Validates a string matches a specific regular expression pattern.
163
+ *
164
+ * @param {string} str - The input string.
165
+ * @param {RegExp} pattern - Regular expression to test against.
166
+ * @returns {boolean} True if string matches pattern.
167
+ */
168
+ export declare function matchesPattern(str: string, pattern: RegExp): boolean;
169
+ /**
170
+ * Validates data against multiple constraints.
171
+ *
172
+ * @param {unknown} value - The value to validate.
173
+ * @param {Array<(value: unknown) => boolean>} validators - Array of validation functions.
174
+ * @returns {boolean} True if value passes all validations.
175
+ */
176
+ export declare function validateAll(value: unknown, validators: Array<(value: unknown) => boolean>): boolean;
177
+ //# sourceMappingURL=validate.utils.d.ts.map