@catbee/utils 0.0.8-rc.3 → 1.0.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 (129) hide show
  1. package/README.md +3 -2
  2. package/build/index.cjs +6912 -0
  3. package/build/index.d.ts +5284 -0
  4. package/build/index.mjs +6635 -0
  5. package/package.json +13 -292
  6. package/build/cjs/config.d.ts +0 -121
  7. package/build/cjs/config.js +0 -137
  8. package/build/cjs/index.d.ts +0 -26
  9. package/build/cjs/index.js +0 -68
  10. package/build/cjs/servers/server.builder.d.ts +0 -507
  11. package/build/cjs/servers/server.builder.js +0 -658
  12. package/build/cjs/servers/server.d.ts +0 -255
  13. package/build/cjs/servers/server.js +0 -970
  14. package/build/cjs/types/api-response.d.ts +0 -151
  15. package/build/cjs/types/api-response.js +0 -36
  16. package/build/cjs/types/index.d.ts +0 -124
  17. package/build/cjs/types/index.js +0 -25
  18. package/build/cjs/types/server.d.ts +0 -267
  19. package/build/cjs/types/server.js +0 -25
  20. package/build/cjs/utils/array.utils.d.ts +0 -167
  21. package/build/cjs/utils/array.utils.js +0 -363
  22. package/build/cjs/utils/async.utils.d.ts +0 -264
  23. package/build/cjs/utils/async.utils.js +0 -640
  24. package/build/cjs/utils/cache.utils.d.ts +0 -152
  25. package/build/cjs/utils/cache.utils.js +0 -298
  26. package/build/cjs/utils/context-store.utils.d.ts +0 -188
  27. package/build/cjs/utils/context-store.utils.js +0 -297
  28. package/build/cjs/utils/crypto.utils.d.ts +0 -159
  29. package/build/cjs/utils/crypto.utils.js +0 -295
  30. package/build/cjs/utils/date.utils.d.ts +0 -158
  31. package/build/cjs/utils/date.utils.js +0 -395
  32. package/build/cjs/utils/decorators.utils.d.ts +0 -511
  33. package/build/cjs/utils/decorators.utils.js +0 -1035
  34. package/build/cjs/utils/dir.utils.d.ts +0 -195
  35. package/build/cjs/utils/dir.utils.js +0 -500
  36. package/build/cjs/utils/env.utils.d.ts +0 -376
  37. package/build/cjs/utils/env.utils.js +0 -786
  38. package/build/cjs/utils/exception.utils.d.ts +0 -229
  39. package/build/cjs/utils/exception.utils.js +0 -408
  40. package/build/cjs/utils/fs.utils.d.ts +0 -163
  41. package/build/cjs/utils/fs.utils.js +0 -371
  42. package/build/cjs/utils/http-status-codes.d.ts +0 -265
  43. package/build/cjs/utils/http-status-codes.js +0 -297
  44. package/build/cjs/utils/id.utils.d.ts +0 -35
  45. package/build/cjs/utils/id.utils.js +0 -90
  46. package/build/cjs/utils/logger.utils.d.ts +0 -159
  47. package/build/cjs/utils/logger.utils.js +0 -350
  48. package/build/cjs/utils/middleware.utils.d.ts +0 -99
  49. package/build/cjs/utils/middleware.utils.js +0 -243
  50. package/build/cjs/utils/obj.utils.d.ts +0 -123
  51. package/build/cjs/utils/obj.utils.js +0 -428
  52. package/build/cjs/utils/performance.utils.d.ts +0 -135
  53. package/build/cjs/utils/performance.utils.js +0 -280
  54. package/build/cjs/utils/request.utils.d.ts +0 -85
  55. package/build/cjs/utils/request.utils.js +0 -199
  56. package/build/cjs/utils/response.utils.d.ts +0 -162
  57. package/build/cjs/utils/response.utils.js +0 -274
  58. package/build/cjs/utils/stream.utils.d.ts +0 -87
  59. package/build/cjs/utils/stream.utils.js +0 -217
  60. package/build/cjs/utils/string.utils.d.ts +0 -92
  61. package/build/cjs/utils/string.utils.js +0 -178
  62. package/build/cjs/utils/type.utils.d.ts +0 -89
  63. package/build/cjs/utils/type.utils.js +0 -195
  64. package/build/cjs/utils/url.utils.d.ts +0 -140
  65. package/build/cjs/utils/url.utils.js +0 -316
  66. package/build/cjs/utils/validate.utils.d.ts +0 -176
  67. package/build/cjs/utils/validate.utils.js +0 -344
  68. package/build/esm/config.d.ts +0 -121
  69. package/build/esm/config.js +0 -132
  70. package/build/esm/index.d.ts +0 -26
  71. package/build/esm/index.js +0 -49
  72. package/build/esm/servers/server.builder.d.ts +0 -507
  73. package/build/esm/servers/server.builder.js +0 -654
  74. package/build/esm/servers/server.d.ts +0 -255
  75. package/build/esm/servers/server.js +0 -963
  76. package/build/esm/types/api-response.d.ts +0 -151
  77. package/build/esm/types/api-response.js +0 -33
  78. package/build/esm/types/index.d.ts +0 -124
  79. package/build/esm/types/index.js +0 -24
  80. package/build/esm/types/server.d.ts +0 -267
  81. package/build/esm/types/server.js +0 -24
  82. package/build/esm/utils/array.utils.d.ts +0 -167
  83. package/build/esm/utils/array.utils.js +0 -344
  84. package/build/esm/utils/async.utils.d.ts +0 -264
  85. package/build/esm/utils/async.utils.js +0 -619
  86. package/build/esm/utils/cache.utils.d.ts +0 -152
  87. package/build/esm/utils/cache.utils.js +0 -294
  88. package/build/esm/utils/context-store.utils.d.ts +0 -188
  89. package/build/esm/utils/context-store.utils.js +0 -290
  90. package/build/esm/utils/crypto.utils.d.ts +0 -159
  91. package/build/esm/utils/crypto.utils.js +0 -278
  92. package/build/esm/utils/date.utils.d.ts +0 -158
  93. package/build/esm/utils/date.utils.js +0 -383
  94. package/build/esm/utils/decorators.utils.d.ts +0 -511
  95. package/build/esm/utils/decorators.utils.js +0 -1013
  96. package/build/esm/utils/dir.utils.d.ts +0 -195
  97. package/build/esm/utils/dir.utils.js +0 -476
  98. package/build/esm/utils/env.utils.d.ts +0 -376
  99. package/build/esm/utils/env.utils.js +0 -782
  100. package/build/esm/utils/exception.utils.d.ts +0 -229
  101. package/build/esm/utils/exception.utils.js +0 -382
  102. package/build/esm/utils/fs.utils.d.ts +0 -163
  103. package/build/esm/utils/fs.utils.js +0 -348
  104. package/build/esm/utils/http-status-codes.d.ts +0 -265
  105. package/build/esm/utils/http-status-codes.js +0 -294
  106. package/build/esm/utils/id.utils.d.ts +0 -35
  107. package/build/esm/utils/id.utils.js +0 -83
  108. package/build/esm/utils/logger.utils.d.ts +0 -159
  109. package/build/esm/utils/logger.utils.js +0 -304
  110. package/build/esm/utils/middleware.utils.d.ts +0 -99
  111. package/build/esm/utils/middleware.utils.js +0 -235
  112. package/build/esm/utils/obj.utils.d.ts +0 -123
  113. package/build/esm/utils/obj.utils.js +0 -412
  114. package/build/esm/utils/performance.utils.d.ts +0 -135
  115. package/build/esm/utils/performance.utils.js +0 -273
  116. package/build/esm/utils/request.utils.d.ts +0 -85
  117. package/build/esm/utils/request.utils.js +0 -190
  118. package/build/esm/utils/response.utils.d.ts +0 -162
  119. package/build/esm/utils/response.utils.js +0 -261
  120. package/build/esm/utils/stream.utils.d.ts +0 -87
  121. package/build/esm/utils/stream.utils.js +0 -209
  122. package/build/esm/utils/string.utils.d.ts +0 -92
  123. package/build/esm/utils/string.utils.js +0 -164
  124. package/build/esm/utils/type.utils.d.ts +0 -89
  125. package/build/esm/utils/type.utils.js +0 -186
  126. package/build/esm/utils/url.utils.d.ts +0 -140
  127. package/build/esm/utils/url.utils.js +0 -303
  128. package/build/esm/utils/validate.utils.d.ts +0 -176
  129. package/build/esm/utils/validate.utils.js +0 -319
@@ -1,395 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
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.formatDate = formatDate;
27
- exports.formatRelativeTime = formatRelativeTime;
28
- exports.parseDate = parseDate;
29
- exports.dateDiff = dateDiff;
30
- exports.addToDate = addToDate;
31
- exports.startOf = startOf;
32
- exports.endOf = endOf;
33
- exports.isBetween = isBetween;
34
- exports.isLeapYear = isLeapYear;
35
- exports.daysInMonth = daysInMonth;
36
- /**
37
- * Format a date according to the specified format pattern.
38
- *
39
- * @param date - Date to format
40
- * @param options - Formatting options
41
- * @returns Formatted date string
42
- *
43
- * @example
44
- * ```typescript
45
- * // Format as ISO date
46
- * formatDate(new Date(), { format: 'yyyy-MM-dd' }); // '2023-05-15'
47
- *
48
- * // Format with time
49
- * formatDate(new Date(), { format: 'yyyy-MM-dd HH:mm:ss' }); // '2023-05-15 14:30:22'
50
- *
51
- * // Format with locale
52
- * formatDate(new Date(), { format: 'PPPP', locale: 'fr-FR' }); // 'lundi 15 mai 2023'
53
- * ```
54
- */
55
- function formatDate(date, options = {}) {
56
- const { format = 'yyyy-MM-dd', locale, timeZone } = options;
57
- const d = date instanceof Date ? date : new Date(date);
58
- // Custom formatter for common patterns
59
- if (format === 'yyyy-MM-dd') {
60
- const yyyy = d.getFullYear();
61
- const mm = String(d.getMonth() + 1).padStart(2, '0');
62
- const dd = String(d.getDate()).padStart(2, '0');
63
- return `${yyyy}-${mm}-${dd}`;
64
- }
65
- if (format === 'yyyy-MM-dd HH:mm:ss') {
66
- const yyyy = d.getFullYear();
67
- const mm = String(d.getMonth() + 1).padStart(2, '0');
68
- const dd = String(d.getDate()).padStart(2, '0');
69
- const HH = String(d.getHours()).padStart(2, '0');
70
- const MM = String(d.getMinutes()).padStart(2, '0');
71
- const SS = String(d.getSeconds()).padStart(2, '0');
72
- return `${yyyy}-${mm}-${dd} ${HH}:${MM}:${SS}`;
73
- }
74
- if (format === 'relative') {
75
- return formatRelativeTime(date);
76
- }
77
- // Fallback to Intl.DateTimeFormat for other formats
78
- return new Intl.DateTimeFormat(locale, {
79
- timeZone,
80
- year: format.includes('yyyy') || format.includes('y') ? 'numeric' : undefined,
81
- month: format.includes('MM') || format.includes('M') ? 'numeric' : undefined,
82
- day: format.includes('dd') || format.includes('d') ? 'numeric' : undefined,
83
- hour: format.includes('HH') || format.includes('H') ? 'numeric' : undefined,
84
- minute: format.includes('mm') || format.includes('m') ? 'numeric' : undefined,
85
- second: format.includes('ss') || format.includes('s') ? 'numeric' : undefined,
86
- hour12: !format.includes('HH') && !format.includes('H')
87
- }).format(d);
88
- }
89
- /**
90
- * Format a date as relative time (e.g., "5 minutes ago", "in 3 days").
91
- *
92
- * @param date - Date to format
93
- * @param now - Reference date (default: current time)
94
- * @param locale - Locale to use for formatting
95
- * @returns Formatted relative time string
96
- */
97
- function formatRelativeTime(date, now = new Date(), locale) {
98
- const d1 = date instanceof Date ? date : new Date(date);
99
- const d2 = now instanceof Date ? now : new Date(now);
100
- const diffMs = d1.getTime() - d2.getTime();
101
- const diffSecs = Math.round(diffMs / 1000);
102
- const diffMins = Math.round(diffSecs / 60);
103
- const diffHours = Math.round(diffMins / 60);
104
- const diffDays = Math.round(diffHours / 24);
105
- const diffMonths = Math.round(diffDays / 30);
106
- const diffYears = Math.round(diffDays / 365);
107
- const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });
108
- if (Math.abs(diffSecs) < 60)
109
- return formatter.format(diffSecs, 'second');
110
- if (Math.abs(diffMins) < 60)
111
- return formatter.format(diffMins, 'minute');
112
- if (Math.abs(diffHours) < 24)
113
- return formatter.format(diffHours, 'hour');
114
- if (Math.abs(diffDays) < 30)
115
- return formatter.format(diffDays, 'day');
116
- if (Math.abs(diffMonths) < 12)
117
- return formatter.format(diffMonths, 'month');
118
- return formatter.format(diffYears, 'year');
119
- }
120
- /**
121
- * Parse a date string or timestamp into a Date object.
122
- *
123
- * @param input - Date string or timestamp to parse
124
- * @param fallback - Fallback date if parsing fails
125
- * @returns Parsed Date object or fallback
126
- *
127
- * @example
128
- * ```typescript
129
- * parseDate('2023-05-15'); // Date object for May 15, 2023
130
- * parseDate('invalid', new Date()); // Returns current date as fallback
131
- * ```
132
- */
133
- function parseDate(input, fallback) {
134
- if (typeof input === 'number') {
135
- return new Date(input);
136
- }
137
- try {
138
- const date = new Date(input);
139
- // Check if date is valid
140
- if (isNaN(date.getTime())) {
141
- return fallback || null;
142
- }
143
- return date;
144
- }
145
- catch {
146
- return fallback || null;
147
- }
148
- }
149
- /**
150
- * Calculate the difference between two dates in the specified unit.
151
- *
152
- * @param date1 - First date
153
- * @param date2 - Second date (default: current time)
154
- * @param unit - Unit of time for the difference
155
- * @returns Difference in the specified unit
156
- *
157
- * @example
158
- * ```typescript
159
- * // Get difference in days
160
- * dateDiff(new Date('2023-05-15'), new Date('2023-05-10'), 'days'); // 5
161
- *
162
- * // Get difference in hours
163
- * dateDiff(new Date('2023-05-15T10:00:00'), new Date('2023-05-15T06:00:00'), 'hours'); // 4
164
- * ```
165
- */
166
- function dateDiff(date1, date2 = new Date(), unit = 'days') {
167
- const d1 = date1 instanceof Date ? date1 : new Date(date1);
168
- const d2 = date2 instanceof Date ? date2 : new Date(date2);
169
- const diffMs = d1.getTime() - d2.getTime();
170
- switch (unit) {
171
- case 'milliseconds':
172
- return diffMs;
173
- case 'seconds':
174
- return diffMs / 1000;
175
- case 'minutes':
176
- return diffMs / (1000 * 60);
177
- case 'hours':
178
- return diffMs / (1000 * 60 * 60);
179
- case 'days':
180
- return diffMs / (1000 * 60 * 60 * 24);
181
- case 'months':
182
- return (d1.getFullYear() - d2.getFullYear()) * 12 + d1.getMonth() - d2.getMonth();
183
- case 'years':
184
- return d1.getFullYear() - d2.getFullYear();
185
- default:
186
- throw new Error(`Unsupported unit: ${unit}`);
187
- }
188
- }
189
- /**
190
- * Add a specified amount of time to a date.
191
- *
192
- * @param date - Base date
193
- * @param amount - Amount to add (can be negative)
194
- * @param unit - Unit of time to add
195
- * @returns New date with the addition
196
- *
197
- * @example
198
- * ```typescript
199
- * // Add 5 days
200
- * addToDate(new Date('2023-05-15'), 5, 'days'); // Date for May 20, 2023
201
- *
202
- * // Subtract 2 hours
203
- * addToDate(new Date('2023-05-15T10:00:00'), -2, 'hours'); // Date for May 15, 2023 08:00:00
204
- * ```
205
- */
206
- function addToDate(date, amount, unit) {
207
- const d = date instanceof Date ? new Date(date) : new Date(date);
208
- switch (unit) {
209
- case 'milliseconds':
210
- d.setMilliseconds(d.getMilliseconds() + amount);
211
- break;
212
- case 'seconds':
213
- d.setSeconds(d.getSeconds() + amount);
214
- break;
215
- case 'minutes':
216
- d.setMinutes(d.getMinutes() + amount);
217
- break;
218
- case 'hours':
219
- d.setHours(d.getHours() + amount);
220
- break;
221
- case 'days':
222
- d.setDate(d.getDate() + amount);
223
- break;
224
- case 'months': {
225
- const origDate = d.getDate();
226
- const origMonth = d.getMonth();
227
- d.setDate(1); // Prevent overflow
228
- d.setMonth(origMonth + amount);
229
- const lastDay = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
230
- d.setDate(Math.min(origDate, lastDay));
231
- break;
232
- }
233
- case 'years':
234
- d.setFullYear(d.getFullYear() + amount);
235
- break;
236
- default:
237
- throw new Error(`Unsupported unit: ${unit}`);
238
- }
239
- return d;
240
- }
241
- /**
242
- * Get the start of a time period containing the specified date.
243
- *
244
- * @param date - Date to get the start from
245
- * @param unit - Time unit
246
- * @returns Date object representing the start of the time unit
247
- *
248
- * @example
249
- * ```typescript
250
- * // Get start of day (midnight)
251
- * startOf(new Date('2023-05-15T14:30:00'), 'day'); // Date for May 15, 2023 00:00:00
252
- *
253
- * // Get start of month
254
- * startOf(new Date('2023-05-15'), 'month'); // Date for May 1, 2023
255
- * ```
256
- */
257
- function startOf(date, unit) {
258
- const d = date instanceof Date ? new Date(date) : new Date(date);
259
- switch (unit) {
260
- case 'second':
261
- d.setMilliseconds(0);
262
- break;
263
- case 'minute':
264
- d.setSeconds(0, 0);
265
- break;
266
- case 'hour':
267
- d.setMinutes(0, 0, 0);
268
- break;
269
- case 'day':
270
- d.setHours(0, 0, 0, 0);
271
- break;
272
- case 'week':
273
- d.setHours(0, 0, 0, 0);
274
- d.setDate(d.getDate() - d.getDay());
275
- break;
276
- case 'month':
277
- d.setDate(1);
278
- d.setHours(0, 0, 0, 0);
279
- break;
280
- case 'quarter':
281
- d.setMonth(Math.floor(d.getMonth() / 3) * 3, 1);
282
- d.setHours(0, 0, 0, 0);
283
- break;
284
- case 'year':
285
- d.setMonth(0, 1);
286
- d.setHours(0, 0, 0, 0);
287
- break;
288
- default:
289
- throw new Error(`Unsupported unit: ${unit}`);
290
- }
291
- return d;
292
- }
293
- /**
294
- * Get the end of a time period containing the specified date.
295
- *
296
- * @param date - Date to get the end from
297
- * @param unit - Time unit
298
- * @returns Date object representing the end of the time unit
299
- *
300
- * @example
301
- * ```typescript
302
- * // Get end of day (23:59:59.999)
303
- * endOf(new Date('2023-05-15T14:30:00'), 'day'); // Date for May 15, 2023 23:59:59.999
304
- *
305
- * // Get end of month
306
- * endOf(new Date('2023-05-15'), 'month'); // Date for May 31, 2023 23:59:59.999
307
- * ```
308
- */
309
- function endOf(date, unit) {
310
- const d = date instanceof Date ? new Date(date) : new Date(date);
311
- switch (unit) {
312
- case 'second':
313
- d.setMilliseconds(999);
314
- break;
315
- case 'minute':
316
- d.setSeconds(59, 999);
317
- break;
318
- case 'hour':
319
- d.setMinutes(59, 59, 999);
320
- break;
321
- case 'day':
322
- d.setHours(23, 59, 59, 999);
323
- break;
324
- case 'week':
325
- d.setDate(d.getDate() - d.getDay() + 6);
326
- d.setHours(23, 59, 59, 999);
327
- break;
328
- case 'month':
329
- d.setMonth(d.getMonth() + 1, 0);
330
- d.setHours(23, 59, 59, 999);
331
- break;
332
- case 'quarter':
333
- d.setMonth(Math.floor(d.getMonth() / 3) * 3 + 3, 0);
334
- d.setHours(23, 59, 59, 999);
335
- break;
336
- case 'year':
337
- d.setMonth(11, 31);
338
- d.setHours(23, 59, 59, 999);
339
- break;
340
- default:
341
- throw new Error(`Unsupported unit: ${unit}`);
342
- }
343
- return d;
344
- }
345
- /**
346
- * Check if a date is between two other dates.
347
- *
348
- * @param date - Date to check
349
- * @param start - Start date of the range
350
- * @param end - End date of the range
351
- * @param inclusive - Whether the comparison should be inclusive of start/end
352
- * @returns True if the date is within the range
353
- *
354
- * @example
355
- * ```typescript
356
- * const date = new Date('2023-05-15');
357
- * const start = new Date('2023-05-10');
358
- * const end = new Date('2023-05-20');
359
- *
360
- * isBetween(date, start, end); // true
361
- * ```
362
- */
363
- function isBetween(date, start, end, inclusive = true) {
364
- const d = date instanceof Date ? date.getTime() : date;
365
- const s = start instanceof Date ? start.getTime() : start;
366
- const e = end instanceof Date ? end.getTime() : end;
367
- return inclusive ? d >= s && d <= e : d > s && d < e;
368
- }
369
- /**
370
- * Check if a year is a leap year.
371
- *
372
- * @param year - Year to check (or date object)
373
- * @returns True if the year is a leap year
374
- */
375
- function isLeapYear(year) {
376
- const y = year instanceof Date ? year.getFullYear() : year;
377
- return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
378
- }
379
- /**
380
- * Get the number of days in a month.
381
- *
382
- * @param year - Year
383
- * @param month - Month (0-11)
384
- * @returns Number of days in the month
385
- */
386
- function daysInMonth(year, month) {
387
- if (year instanceof Date) {
388
- month = year.getMonth();
389
- year = year.getFullYear();
390
- }
391
- if (month === undefined) {
392
- throw new Error('Month is required when year is a number');
393
- }
394
- return new Date(year, month + 1, 0).getDate();
395
- }