@catbee/utils 0.0.6 → 0.0.8-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +77 -1107
  3. package/build/index.cjs +6888 -0
  4. package/build/index.d.ts +5242 -0
  5. package/build/index.mjs +6615 -0
  6. package/package.json +26 -95
  7. package/build/esm/config.d.ts +0 -130
  8. package/build/esm/config.js +0 -145
  9. package/build/esm/config.js.map +0 -1
  10. package/build/esm/index.d.ts +0 -27
  11. package/build/esm/index.js +0 -50
  12. package/build/esm/index.js.map +0 -1
  13. package/build/esm/servers/server.builder.d.ts +0 -625
  14. package/build/esm/servers/server.builder.js +0 -722
  15. package/build/esm/servers/server.builder.js.map +0 -1
  16. package/build/esm/servers/server.d.ts +0 -256
  17. package/build/esm/servers/server.js +0 -1211
  18. package/build/esm/servers/server.js.map +0 -1
  19. package/build/esm/types/api-response.d.ts +0 -124
  20. package/build/esm/types/api-response.js +0 -25
  21. package/build/esm/types/api-response.js.map +0 -1
  22. package/build/esm/types/index.d.ts +0 -125
  23. package/build/esm/types/index.js +0 -25
  24. package/build/esm/types/index.js.map +0 -1
  25. package/build/esm/types/server.d.ts +0 -285
  26. package/build/esm/types/server.js +0 -25
  27. package/build/esm/types/server.js.map +0 -1
  28. package/build/esm/utils/array.utils.d.ts +0 -168
  29. package/build/esm/utils/array.utils.js +0 -382
  30. package/build/esm/utils/array.utils.js.map +0 -1
  31. package/build/esm/utils/async.utils.d.ts +0 -174
  32. package/build/esm/utils/async.utils.js +0 -652
  33. package/build/esm/utils/async.utils.js.map +0 -1
  34. package/build/esm/utils/cache.utils.d.ts +0 -153
  35. package/build/esm/utils/cache.utils.js +0 -499
  36. package/build/esm/utils/cache.utils.js.map +0 -1
  37. package/build/esm/utils/context-store.utils.d.ts +0 -186
  38. package/build/esm/utils/context-store.utils.js +0 -304
  39. package/build/esm/utils/context-store.utils.js.map +0 -1
  40. package/build/esm/utils/crypto.utils.d.ts +0 -160
  41. package/build/esm/utils/crypto.utils.js +0 -375
  42. package/build/esm/utils/crypto.utils.js.map +0 -1
  43. package/build/esm/utils/decorators.utils.d.ts +0 -315
  44. package/build/esm/utils/decorators.utils.js +0 -666
  45. package/build/esm/utils/decorators.utils.js.map +0 -1
  46. package/build/esm/utils/dir.utils.d.ts +0 -196
  47. package/build/esm/utils/dir.utils.js +0 -998
  48. package/build/esm/utils/dir.utils.js.map +0 -1
  49. package/build/esm/utils/env.utils.d.ts +0 -377
  50. package/build/esm/utils/env.utils.js +0 -873
  51. package/build/esm/utils/env.utils.js.map +0 -1
  52. package/build/esm/utils/exception.utils.d.ts +0 -230
  53. package/build/esm/utils/exception.utils.js +0 -559
  54. package/build/esm/utils/exception.utils.js.map +0 -1
  55. package/build/esm/utils/fs.utils.d.ts +0 -164
  56. package/build/esm/utils/fs.utils.js +0 -580
  57. package/build/esm/utils/fs.utils.js.map +0 -1
  58. package/build/esm/utils/http-status-codes.d.ts +0 -266
  59. package/build/esm/utils/http-status-codes.js +0 -295
  60. package/build/esm/utils/http-status-codes.js.map +0 -1
  61. package/build/esm/utils/id.utils.d.ts +0 -36
  62. package/build/esm/utils/id.utils.js +0 -87
  63. package/build/esm/utils/id.utils.js.map +0 -1
  64. package/build/esm/utils/logger.utils.d.ts +0 -166
  65. package/build/esm/utils/logger.utils.js +0 -347
  66. package/build/esm/utils/logger.utils.js.map +0 -1
  67. package/build/esm/utils/middleware.utils.d.ts +0 -83
  68. package/build/esm/utils/middleware.utils.js +0 -205
  69. package/build/esm/utils/middleware.utils.js.map +0 -1
  70. package/build/esm/utils/obj.utils.d.ts +0 -124
  71. package/build/esm/utils/obj.utils.js +0 -519
  72. package/build/esm/utils/obj.utils.js.map +0 -1
  73. package/build/esm/utils/request.utils.d.ts +0 -77
  74. package/build/esm/utils/request.utils.js +0 -196
  75. package/build/esm/utils/request.utils.js.map +0 -1
  76. package/build/esm/utils/response.utils.d.ts +0 -163
  77. package/build/esm/utils/response.utils.js +0 -284
  78. package/build/esm/utils/response.utils.js.map +0 -1
  79. package/build/esm/utils/string.utils.d.ts +0 -93
  80. package/build/esm/utils/string.utils.js +0 -169
  81. package/build/esm/utils/string.utils.js.map +0 -1
  82. package/build/esm/utils/url.utils.d.ts +0 -141
  83. package/build/esm/utils/url.utils.js +0 -381
  84. package/build/esm/utils/url.utils.js.map +0 -1
  85. package/build/esm/utils/validate.utils.d.ts +0 -177
  86. package/build/esm/utils/validate.utils.js +0 -336
  87. package/build/esm/utils/validate.utils.js.map +0 -1
  88. package/build/esnext/config.d.ts +0 -130
  89. package/build/esnext/config.js +0 -145
  90. package/build/esnext/config.js.map +0 -1
  91. package/build/esnext/index.d.ts +0 -27
  92. package/build/esnext/index.js +0 -50
  93. package/build/esnext/index.js.map +0 -1
  94. package/build/esnext/servers/server.builder.d.ts +0 -625
  95. package/build/esnext/servers/server.builder.js +0 -677
  96. package/build/esnext/servers/server.builder.js.map +0 -1
  97. package/build/esnext/servers/server.d.ts +0 -256
  98. package/build/esnext/servers/server.js +0 -918
  99. package/build/esnext/servers/server.js.map +0 -1
  100. package/build/esnext/types/api-response.d.ts +0 -124
  101. package/build/esnext/types/api-response.js +0 -25
  102. package/build/esnext/types/api-response.js.map +0 -1
  103. package/build/esnext/types/index.d.ts +0 -125
  104. package/build/esnext/types/index.js +0 -25
  105. package/build/esnext/types/index.js.map +0 -1
  106. package/build/esnext/types/server.d.ts +0 -285
  107. package/build/esnext/types/server.js +0 -25
  108. package/build/esnext/types/server.js.map +0 -1
  109. package/build/esnext/utils/array.utils.d.ts +0 -168
  110. package/build/esnext/utils/array.utils.js +0 -345
  111. package/build/esnext/utils/array.utils.js.map +0 -1
  112. package/build/esnext/utils/async.utils.d.ts +0 -174
  113. package/build/esnext/utils/async.utils.js +0 -430
  114. package/build/esnext/utils/async.utils.js.map +0 -1
  115. package/build/esnext/utils/cache.utils.d.ts +0 -153
  116. package/build/esnext/utils/cache.utils.js +0 -293
  117. package/build/esnext/utils/cache.utils.js.map +0 -1
  118. package/build/esnext/utils/context-store.utils.d.ts +0 -186
  119. package/build/esnext/utils/context-store.utils.js +0 -286
  120. package/build/esnext/utils/context-store.utils.js.map +0 -1
  121. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  122. package/build/esnext/utils/crypto.utils.js +0 -276
  123. package/build/esnext/utils/crypto.utils.js.map +0 -1
  124. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  125. package/build/esnext/utils/decorators.utils.js +0 -504
  126. package/build/esnext/utils/decorators.utils.js.map +0 -1
  127. package/build/esnext/utils/dir.utils.d.ts +0 -196
  128. package/build/esnext/utils/dir.utils.js +0 -477
  129. package/build/esnext/utils/dir.utils.js.map +0 -1
  130. package/build/esnext/utils/env.utils.d.ts +0 -377
  131. package/build/esnext/utils/env.utils.js +0 -785
  132. package/build/esnext/utils/env.utils.js.map +0 -1
  133. package/build/esnext/utils/exception.utils.d.ts +0 -230
  134. package/build/esnext/utils/exception.utils.js +0 -381
  135. package/build/esnext/utils/exception.utils.js.map +0 -1
  136. package/build/esnext/utils/fs.utils.d.ts +0 -164
  137. package/build/esnext/utils/fs.utils.js +0 -349
  138. package/build/esnext/utils/fs.utils.js.map +0 -1
  139. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  140. package/build/esnext/utils/http-status-codes.js +0 -295
  141. package/build/esnext/utils/http-status-codes.js.map +0 -1
  142. package/build/esnext/utils/id.utils.d.ts +0 -36
  143. package/build/esnext/utils/id.utils.js +0 -84
  144. package/build/esnext/utils/id.utils.js.map +0 -1
  145. package/build/esnext/utils/logger.utils.d.ts +0 -166
  146. package/build/esnext/utils/logger.utils.js +0 -306
  147. package/build/esnext/utils/logger.utils.js.map +0 -1
  148. package/build/esnext/utils/middleware.utils.d.ts +0 -83
  149. package/build/esnext/utils/middleware.utils.js +0 -198
  150. package/build/esnext/utils/middleware.utils.js.map +0 -1
  151. package/build/esnext/utils/obj.utils.d.ts +0 -124
  152. package/build/esnext/utils/obj.utils.js +0 -413
  153. package/build/esnext/utils/obj.utils.js.map +0 -1
  154. package/build/esnext/utils/request.utils.d.ts +0 -77
  155. package/build/esnext/utils/request.utils.js +0 -168
  156. package/build/esnext/utils/request.utils.js.map +0 -1
  157. package/build/esnext/utils/response.utils.d.ts +0 -163
  158. package/build/esnext/utils/response.utils.js +0 -247
  159. package/build/esnext/utils/response.utils.js.map +0 -1
  160. package/build/esnext/utils/string.utils.d.ts +0 -93
  161. package/build/esnext/utils/string.utils.js +0 -165
  162. package/build/esnext/utils/string.utils.js.map +0 -1
  163. package/build/esnext/utils/url.utils.d.ts +0 -141
  164. package/build/esnext/utils/url.utils.js +0 -304
  165. package/build/esnext/utils/url.utils.js.map +0 -1
  166. package/build/esnext/utils/validate.utils.d.ts +0 -177
  167. package/build/esnext/utils/validate.utils.js +0 -320
  168. package/build/esnext/utils/validate.utils.js.map +0 -1
  169. package/build/src/config.d.ts +0 -130
  170. package/build/src/config.js +0 -150
  171. package/build/src/config.js.map +0 -1
  172. package/build/src/index.d.ts +0 -27
  173. package/build/src/index.js +0 -69
  174. package/build/src/index.js.map +0 -1
  175. package/build/src/servers/server.builder.d.ts +0 -625
  176. package/build/src/servers/server.builder.js +0 -681
  177. package/build/src/servers/server.builder.js.map +0 -1
  178. package/build/src/servers/server.d.ts +0 -256
  179. package/build/src/servers/server.js +0 -958
  180. package/build/src/servers/server.js.map +0 -1
  181. package/build/src/types/api-response.d.ts +0 -124
  182. package/build/src/types/api-response.js +0 -26
  183. package/build/src/types/api-response.js.map +0 -1
  184. package/build/src/types/index.d.ts +0 -125
  185. package/build/src/types/index.js +0 -26
  186. package/build/src/types/index.js.map +0 -1
  187. package/build/src/types/server.d.ts +0 -285
  188. package/build/src/types/server.js +0 -26
  189. package/build/src/types/server.js.map +0 -1
  190. package/build/src/utils/array.utils.d.ts +0 -168
  191. package/build/src/utils/array.utils.js +0 -364
  192. package/build/src/utils/array.utils.js.map +0 -1
  193. package/build/src/utils/async.utils.d.ts +0 -174
  194. package/build/src/utils/async.utils.js +0 -447
  195. package/build/src/utils/async.utils.js.map +0 -1
  196. package/build/src/utils/cache.utils.d.ts +0 -153
  197. package/build/src/utils/cache.utils.js +0 -297
  198. package/build/src/utils/cache.utils.js.map +0 -1
  199. package/build/src/utils/context-store.utils.d.ts +0 -186
  200. package/build/src/utils/context-store.utils.js +0 -293
  201. package/build/src/utils/context-store.utils.js.map +0 -1
  202. package/build/src/utils/crypto.utils.d.ts +0 -160
  203. package/build/src/utils/crypto.utils.js +0 -293
  204. package/build/src/utils/crypto.utils.js.map +0 -1
  205. package/build/src/utils/decorators.utils.d.ts +0 -315
  206. package/build/src/utils/decorators.utils.js +0 -516
  207. package/build/src/utils/decorators.utils.js.map +0 -1
  208. package/build/src/utils/dir.utils.d.ts +0 -196
  209. package/build/src/utils/dir.utils.js +0 -501
  210. package/build/src/utils/dir.utils.js.map +0 -1
  211. package/build/src/utils/env.utils.d.ts +0 -377
  212. package/build/src/utils/env.utils.js +0 -789
  213. package/build/src/utils/env.utils.js.map +0 -1
  214. package/build/src/utils/exception.utils.d.ts +0 -230
  215. package/build/src/utils/exception.utils.js +0 -407
  216. package/build/src/utils/exception.utils.js.map +0 -1
  217. package/build/src/utils/fs.utils.d.ts +0 -164
  218. package/build/src/utils/fs.utils.js +0 -372
  219. package/build/src/utils/fs.utils.js.map +0 -1
  220. package/build/src/utils/http-status-codes.d.ts +0 -266
  221. package/build/src/utils/http-status-codes.js +0 -298
  222. package/build/src/utils/http-status-codes.js.map +0 -1
  223. package/build/src/utils/id.utils.d.ts +0 -36
  224. package/build/src/utils/id.utils.js +0 -91
  225. package/build/src/utils/id.utils.js.map +0 -1
  226. package/build/src/utils/logger.utils.d.ts +0 -166
  227. package/build/src/utils/logger.utils.js +0 -352
  228. package/build/src/utils/logger.utils.js.map +0 -1
  229. package/build/src/utils/middleware.utils.d.ts +0 -83
  230. package/build/src/utils/middleware.utils.js +0 -205
  231. package/build/src/utils/middleware.utils.js.map +0 -1
  232. package/build/src/utils/obj.utils.d.ts +0 -124
  233. package/build/src/utils/obj.utils.js +0 -429
  234. package/build/src/utils/obj.utils.js.map +0 -1
  235. package/build/src/utils/request.utils.d.ts +0 -77
  236. package/build/src/utils/request.utils.js +0 -175
  237. package/build/src/utils/request.utils.js.map +0 -1
  238. package/build/src/utils/response.utils.d.ts +0 -163
  239. package/build/src/utils/response.utils.js +0 -260
  240. package/build/src/utils/response.utils.js.map +0 -1
  241. package/build/src/utils/string.utils.d.ts +0 -93
  242. package/build/src/utils/string.utils.js +0 -179
  243. package/build/src/utils/string.utils.js.map +0 -1
  244. package/build/src/utils/url.utils.d.ts +0 -141
  245. package/build/src/utils/url.utils.js +0 -317
  246. package/build/src/utils/url.utils.js.map +0 -1
  247. package/build/src/utils/validate.utils.d.ts +0 -177
  248. package/build/src/utils/validate.utils.js +0 -345
  249. package/build/src/utils/validate.utils.js.map +0 -1
@@ -1,260 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.RedirectResponse = exports.NoContentResponse = exports.PaginatedResponse = exports.ErrorResponse = exports.SuccessResponse = void 0;
27
- exports.createSuccessResponse = createSuccessResponse;
28
- exports.createErrorResponse = createErrorResponse;
29
- exports.createFinalErrorResponse = createFinalErrorResponse;
30
- exports.createPaginatedResponse = createPaginatedResponse;
31
- exports.sendResponse = sendResponse;
32
- const context_store_utils_1 = require("./context-store.utils");
33
- const env_utils_1 = require("./env.utils");
34
- const id_utils_1 = require("./id.utils");
35
- /**
36
- * Standard HTTP response wrapper for successful responses.
37
- * Implements the `ApiResponse<T>` interface and sets default values.
38
- *
39
- * @typeParam T - The shape of the data returned in the response.
40
- */
41
- class SuccessResponse {
42
- /**
43
- * Constructs a new success response.
44
- *
45
- * @param {string} message - Optional message to override the default.
46
- * @param {T} [data] - Optional data payload.
47
- */
48
- constructor(message, data) {
49
- var _a;
50
- /** Message describing the result of the operation. */
51
- this.message = 'Success';
52
- /** Whether the response is an error. Always false in success responses. */
53
- this.error = false;
54
- /** The payload returned from the API. */
55
- this.data = null;
56
- /** Timestamp when the response was generated, in ISO format. */
57
- this.timestamp = new Date().toISOString();
58
- /** Unique identifier for this response, useful for request tracing. */
59
- this.requestId = (_a = (0, context_store_utils_1.getRequestId)()) !== null && _a !== void 0 ? _a : (0, id_utils_1.uuid)();
60
- if (message)
61
- this.message = message;
62
- this.data = data !== null && data !== void 0 ? data : null;
63
- }
64
- }
65
- exports.SuccessResponse = SuccessResponse;
66
- /**
67
- * Wrapper for error responses that extends the native `Error` object.
68
- * Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
69
- */
70
- class ErrorResponse extends Error {
71
- /**
72
- * Constructs a new error response.
73
- *
74
- * @param {string} message - The error message to display or log.
75
- * @param {number} [status=500] - Optional HTTP status code (defaults to 500).
76
- */
77
- constructor(message, status = 500) {
78
- var _a;
79
- super(message);
80
- /** Indicates that this is an error. Always true. */
81
- this.error = true;
82
- /** Timestamp when the error occurred, in ISO format. */
83
- this.timestamp = new Date().toISOString();
84
- /** Unique identifier for this error instance. */
85
- this.requestId = (_a = (0, context_store_utils_1.getRequestId)()) !== null && _a !== void 0 ? _a : (0, id_utils_1.uuid)();
86
- this.status = status;
87
- this.name = this.constructor.name;
88
- Error.captureStackTrace(this, this.constructor);
89
- }
90
- }
91
- exports.ErrorResponse = ErrorResponse;
92
- /**
93
- * Response with paginated data extending the standard success response.
94
- * Useful for APIs that return large collections of data.
95
- *
96
- * @typeParam T - The shape of each item in the paginated collection.
97
- */
98
- class PaginatedResponse extends SuccessResponse {
99
- /**
100
- * Constructs a new paginated response.
101
- *
102
- * @param {T[]} items - The current page of items.
103
- * @param {Object} pagination - Pagination information.
104
- * @param {number} pagination.total - Total number of items across all pages.
105
- * @param {number} pagination.page - Current page number (1-based).
106
- * @param {number} pagination.pageSize - Number of items per page.
107
- * @param {string} [message="Success"] - Optional custom message.
108
- */
109
- constructor(items, pagination, message = 'Success') {
110
- super(message, items);
111
- this.total = pagination.total;
112
- this.page = pagination.page;
113
- this.pageSize = pagination.pageSize;
114
- this.totalPages = Math.ceil(this.total / this.pageSize);
115
- this.hasNext = this.page < this.totalPages;
116
- this.hasPrevious = this.page > 1;
117
- }
118
- }
119
- exports.PaginatedResponse = PaginatedResponse;
120
- /**
121
- * Specialized response for operations that don't return data (HTTP 204).
122
- */
123
- class NoContentResponse extends SuccessResponse {
124
- /**
125
- * Constructs a new no-content response.
126
- *
127
- * @param {string} [message="Operation completed successfully"] - Optional custom message.
128
- */
129
- constructor(message = 'Operation completed successfully') {
130
- super(message, null);
131
- }
132
- }
133
- exports.NoContentResponse = NoContentResponse;
134
- /**
135
- * Specialized response for redirects.
136
- */
137
- class RedirectResponse {
138
- /**
139
- * Constructs a new redirect response.
140
- *
141
- * @param {string} url - The URL to redirect to.
142
- * @param {number} [statusCode=302] - HTTP status code for the redirect.
143
- */
144
- constructor(url, statusCode = 302) {
145
- var _a;
146
- /** Whether the response is a redirect */
147
- this.isRedirect = true;
148
- /** Unique identifier for this response */
149
- this.requestId = (_a = (0, context_store_utils_1.getRequestId)()) !== null && _a !== void 0 ? _a : (0, id_utils_1.uuid)();
150
- this.redirectUrl = url;
151
- this.statusCode = statusCode;
152
- }
153
- }
154
- exports.RedirectResponse = RedirectResponse;
155
- /**
156
- * Creates a standard success response.
157
- *
158
- * @typeParam T - The shape of the data returned in the response.
159
- * @param {T} data - The data to include in the response.
160
- * @param {string} [message="Success"] - Optional custom message.
161
- * @returns {SuccessResponse<T>} A properly formatted success response.
162
- */
163
- function createSuccessResponse(data, message = 'Success') {
164
- return new SuccessResponse(message, data);
165
- }
166
- /**
167
- * Creates a standard error response.
168
- *
169
- * @param {string} message - The error message.
170
- * @param {number} [statusCode=500] - HTTP status code for the error.
171
- * @returns {ErrorResponse} A properly formatted error response.
172
- */
173
- function createErrorResponse(message, statusCode = 500) {
174
- return new ErrorResponse(message, statusCode);
175
- }
176
- /**
177
- * Creates a final error response with request information.
178
- *
179
- * @param req - The original request object.
180
- * @param status - The HTTP status code.
181
- * @param message - The error message.
182
- * @param error - The original error object (optional).
183
- * @param options - Additional options for the response (optional).
184
- * @returns A properly formatted final error response.
185
- */
186
- function createFinalErrorResponse(req, status, message, error, options) {
187
- const isDev = env_utils_1.Env.isDev();
188
- const includeDetails = (options === null || options === void 0 ? void 0 : options.includeDetails) && isDev;
189
- const response = {
190
- error: true,
191
- message,
192
- timestamp: new Date().toISOString(),
193
- requestId: (error === null || error === void 0 ? void 0 : error.requestId) || (req === null || req === void 0 ? void 0 : req.id) || (0, id_utils_1.uuid)(),
194
- status,
195
- path: req.originalUrl || req.url
196
- };
197
- // Include stack trace in development mode if requested
198
- if (includeDetails && (error === null || error === void 0 ? void 0 : error.stack)) {
199
- response.stack = error.stack.split('\n').map((line) => line.trim());
200
- }
201
- return response;
202
- }
203
- /**
204
- * Creates a paginated response from array data.
205
- *
206
- * @typeParam T - The shape of each item in the collection.
207
- * @param {T[]} allItems - The complete array of items to paginate.
208
- * @param {number} page - The requested page (1-based).
209
- * @param {number} pageSize - The number of items per page.
210
- * @param {string} [message="Success"] - Optional custom message.
211
- * @returns {PaginatedResponse<T>} A properly formatted paginated response.
212
- */
213
- function createPaginatedResponse(allItems, page, pageSize, message = 'Success') {
214
- // Ensure valid pagination parameters
215
- const validPage = Math.max(1, page);
216
- const validPageSize = Math.max(1, pageSize);
217
- // Calculate slice indices
218
- const startIndex = (validPage - 1) * validPageSize;
219
- const endIndex = startIndex + validPageSize;
220
- // Get current page items
221
- const pageItems = allItems.slice(startIndex, endIndex);
222
- return new PaginatedResponse(pageItems, {
223
- total: allItems.length,
224
- page: validPage,
225
- pageSize: validPageSize
226
- }, message);
227
- }
228
- /**
229
- * Adapter to convert API responses to Express.js response format.
230
- *
231
- * @param {any} res - Express response object.
232
- * @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
233
- */
234
- function sendResponse(res, apiResponse) {
235
- // Handle redirects
236
- if ('redirectUrl' in apiResponse) {
237
- res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);
238
- return;
239
- }
240
- // Handle errors
241
- if ('error' in apiResponse && apiResponse.error) {
242
- const errorResponse = apiResponse;
243
- res.status(errorResponse.status).json({
244
- error: true,
245
- message: errorResponse.message,
246
- timestamp: errorResponse.timestamp,
247
- requestId: errorResponse.requestId
248
- });
249
- return;
250
- }
251
- // Handle success responses
252
- const successResponse = apiResponse;
253
- // No content (204) shouldn't return a body
254
- if (successResponse instanceof NoContentResponse) {
255
- res.status(204).end();
256
- return;
257
- }
258
- res.status(200).json(successResponse);
259
- }
260
- //# sourceMappingURL=response.utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"response.utils.js","sourceRoot":"","sources":["../../../src/utils/response.utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;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AAe,YAAY,iBAAiB,EAAE,CAAC;QACjD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;QACtB,OAAO;IACT,CAAC;IAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AACxC,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies\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 { Request } from 'express';\nimport { ApiErrorResponse, ApiResponse } from '../types/api-response';\nimport { getRequestId } from './context-store.utils';\nimport { Env } from './env.utils';\nimport { uuid } from './id.utils';\n\n/**\n * Standard HTTP response wrapper for successful responses.\n * Implements the `ApiResponse<T>` interface and sets default values.\n *\n * @typeParam T - The shape of the data returned in the response.\n */\nexport class SuccessResponse<T> implements ApiResponse<T> {\n /** Message describing the result of the operation. */\n message: string = 'Success';\n\n /** Whether the response is an error. Always false in success responses. */\n error: boolean = false;\n\n /** The payload returned from the API. */\n data: T | null = null;\n\n /** Timestamp when the response was generated, in ISO format. */\n timestamp: string = new Date().toISOString();\n\n /** Unique identifier for this response, useful for request tracing. */\n requestId: string = getRequestId() ?? uuid();\n\n /**\n * Constructs a new success response.\n *\n * @param {string} message - Optional message to override the default.\n * @param {T} [data] - Optional data payload.\n */\n constructor(message: string, data?: T) {\n if (message) this.message = message;\n this.data = data ?? null;\n }\n}\n\n/**\n * Wrapper for error responses that extends the native `Error` object.\n * Implements `ApiResponse` but omits the `data` field (which should not be present in errors).\n */\nexport class ErrorResponse extends Error implements Omit<ApiResponse<never>, 'data'> {\n /** HTTP status code associated with the error (e.g., 404, 500). */\n status: number;\n\n /** Indicates that this is an error. Always true. */\n error: boolean = true;\n\n /** Timestamp when the error occurred, in ISO format. */\n timestamp: string = new Date().toISOString();\n\n /** Unique identifier for this error instance. */\n requestId: string = getRequestId() ?? uuid();\n\n /**\n * Constructs a new error response.\n *\n * @param {string} message - The error message to display or log.\n * @param {number} [status=500] - Optional HTTP status code (defaults to 500).\n */\n constructor(message: string, status: number = 500) {\n super(message);\n this.status = status;\n this.name = this.constructor.name;\n Error.captureStackTrace(this, this.constructor);\n }\n}\n\n/**\n * Response with paginated data extending the standard success response.\n * Useful for APIs that return large collections of data.\n *\n * @typeParam T - The shape of each item in the paginated collection.\n */\nexport class PaginatedResponse<T> extends SuccessResponse<T[]> {\n /** Total number of items across all pages */\n total: number;\n\n /** Current page number (1-based) */\n page: number;\n\n /** Number of items per page */\n pageSize: number;\n\n /** Total number of pages */\n totalPages: number;\n\n /** Whether there's a next page available */\n hasNext: boolean;\n\n /** Whether there's a previous page available */\n hasPrevious: boolean;\n\n /**\n * Constructs a new paginated response.\n *\n * @param {T[]} items - The current page of items.\n * @param {Object} pagination - Pagination information.\n * @param {number} pagination.total - Total number of items across all pages.\n * @param {number} pagination.page - Current page number (1-based).\n * @param {number} pagination.pageSize - Number of items per page.\n * @param {string} [message=\"Success\"] - Optional custom message.\n */\n constructor(items: T[], pagination: { total: number; page: number; pageSize: number }, message: string = 'Success') {\n super(message, items);\n\n this.total = pagination.total;\n this.page = pagination.page;\n this.pageSize = pagination.pageSize;\n\n this.totalPages = Math.ceil(this.total / this.pageSize);\n this.hasNext = this.page < this.totalPages;\n this.hasPrevious = this.page > 1;\n }\n}\n\n/**\n * Specialized response for operations that don't return data (HTTP 204).\n */\nexport class NoContentResponse extends SuccessResponse<null> {\n /**\n * Constructs a new no-content response.\n *\n * @param {string} [message=\"Operation completed successfully\"] - Optional custom message.\n */\n constructor(message: string = 'Operation completed successfully') {\n super(message, null);\n }\n}\n\n/**\n * Specialized response for redirects.\n */\nexport class RedirectResponse {\n /** The URL to redirect to */\n redirectUrl: string;\n\n /** HTTP status code for the redirect (301, 302, 303, 307, 308) */\n statusCode: number;\n\n /** Whether the response is a redirect */\n isRedirect: boolean = true;\n\n /** Unique identifier for this response */\n requestId: string = getRequestId() ?? uuid();\n\n /**\n * Constructs a new redirect response.\n *\n * @param {string} url - The URL to redirect to.\n * @param {number} [statusCode=302] - HTTP status code for the redirect.\n */\n constructor(url: string, statusCode: number = 302) {\n this.redirectUrl = url;\n this.statusCode = statusCode;\n }\n}\n\n/**\n * Creates a standard success response.\n *\n * @typeParam T - The shape of the data returned in the response.\n * @param {T} data - The data to include in the response.\n * @param {string} [message=\"Success\"] - Optional custom message.\n * @returns {SuccessResponse<T>} A properly formatted success response.\n */\nexport function createSuccessResponse<T>(data: T, message: string = 'Success'): SuccessResponse<T> {\n return new SuccessResponse<T>(message, data);\n}\n\n/**\n * Creates a standard error response.\n *\n * @param {string} message - The error message.\n * @param {number} [statusCode=500] - HTTP status code for the error.\n * @returns {ErrorResponse} A properly formatted error response.\n */\nexport function createErrorResponse(message: string, statusCode: number = 500): ErrorResponse {\n return new ErrorResponse(message, statusCode);\n}\n\n/**\n * Creates a final error response with request information.\n *\n * @param req - The original request object.\n * @param status - The HTTP status code.\n * @param message - The error message.\n * @param error - The original error object (optional).\n * @param options - Additional options for the response (optional).\n * @returns A properly formatted final error response.\n */\nexport function createFinalErrorResponse(\n req: Request,\n status: number,\n message: string,\n error?: any,\n options?: { includeDetails?: boolean }\n) {\n const isDev = Env.isDev();\n const includeDetails = options?.includeDetails && isDev;\n\n const response: ApiErrorResponse = {\n error: true,\n message,\n timestamp: new Date().toISOString(),\n requestId: error?.requestId || req?.id || uuid(),\n status,\n path: req.originalUrl || req.url\n };\n\n // Include stack trace in development mode if requested\n if (includeDetails && error?.stack) {\n response.stack = error.stack.split('\\n').map((line: string) => line.trim());\n }\n\n return response;\n}\n\n/**\n * Creates a paginated response from array data.\n *\n * @typeParam T - The shape of each item in the collection.\n * @param {T[]} allItems - The complete array of items to paginate.\n * @param {number} page - The requested page (1-based).\n * @param {number} pageSize - The number of items per page.\n * @param {string} [message=\"Success\"] - Optional custom message.\n * @returns {PaginatedResponse<T>} A properly formatted paginated response.\n */\nexport function createPaginatedResponse<T>(\n allItems: T[],\n page: number,\n pageSize: number,\n message: string = 'Success'\n): PaginatedResponse<T> {\n // Ensure valid pagination parameters\n const validPage = Math.max(1, page);\n const validPageSize = Math.max(1, pageSize);\n\n // Calculate slice indices\n const startIndex = (validPage - 1) * validPageSize;\n const endIndex = startIndex + validPageSize;\n\n // Get current page items\n const pageItems = allItems.slice(startIndex, endIndex);\n\n return new PaginatedResponse<T>(\n pageItems,\n {\n total: allItems.length,\n page: validPage,\n pageSize: validPageSize\n },\n message\n );\n}\n\n/**\n * Adapter to convert API responses to Express.js response format.\n *\n * @param {any} res - Express response object.\n * @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.\n */\nexport function sendResponse(res: any, apiResponse: SuccessResponse<any> | ErrorResponse | RedirectResponse): void {\n // Handle redirects\n if ('redirectUrl' in apiResponse) {\n res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);\n return;\n }\n\n // Handle errors\n if ('error' in apiResponse && apiResponse.error) {\n const errorResponse = apiResponse as ErrorResponse;\n res.status(errorResponse.status).json({\n error: true,\n message: errorResponse.message,\n timestamp: errorResponse.timestamp,\n requestId: errorResponse.requestId\n });\n return;\n }\n\n // Handle success responses\n const successResponse = apiResponse as SuccessResponse<any>;\n\n // No content (204) shouldn't return a body\n if (successResponse instanceof NoContentResponse) {\n res.status(204).end();\n return;\n }\n\n res.status(200).json(successResponse);\n}\n"]}
@@ -1,93 +0,0 @@
1
- /**
2
- * Capitalizes the first character of a string.
3
- *
4
- * @param {string} str - The input string.
5
- * @returns {string} The string with the first character in uppercase.
6
- */
7
- export declare function capitalize(str: string): string;
8
- /**
9
- * Converts a string to kebab-case (e.g., "FooBar test" → "foo-bar-test").
10
- *
11
- * @param {string} str - The input string.
12
- * @returns {string} The kebab-cased string.
13
- */
14
- export declare function toKebabCase(str: string): string;
15
- /**
16
- * Converts a kebab-case or snake_case string to camelCase.
17
- *
18
- * @param {string} str - The input string.
19
- * @returns {string} The camelCased string.
20
- */
21
- export declare function toCamelCase(str: string): string;
22
- /**
23
- * Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
24
- *
25
- * @param {string} str - The input string.
26
- * @returns {string} The slugified string.
27
- */
28
- export declare function slugify(str: string): string;
29
- /**
30
- * Truncates a string to a specific length, appending '...' if truncated.
31
- *
32
- * @param {string} str - The input string.
33
- * @param {number} len - The maximum length.
34
- * @returns {string} The truncated string.
35
- */
36
- export declare function truncate(str: string, len: number): string;
37
- /**
38
- * Converts a string to PascalCase (e.g., "foo-bar" → "FooBar").
39
- *
40
- * @param {string} str - The input string.
41
- * @returns {string} The PascalCased string.
42
- */
43
- export declare function toPascalCase(str: string): string;
44
- /**
45
- * Converts a string to snake_case (e.g., "FooBar test" → "foo_bar_test").
46
- *
47
- * @param {string} str - The input string.
48
- * @returns {string} The snake_cased string.
49
- */
50
- export declare function toSnakeCase(str: string): string;
51
- /**
52
- * Masks a string by replacing characters with a mask character.
53
- * Useful for hiding sensitive information like credit cards or passwords.
54
- *
55
- * @param {string} str - The string to mask.
56
- * @param {number} [visibleStart=0] - Number of characters to show at start.
57
- * @param {number} [visibleEnd=0] - Number of characters to show at end.
58
- * @param {string} [maskChar="*"] - Character to use for masking.
59
- * @returns {string} The masked string.
60
- */
61
- export declare function mask(str: string, visibleStart?: number, visibleEnd?: number, maskChar?: string): string;
62
- /**
63
- * Removes all HTML tags from a string.
64
- *
65
- * @param {string} str - The HTML string to process.
66
- * @returns {string} The string with HTML tags removed.
67
- */
68
- export declare function stripHtml(str: string): string;
69
- /**
70
- * Performs case-insensitive string comparison.
71
- *
72
- * @param {string} a - First string.
73
- * @param {string} b - Second string.
74
- * @returns {boolean} True if the strings are equal ignoring case.
75
- */
76
- export declare function equalsIgnoreCase(a: string, b: string): boolean;
77
- /**
78
- * Reverses a string.
79
- *
80
- * @param {string} str - The string to reverse.
81
- * @returns {string} The reversed string.
82
- */
83
- export declare function reverse(str: string): string;
84
- /**
85
- * Counts occurrences of a substring within a string.
86
- *
87
- * @param {string} str - The source string.
88
- * @param {string} substring - The substring to count.
89
- * @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
90
- * @returns {number} Number of occurrences.
91
- */
92
- export declare function countOccurrences(str: string, substring: string, caseSensitive?: boolean): number;
93
- //# sourceMappingURL=string.utils.d.ts.map
@@ -1,179 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.capitalize = capitalize;
27
- exports.toKebabCase = toKebabCase;
28
- exports.toCamelCase = toCamelCase;
29
- exports.slugify = slugify;
30
- exports.truncate = truncate;
31
- exports.toPascalCase = toPascalCase;
32
- exports.toSnakeCase = toSnakeCase;
33
- exports.mask = mask;
34
- exports.stripHtml = stripHtml;
35
- exports.equalsIgnoreCase = equalsIgnoreCase;
36
- exports.reverse = reverse;
37
- exports.countOccurrences = countOccurrences;
38
- /**
39
- * Capitalizes the first character of a string.
40
- *
41
- * @param {string} str - The input string.
42
- * @returns {string} The string with the first character in uppercase.
43
- */
44
- function capitalize(str) {
45
- return str.length === 0 ? '' : str.charAt(0).toUpperCase() + str.slice(1);
46
- }
47
- /**
48
- * Converts a string to kebab-case (e.g., "FooBar test" → "foo-bar-test").
49
- *
50
- * @param {string} str - The input string.
51
- * @returns {string} The kebab-cased string.
52
- */
53
- function toKebabCase(str) {
54
- return str
55
- .replace(/([a-z])([A-Z])/g, '$1-$2') // camelCase -> camel-Case
56
- .replace(/\s+/g, '-') // spaces to dash
57
- .replace(/_+/g, '-') // underscores to dash
58
- .toLowerCase();
59
- }
60
- /**
61
- * Converts a kebab-case or snake_case string to camelCase.
62
- *
63
- * @param {string} str - The input string.
64
- * @returns {string} The camelCased string.
65
- */
66
- function toCamelCase(str) {
67
- return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
68
- }
69
- /**
70
- * Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).
71
- *
72
- * @param {string} str - The input string.
73
- * @returns {string} The slugified string.
74
- */
75
- function slugify(str) {
76
- return str
77
- .toLowerCase()
78
- .replace(/[^\w\s-]/g, '') // remove non-word
79
- .replace(/\s+/g, '-') // spaces to dash
80
- .replace(/-+/g, '-') // multiple dashes to one
81
- .replace(/^-+|-+$/g, ''); // trim leading/trailing dashes
82
- }
83
- /**
84
- * Truncates a string to a specific length, appending '...' if truncated.
85
- *
86
- * @param {string} str - The input string.
87
- * @param {number} len - The maximum length.
88
- * @returns {string} The truncated string.
89
- */
90
- function truncate(str, len) {
91
- return str.length > len ? str.slice(0, len) + '...' : str;
92
- }
93
- /**
94
- * Converts a string to PascalCase (e.g., "foo-bar" → "FooBar").
95
- *
96
- * @param {string} str - The input string.
97
- * @returns {string} The PascalCased string.
98
- */
99
- function toPascalCase(str) {
100
- return capitalize(toCamelCase(str));
101
- }
102
- /**
103
- * Converts a string to snake_case (e.g., "FooBar test" → "foo_bar_test").
104
- *
105
- * @param {string} str - The input string.
106
- * @returns {string} The snake_cased string.
107
- */
108
- function toSnakeCase(str) {
109
- return str
110
- .trim()
111
- .replace(/([a-z])([A-Z])/g, '$1_$2') // camelCase -> camel_Case
112
- .replace(/\s+/g, '_') // spaces to underscore
113
- .replace(/-+/g, '_') // dashes to underscore
114
- .toLowerCase();
115
- }
116
- /**
117
- * Masks a string by replacing characters with a mask character.
118
- * Useful for hiding sensitive information like credit cards or passwords.
119
- *
120
- * @param {string} str - The string to mask.
121
- * @param {number} [visibleStart=0] - Number of characters to show at start.
122
- * @param {number} [visibleEnd=0] - Number of characters to show at end.
123
- * @param {string} [maskChar="*"] - Character to use for masking.
124
- * @returns {string} The masked string.
125
- */
126
- function mask(str, visibleStart = 0, visibleEnd = 0, maskChar = '*') {
127
- if (!str || str.length === 0)
128
- return '';
129
- if (visibleStart >= str.length)
130
- return str;
131
- const start = str.slice(0, visibleStart);
132
- const end = visibleEnd > 0 ? str.slice(-visibleEnd) : '';
133
- const masked = maskChar.repeat(Math.max(0, str.length - visibleStart - visibleEnd));
134
- return start + masked + end;
135
- }
136
- /**
137
- * Removes all HTML tags from a string.
138
- *
139
- * @param {string} str - The HTML string to process.
140
- * @returns {string} The string with HTML tags removed.
141
- */
142
- function stripHtml(str) {
143
- return str.replace(/<[^>]*>/g, '');
144
- }
145
- /**
146
- * Performs case-insensitive string comparison.
147
- *
148
- * @param {string} a - First string.
149
- * @param {string} b - Second string.
150
- * @returns {boolean} True if the strings are equal ignoring case.
151
- */
152
- function equalsIgnoreCase(a, b) {
153
- return a.toLowerCase() === b.toLowerCase();
154
- }
155
- /**
156
- * Reverses a string.
157
- *
158
- * @param {string} str - The string to reverse.
159
- * @returns {string} The reversed string.
160
- */
161
- function reverse(str) {
162
- return str.split('').reverse().join('');
163
- }
164
- /**
165
- * Counts occurrences of a substring within a string.
166
- *
167
- * @param {string} str - The source string.
168
- * @param {string} substring - The substring to count.
169
- * @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.
170
- * @returns {number} Number of occurrences.
171
- */
172
- function countOccurrences(str, substring, caseSensitive = true) {
173
- if (!caseSensitive) {
174
- str = str.toLowerCase();
175
- substring = substring.toLowerCase();
176
- }
177
- return str.split(substring).length - 1;
178
- }
179
- //# sourceMappingURL=string.utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"string.utils.js","sourceRoot":"","sources":["../../../src/utils/string.utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;AAQH,gCAEC;AAQD,kCAMC;AAQD,kCAEC;AAQD,0BAOC;AASD,4BAEC;AAQD,oCAEC;AAQD,kCAOC;AAYD,oBASC;AAQD,8BAEC;AASD,4CAEC;AAQD,0BAEC;AAUD,4CAOC;AAxJD;;;;;GAKG;AACH,SAAgB,UAAU,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,C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G;AACH,SAAgB,OAAO,CAAC,GAAW;IACjC,OAAO,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,gBAAgB,CAAC,GAAW,EAAE,SAAiB,EAAE,gBAAyB,IAAI;IAC5F,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,GAAG,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC;QACxB,SAAS,GAAG,SAAS,CAAC,WAAW,EAAE,CAAC;IACtC,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;AACzC,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies\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\n/**\n * Capitalizes the first character of a string.\n *\n * @param {string} str - The input string.\n * @returns {string} The string with the first character in uppercase.\n */\nexport function capitalize(str: string): string {\n return str.length === 0 ? '' : str.charAt(0).toUpperCase() + str.slice(1);\n}\n\n/**\n * Converts a string to kebab-case (e.g., \"FooBar test\" → \"foo-bar-test\").\n *\n * @param {string} str - The input string.\n * @returns {string} The kebab-cased string.\n */\nexport function toKebabCase(str: string): string {\n return str\n .replace(/([a-z])([A-Z])/g, '$1-$2') // camelCase -> camel-Case\n .replace(/\\s+/g, '-') // spaces to dash\n .replace(/_+/g, '-') // underscores to dash\n .toLowerCase();\n}\n\n/**\n * Converts a kebab-case or snake_case string to camelCase.\n *\n * @param {string} str - The input string.\n * @returns {string} The camelCased string.\n */\nexport function toCamelCase(str: string): string {\n return str.replace(/[-_](.)/g, (_, c: string) => c.toUpperCase());\n}\n\n/**\n * Converts a string to a URL-friendly slug (lowercase, dashes, alphanumeric).\n *\n * @param {string} str - The input string.\n * @returns {string} The slugified string.\n */\nexport function slugify(str: string): string {\n return str\n .toLowerCase()\n .replace(/[^\\w\\s-]/g, '') // remove non-word\n .replace(/\\s+/g, '-') // spaces to dash\n .replace(/-+/g, '-') // multiple dashes to one\n .replace(/^-+|-+$/g, ''); // trim leading/trailing dashes\n}\n\n/**\n * Truncates a string to a specific length, appending '...' if truncated.\n *\n * @param {string} str - The input string.\n * @param {number} len - The maximum length.\n * @returns {string} The truncated string.\n */\nexport function truncate(str: string, len: number): string {\n return str.length > len ? str.slice(0, len) + '...' : str;\n}\n\n/**\n * Converts a string to PascalCase (e.g., \"foo-bar\" → \"FooBar\").\n *\n * @param {string} str - The input string.\n * @returns {string} The PascalCased string.\n */\nexport function toPascalCase(str: string): string {\n return capitalize(toCamelCase(str));\n}\n\n/**\n * Converts a string to snake_case (e.g., \"FooBar test\" → \"foo_bar_test\").\n *\n * @param {string} str - The input string.\n * @returns {string} The snake_cased string.\n */\nexport function toSnakeCase(str: string): string {\n return str\n .trim()\n .replace(/([a-z])([A-Z])/g, '$1_$2') // camelCase -> camel_Case\n .replace(/\\s+/g, '_') // spaces to underscore\n .replace(/-+/g, '_') // dashes to underscore\n .toLowerCase();\n}\n\n/**\n * Masks a string by replacing characters with a mask character.\n * Useful for hiding sensitive information like credit cards or passwords.\n *\n * @param {string} str - The string to mask.\n * @param {number} [visibleStart=0] - Number of characters to show at start.\n * @param {number} [visibleEnd=0] - Number of characters to show at end.\n * @param {string} [maskChar=\"*\"] - Character to use for masking.\n * @returns {string} The masked string.\n */\nexport function mask(str: string, visibleStart: number = 0, visibleEnd: number = 0, maskChar: string = '*'): string {\n if (!str || str.length === 0) return '';\n if (visibleStart >= str.length) return str;\n\n const start = str.slice(0, visibleStart);\n const end = visibleEnd > 0 ? str.slice(-visibleEnd) : '';\n const masked = maskChar.repeat(Math.max(0, str.length - visibleStart - visibleEnd));\n\n return start + masked + end;\n}\n\n/**\n * Removes all HTML tags from a string.\n *\n * @param {string} str - The HTML string to process.\n * @returns {string} The string with HTML tags removed.\n */\nexport function stripHtml(str: string): string {\n return str.replace(/<[^>]*>/g, '');\n}\n\n/**\n * Performs case-insensitive string comparison.\n *\n * @param {string} a - First string.\n * @param {string} b - Second string.\n * @returns {boolean} True if the strings are equal ignoring case.\n */\nexport function equalsIgnoreCase(a: string, b: string): boolean {\n return a.toLowerCase() === b.toLowerCase();\n}\n\n/**\n * Reverses a string.\n *\n * @param {string} str - The string to reverse.\n * @returns {string} The reversed string.\n */\nexport function reverse(str: string): string {\n return str.split('').reverse().join('');\n}\n\n/**\n * Counts occurrences of a substring within a string.\n *\n * @param {string} str - The source string.\n * @param {string} substring - The substring to count.\n * @param {boolean} [caseSensitive=true] - Whether to perform case-sensitive counting.\n * @returns {number} Number of occurrences.\n */\nexport function countOccurrences(str: string, substring: string, caseSensitive: boolean = true): number {\n if (!caseSensitive) {\n str = str.toLowerCase();\n substring = substring.toLowerCase();\n }\n\n return str.split(substring).length - 1;\n}\n"]}