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