@catbee/utils 2.0.0-next.0 → 2.0.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +52 -16
  2. package/array/index.cjs +180 -71
  3. package/array/index.d.ts +293 -1
  4. package/array/index.mjs +171 -72
  5. package/async/index.cjs +92 -36
  6. package/async/index.d.ts +275 -1
  7. package/async/index.mjs +92 -36
  8. package/cache/index.cjs +1 -1
  9. package/cache/index.d.ts +155 -1
  10. package/cache/index.mjs +2 -2
  11. package/config/index.cjs +78 -64
  12. package/config/index.d.ts +64 -2
  13. package/config/index.mjs +76 -64
  14. package/context-store/index.d.ts +192 -1
  15. package/crypto/index.d.ts +163 -1
  16. package/date/index.cjs +46 -1
  17. package/date/index.d.ts +190 -1
  18. package/date/index.mjs +45 -2
  19. package/decorators/index.cjs +1156 -18
  20. package/decorators/index.d.ts +684 -1
  21. package/decorators/index.mjs +1156 -18
  22. package/dir/index.cjs +4 -3
  23. package/dir/index.d.ts +195 -1
  24. package/dir/index.mjs +4 -3
  25. package/env/index.cjs +10 -26
  26. package/env/index.d.ts +379 -1
  27. package/env/index.mjs +10 -26
  28. package/exception/index.d.ts +232 -1
  29. package/fs/index.cjs +70 -36
  30. package/fs/index.d.ts +205 -1
  31. package/fs/index.mjs +64 -34
  32. package/http-status-codes/index.d.ts +267 -1
  33. package/id/index.d.ts +37 -1
  34. package/index.cjs +3 -3
  35. package/index.d.ts +1 -1
  36. package/index.mjs +1 -1
  37. package/logger/index.cjs +11 -11
  38. package/logger/index.d.ts +189 -1
  39. package/logger/index.mjs +12 -12
  40. package/middleware/index.d.ts +103 -1
  41. package/obj/index.cjs +150 -162
  42. package/obj/index.d.ts +136 -1
  43. package/obj/index.mjs +150 -162
  44. package/package.json +11 -11
  45. package/performance/index.cjs +2 -2
  46. package/performance/index.d.ts +138 -1
  47. package/performance/index.mjs +2 -2
  48. package/request/index.cjs +1 -1
  49. package/request/index.d.ts +241 -2
  50. package/request/index.mjs +1 -1
  51. package/response/index.d.ts +318 -2
  52. package/server/index.cjs +27 -23
  53. package/server/index.d.ts +785 -4
  54. package/server/index.mjs +28 -23
  55. package/stream/index.d.ts +90 -1
  56. package/string/index.d.ts +102 -1
  57. package/type/index.cjs +1 -1
  58. package/type/index.d.ts +107 -1
  59. package/type/index.mjs +1 -1
  60. package/types/index.d.ts +774 -4
  61. package/url/index.cjs +2 -4
  62. package/url/index.d.ts +142 -1
  63. package/url/index.mjs +2 -4
  64. package/{validate → validation}/index.cjs +89 -42
  65. package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
  66. package/{validate → validation}/index.mjs +85 -42
  67. package/array/array.utils.d.ts +0 -191
  68. package/async/async.utils.d.ts +0 -296
  69. package/cache/cache.utils.d.ts +0 -176
  70. package/config/config.d.ts +0 -57
  71. package/context-store/context-store.utils.d.ts +0 -212
  72. package/crypto/crypto.utils.d.ts +0 -183
  73. package/date/date.utils.d.ts +0 -190
  74. package/decorators/decorators.utils.d.ts +0 -705
  75. package/dir/dir.utils.d.ts +0 -216
  76. package/env/env.utils.d.ts +0 -400
  77. package/exception/exception.utils.d.ts +0 -253
  78. package/fs/fs.utils.d.ts +0 -196
  79. package/http-status-codes/http-status-codes.d.ts +0 -289
  80. package/id/id.utils.d.ts +0 -59
  81. package/logger/logger.utils.d.ts +0 -210
  82. package/middleware/middleware.utils.d.ts +0 -123
  83. package/obj/obj.utils.d.ts +0 -156
  84. package/performance/performance.utils.d.ts +0 -159
  85. package/request/request.utils.d.ts +0 -109
  86. package/response/response.utils.d.ts +0 -186
  87. package/server/server.builder.d.ts +0 -531
  88. package/server/server.d.ts +0 -303
  89. package/stream/stream.utils.d.ts +0 -111
  90. package/string/string.utils.d.ts +0 -124
  91. package/type/type.utils.d.ts +0 -129
  92. package/types/api-response.d.ts +0 -175
  93. package/types/common.d.ts +0 -148
  94. package/types/config.d.ts +0 -88
  95. package/types/server.d.ts +0 -291
  96. package/url/url.utils.d.ts +0 -164
  97. package/validate/index.d.ts +0 -25
package/date/index.d.ts CHANGED
@@ -22,4 +22,193 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
- export * from './date.utils';
25
+ /**
26
+ * Format options for the formatDate function
27
+ */
28
+ interface DateFormatOptions {
29
+ /** Date format pattern (default: 'yyyy-MM-dd') */
30
+ format?: string;
31
+ /** Locale to use for formatting (default: system locale) */
32
+ locale?: string | string[];
33
+ /** Time zone to use (default: system time zone) */
34
+ timeZone?: string;
35
+ }
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
+ declare function formatDate(date: Date | number, options?: DateFormatOptions): string;
56
+ /**
57
+ * Format a date as relative time (e.g., "5 minutes ago", "in 3 days").
58
+ *
59
+ * @param date - Date to format
60
+ * @param now - Reference date (default: current time)
61
+ * @param locale - Locale to use for formatting
62
+ * @returns Formatted relative time string
63
+ */
64
+ declare function formatRelativeTime(date: Date | number, now?: Date | number, locale?: string | string[]): string;
65
+ /**
66
+ * Parse a date string or timestamp into a Date object.
67
+ *
68
+ * @param input - Date string or timestamp to parse
69
+ * @param fallback - Fallback date if parsing fails
70
+ * @returns Parsed Date object or fallback
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * parseDate('2023-05-15'); // Date object for May 15, 2023
75
+ * parseDate('invalid', new Date()); // Returns current date as fallback
76
+ * ```
77
+ */
78
+ declare function parseDate(input: string | number, fallback?: Date): Date | null;
79
+ /**
80
+ * Calculate the difference between two dates in the specified unit.
81
+ *
82
+ * @param date1 - First date
83
+ * @param date2 - Second date (default: current time)
84
+ * @param unit - Unit of time for the difference
85
+ * @returns Difference in the specified unit
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * // Get difference in days
90
+ * dateDiff(new Date('2023-05-15'), new Date('2023-05-10'), 'days'); // 5
91
+ *
92
+ * // Get difference in hours
93
+ * dateDiff(new Date('2023-05-15T10:00:00'), new Date('2023-05-15T06:00:00'), 'hours'); // 4
94
+ * ```
95
+ */
96
+ declare function dateDiff(date1: Date | number, date2?: Date | number, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years'): number;
97
+ /**
98
+ * Add a specified amount of time to a date.
99
+ *
100
+ * @param date - Base date
101
+ * @param amount - Amount to add (can be negative)
102
+ * @param unit - Unit of time to add
103
+ * @returns New date with the addition
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * // Add 5 days
108
+ * addToDate(new Date('2023-05-15'), 5, 'days'); // Date for May 20, 2023
109
+ *
110
+ * // Subtract 2 hours
111
+ * addToDate(new Date('2023-05-15T10:00:00'), -2, 'hours'); // Date for May 15, 2023 08:00:00
112
+ * ```
113
+ */
114
+ declare function addToDate(date: Date | number, amount: number, unit: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years'): Date;
115
+ /**
116
+ * Get the start of a time period containing the specified date.
117
+ *
118
+ * @param date - Date to get the start from
119
+ * @param unit - Time unit
120
+ * @returns Date object representing the start of the time unit
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * // Get start of day (midnight)
125
+ * startOf(new Date('2023-05-15T14:30:00'), 'day'); // Date for May 15, 2023 00:00:00
126
+ *
127
+ * // Get start of month
128
+ * startOf(new Date('2023-05-15'), 'month'); // Date for May 1, 2023
129
+ * ```
130
+ */
131
+ declare function startOf(date: Date | number, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'): Date;
132
+ /**
133
+ * Get the end of a time period containing the specified date.
134
+ *
135
+ * @param date - Date to get the end from
136
+ * @param unit - Time unit
137
+ * @returns Date object representing the end of the time unit
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * // Get end of day (23:59:59.999)
142
+ * endOf(new Date('2023-05-15T14:30:00'), 'day'); // Date for May 15, 2023 23:59:59.999
143
+ *
144
+ * // Get end of month
145
+ * endOf(new Date('2023-05-15'), 'month'); // Date for May 31, 2023 23:59:59.999
146
+ * ```
147
+ */
148
+ declare function endOf(date: Date | number, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'): Date;
149
+ /**
150
+ * Check if a date is between two other dates.
151
+ *
152
+ * @param date - Date to check
153
+ * @param start - Start date of the range
154
+ * @param end - End date of the range
155
+ * @param inclusive - Whether the comparison should be inclusive of start/end
156
+ * @returns True if the date is within the range
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * const date = new Date('2023-05-15');
161
+ * const start = new Date('2023-05-10');
162
+ * const end = new Date('2023-05-20');
163
+ *
164
+ * isBetween(date, start, end); // true
165
+ * ```
166
+ */
167
+ declare function isBetween(date: Date | number, start: Date | number, end: Date | number, inclusive?: boolean): boolean;
168
+ /**
169
+ * Check if a year is a leap year.
170
+ *
171
+ * @param year - Year to check (or date object)
172
+ * @returns True if the year is a leap year
173
+ */
174
+ declare function isLeapYear(year: number | Date): boolean;
175
+ /**
176
+ * Get the number of days in a month.
177
+ *
178
+ * @param year - Year
179
+ * @param month - Month (0-11)
180
+ * @returns Number of days in the month
181
+ */
182
+ declare function daysInMonth(year: number | Date, month?: number): number;
183
+ /**
184
+ * Format a duration given in milliseconds to a human readable string.
185
+ * Examples: 3661000 -> "1h 1m 1s", 90061 -> "1m 30s 61ms"
186
+ *
187
+ * @param {number} ms - Duration in milliseconds
188
+ * @returns {string} Human readable duration
189
+ */
190
+ declare function formatDuration(ms: number): string;
191
+ /**
192
+ * Parse a duration string or number into milliseconds.
193
+ * Supports formats like: 1y, 2w, 3d, 4h, 5m, 6s, 7ms, 1w2d3h, or plain milliseconds like "60000"
194
+ *
195
+ * @param value - Duration string or number
196
+ * @returns Milliseconds
197
+ */
198
+ declare function parseDuration(value: string | number): number;
199
+ /**
200
+ * Convert a duration string into a Date object representing the future time.
201
+ *
202
+ * @param input - Duration string (e.g., "5m", "2h", "1d")
203
+ * @returns Date object representing the future time
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * toDateFromNow("5m"); // Date object 5 minutes from now
208
+ * toDateFromNow("2h"); // Date object 2 hours from now
209
+ * ```
210
+ */
211
+ declare function getDateFromDuration(input: string): Date;
212
+
213
+ export { addToDate, dateDiff, daysInMonth, endOf, formatDate, formatDuration, formatRelativeTime, getDateFromDuration, isBetween, isLeapYear, parseDate, parseDuration, startOf };
214
+ export type { DateFormatOptions };
package/date/index.mjs CHANGED
@@ -86,7 +86,7 @@ function parseDate(input, fallback) {
86
86
  }
87
87
  try {
88
88
  const date = new Date(input);
89
- if (isNaN(date.getTime())) {
89
+ if (Number.isNaN(date.getTime())) {
90
90
  return fallback || null;
91
91
  }
92
92
  return date;
@@ -279,5 +279,48 @@ function formatDuration(ms) {
279
279
  return parts.join(" ");
280
280
  }
281
281
  __name(formatDuration, "formatDuration");
282
+ function parseDuration(value) {
283
+ const MAX_SAFE = Number.MAX_SAFE_INTEGER;
284
+ if (typeof value === "number" && Number.isFinite(value)) return Math.min(value, MAX_SAFE);
285
+ if (typeof value !== "string" || !value.trim()) return 0;
286
+ if (/^\d+$/.test(value)) return Math.min(Number(value), MAX_SAFE);
287
+ const val = value.replace(/\s+/g, "").toLowerCase();
288
+ const unitMap = {
289
+ y: 31536e6,
290
+ w: 6048e5,
291
+ d: 864e5,
292
+ h: 36e5,
293
+ m: 6e4,
294
+ s: 1e3,
295
+ ms: 1
296
+ };
297
+ const unitKeys = Object.keys(unitMap).sort((a, b) => b.length - a.length);
298
+ let total = 0;
299
+ let i = 0;
300
+ while (i < val.length) {
301
+ const start = i;
302
+ while (i < val.length && /\d/.test(val[i])) i++;
303
+ if (start === i) {
304
+ throw new Error(`Invalid duration at offset ${i} in "${value}"`);
305
+ }
306
+ const num = Number(val.slice(start, i));
307
+ if (!Number.isSafeInteger(num)) throw new Error(`Invalid number at offset ${start} in "${value}"`);
308
+ const unit = unitKeys.find((u) => val.startsWith(u, i));
309
+ if (!unit) throw new Error(`Unknown unit at offset ${i} in "${value}"`);
310
+ const add = num * unitMap[unit];
311
+ total = Math.min(total + add, MAX_SAFE);
312
+ i += unit.length;
313
+ }
314
+ return total;
315
+ }
316
+ __name(parseDuration, "parseDuration");
317
+ function getDateFromDuration(input) {
318
+ const ms = parseDuration(input);
319
+ if (ms <= 0) {
320
+ throw new Error(`Invalid duration: "${input}". Duration must be a positive value.`);
321
+ }
322
+ return new Date(Date.now() + ms);
323
+ }
324
+ __name(getDateFromDuration, "getDateFromDuration");
282
325
 
283
- export { addToDate, dateDiff, daysInMonth, endOf, formatDate, formatDuration, formatRelativeTime, isBetween, isLeapYear, parseDate, startOf };
326
+ export { addToDate, dateDiff, daysInMonth, endOf, formatDate, formatDuration, formatRelativeTime, getDateFromDuration, isBetween, isLeapYear, parseDate, parseDuration, startOf };