@catbee/utils 1.0.5 → 2.0.0-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 (121) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -27
  3. package/array/array.utils.d.ts +191 -0
  4. package/array/index.cjs +246 -0
  5. package/array/index.d.ts +25 -0
  6. package/array/index.mjs +228 -0
  7. package/async/async.utils.d.ts +296 -0
  8. package/async/index.cjs +428 -0
  9. package/async/index.d.ts +25 -0
  10. package/async/index.mjs +407 -0
  11. package/cache/cache.utils.d.ts +176 -0
  12. package/cache/index.cjs +292 -0
  13. package/cache/index.d.ts +25 -0
  14. package/cache/index.mjs +290 -0
  15. package/config/config.d.ts +57 -0
  16. package/config/index.cjs +136 -0
  17. package/config/index.d.ts +26 -0
  18. package/config/index.mjs +131 -0
  19. package/context-store/context-store.utils.d.ts +212 -0
  20. package/context-store/index.cjs +267 -0
  21. package/context-store/index.d.ts +25 -0
  22. package/context-store/index.mjs +261 -0
  23. package/crypto/crypto.utils.d.ts +183 -0
  24. package/crypto/index.cjs +182 -0
  25. package/crypto/index.d.ts +25 -0
  26. package/crypto/index.mjs +166 -0
  27. package/date/date.utils.d.ts +190 -0
  28. package/date/index.cjs +295 -0
  29. package/date/index.d.ts +25 -0
  30. package/date/index.mjs +283 -0
  31. package/decorators/decorators.utils.d.ts +705 -0
  32. package/decorators/index.cjs +913 -0
  33. package/decorators/index.d.ts +25 -0
  34. package/decorators/index.mjs +872 -0
  35. package/dir/dir.utils.d.ts +216 -0
  36. package/dir/index.cjs +416 -0
  37. package/dir/index.d.ts +25 -0
  38. package/dir/index.mjs +389 -0
  39. package/env/env.utils.d.ts +400 -0
  40. package/env/index.cjs +761 -0
  41. package/env/index.d.ts +25 -0
  42. package/env/index.mjs +758 -0
  43. package/exception/exception.utils.d.ts +253 -0
  44. package/exception/index.cjs +362 -0
  45. package/exception/index.d.ts +25 -0
  46. package/exception/index.mjs +338 -0
  47. package/fs/fs.utils.d.ts +196 -0
  48. package/fs/index.cjs +253 -0
  49. package/fs/index.d.ts +25 -0
  50. package/fs/index.mjs +228 -0
  51. package/http-status-codes/http-status-codes.d.ts +289 -0
  52. package/http-status-codes/index.cjs +96 -0
  53. package/http-status-codes/index.d.ts +25 -0
  54. package/http-status-codes/index.mjs +94 -0
  55. package/id/id.utils.d.ts +59 -0
  56. package/id/index.cjs +62 -0
  57. package/id/index.d.ts +25 -0
  58. package/id/index.mjs +56 -0
  59. package/index.cjs +218 -0
  60. package/index.d.ts +51 -0
  61. package/index.mjs +51 -0
  62. package/logger/index.cjs +334 -0
  63. package/logger/index.d.ts +25 -0
  64. package/logger/index.mjs +313 -0
  65. package/logger/logger.utils.d.ts +210 -0
  66. package/middleware/index.cjs +177 -0
  67. package/middleware/index.d.ts +25 -0
  68. package/middleware/index.mjs +170 -0
  69. package/middleware/middleware.utils.d.ts +123 -0
  70. package/obj/index.cjs +317 -0
  71. package/obj/index.d.ts +25 -0
  72. package/obj/index.mjs +301 -0
  73. package/obj/obj.utils.d.ts +156 -0
  74. package/package.json +172 -20
  75. package/performance/index.cjs +231 -0
  76. package/performance/index.d.ts +25 -0
  77. package/performance/index.mjs +225 -0
  78. package/performance/performance.utils.d.ts +159 -0
  79. package/request/index.cjs +202 -0
  80. package/request/index.d.ts +26 -0
  81. package/request/index.mjs +194 -0
  82. package/request/request.utils.d.ts +109 -0
  83. package/response/index.cjs +234 -0
  84. package/response/index.d.ts +26 -0
  85. package/response/index.mjs +222 -0
  86. package/response/response.utils.d.ts +186 -0
  87. package/server/index.cjs +1627 -0
  88. package/server/index.d.ts +28 -0
  89. package/server/index.mjs +1617 -0
  90. package/server/server.builder.d.ts +531 -0
  91. package/server/server.d.ts +303 -0
  92. package/stream/index.cjs +151 -0
  93. package/stream/index.d.ts +25 -0
  94. package/stream/index.mjs +144 -0
  95. package/stream/stream.utils.d.ts +111 -0
  96. package/string/index.cjs +109 -0
  97. package/string/index.d.ts +25 -0
  98. package/string/index.mjs +95 -0
  99. package/string/string.utils.d.ts +124 -0
  100. package/type/index.cjs +129 -0
  101. package/type/index.d.ts +25 -0
  102. package/type/index.mjs +119 -0
  103. package/type/type.utils.d.ts +129 -0
  104. package/types/api-response.d.ts +175 -0
  105. package/types/common.d.ts +148 -0
  106. package/types/config.d.ts +88 -0
  107. package/types/index.cjs +34 -0
  108. package/types/index.d.ts +28 -0
  109. package/types/index.mjs +32 -0
  110. package/types/server.d.ts +291 -0
  111. package/url/index.cjs +201 -0
  112. package/url/index.d.ts +25 -0
  113. package/url/index.mjs +189 -0
  114. package/url/url.utils.d.ts +164 -0
  115. package/validate/index.cjs +212 -0
  116. package/validate/index.d.ts +25 -0
  117. package/validate/index.mjs +188 -0
  118. package/validate/validate.utils.d.ts +200 -0
  119. package/build/index.cjs +0 -7579
  120. package/build/index.d.ts +0 -5774
  121. package/build/index.mjs +0 -7274
package/date/index.mjs ADDED
@@ -0,0 +1,283 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
+ var __defProp = Object.defineProperty;
26
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
27
+
28
+ // src/date/date.utils.ts
29
+ function formatDate(date, options = {}) {
30
+ const { format = "yyyy-MM-dd", locale, timeZone } = options;
31
+ const d = date instanceof Date ? date : new Date(date);
32
+ if (format === "yyyy-MM-dd") {
33
+ const yyyy = d.getFullYear();
34
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
35
+ const dd = String(d.getDate()).padStart(2, "0");
36
+ return `${yyyy}-${mm}-${dd}`;
37
+ }
38
+ if (format === "yyyy-MM-dd HH:mm:ss") {
39
+ const yyyy = d.getFullYear();
40
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
41
+ const dd = String(d.getDate()).padStart(2, "0");
42
+ const HH = String(d.getHours()).padStart(2, "0");
43
+ const MM = String(d.getMinutes()).padStart(2, "0");
44
+ const SS = String(d.getSeconds()).padStart(2, "0");
45
+ return `${yyyy}-${mm}-${dd} ${HH}:${MM}:${SS}`;
46
+ }
47
+ if (format === "relative") {
48
+ return formatRelativeTime(date);
49
+ }
50
+ return new Intl.DateTimeFormat(locale, {
51
+ timeZone,
52
+ year: format.includes("yyyy") || format.includes("y") ? "numeric" : void 0,
53
+ month: format.includes("MM") || format.includes("M") ? "numeric" : void 0,
54
+ day: format.includes("dd") || format.includes("d") ? "numeric" : void 0,
55
+ hour: format.includes("HH") || format.includes("H") ? "numeric" : void 0,
56
+ minute: format.includes("mm") || format.includes("m") ? "numeric" : void 0,
57
+ second: format.includes("ss") || format.includes("s") ? "numeric" : void 0,
58
+ hour12: !format.includes("HH") && !format.includes("H")
59
+ }).format(d);
60
+ }
61
+ __name(formatDate, "formatDate");
62
+ function formatRelativeTime(date, now = /* @__PURE__ */ new Date(), locale) {
63
+ const d1 = date instanceof Date ? date : new Date(date);
64
+ const d2 = now instanceof Date ? now : new Date(now);
65
+ const diffMs = d1.getTime() - d2.getTime();
66
+ const diffSecs = Math.round(diffMs / 1e3);
67
+ const diffMins = Math.round(diffSecs / 60);
68
+ const diffHours = Math.round(diffMins / 60);
69
+ const diffDays = Math.round(diffHours / 24);
70
+ const diffMonths = Math.round(diffDays / 30);
71
+ const diffYears = Math.round(diffDays / 365);
72
+ const formatter = new Intl.RelativeTimeFormat(locale, {
73
+ numeric: "auto"
74
+ });
75
+ if (Math.abs(diffSecs) < 60) return formatter.format(diffSecs, "second");
76
+ if (Math.abs(diffMins) < 60) return formatter.format(diffMins, "minute");
77
+ if (Math.abs(diffHours) < 24) return formatter.format(diffHours, "hour");
78
+ if (Math.abs(diffDays) < 30) return formatter.format(diffDays, "day");
79
+ if (Math.abs(diffMonths) < 12) return formatter.format(diffMonths, "month");
80
+ return formatter.format(diffYears, "year");
81
+ }
82
+ __name(formatRelativeTime, "formatRelativeTime");
83
+ function parseDate(input, fallback) {
84
+ if (typeof input === "number") {
85
+ return new Date(input);
86
+ }
87
+ try {
88
+ const date = new Date(input);
89
+ if (isNaN(date.getTime())) {
90
+ return fallback || null;
91
+ }
92
+ return date;
93
+ } catch {
94
+ return fallback || null;
95
+ }
96
+ }
97
+ __name(parseDate, "parseDate");
98
+ function dateDiff(date1, date2 = /* @__PURE__ */ new Date(), unit = "days") {
99
+ const d1 = date1 instanceof Date ? date1 : new Date(date1);
100
+ const d2 = date2 instanceof Date ? date2 : new Date(date2);
101
+ const diffMs = d1.getTime() - d2.getTime();
102
+ switch (unit) {
103
+ case "milliseconds":
104
+ return diffMs;
105
+ case "seconds":
106
+ return diffMs / 1e3;
107
+ case "minutes":
108
+ return diffMs / (1e3 * 60);
109
+ case "hours":
110
+ return diffMs / (1e3 * 60 * 60);
111
+ case "days":
112
+ return diffMs / (1e3 * 60 * 60 * 24);
113
+ case "months":
114
+ return (d1.getFullYear() - d2.getFullYear()) * 12 + d1.getMonth() - d2.getMonth();
115
+ case "years":
116
+ return d1.getFullYear() - d2.getFullYear();
117
+ default:
118
+ throw new Error(`Unsupported unit: ${unit}`);
119
+ }
120
+ }
121
+ __name(dateDiff, "dateDiff");
122
+ function addToDate(date, amount, unit) {
123
+ const d = date instanceof Date ? new Date(date) : new Date(date);
124
+ switch (unit) {
125
+ case "milliseconds":
126
+ d.setMilliseconds(d.getMilliseconds() + amount);
127
+ break;
128
+ case "seconds":
129
+ d.setSeconds(d.getSeconds() + amount);
130
+ break;
131
+ case "minutes":
132
+ d.setMinutes(d.getMinutes() + amount);
133
+ break;
134
+ case "hours":
135
+ d.setHours(d.getHours() + amount);
136
+ break;
137
+ case "days":
138
+ d.setDate(d.getDate() + amount);
139
+ break;
140
+ case "months": {
141
+ const origDate = d.getDate();
142
+ const origMonth = d.getMonth();
143
+ d.setDate(1);
144
+ d.setMonth(origMonth + amount);
145
+ const lastDay = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
146
+ d.setDate(Math.min(origDate, lastDay));
147
+ break;
148
+ }
149
+ case "years":
150
+ d.setFullYear(d.getFullYear() + amount);
151
+ break;
152
+ default:
153
+ throw new Error(`Unsupported unit: ${unit}`);
154
+ }
155
+ return d;
156
+ }
157
+ __name(addToDate, "addToDate");
158
+ function startOf(date, unit) {
159
+ const d = date instanceof Date ? new Date(date) : new Date(date);
160
+ switch (unit) {
161
+ case "second":
162
+ d.setMilliseconds(0);
163
+ break;
164
+ case "minute":
165
+ d.setSeconds(0, 0);
166
+ break;
167
+ case "hour":
168
+ d.setMinutes(0, 0, 0);
169
+ break;
170
+ case "day":
171
+ d.setHours(0, 0, 0, 0);
172
+ break;
173
+ case "week":
174
+ d.setHours(0, 0, 0, 0);
175
+ d.setDate(d.getDate() - d.getDay());
176
+ break;
177
+ case "month":
178
+ d.setDate(1);
179
+ d.setHours(0, 0, 0, 0);
180
+ break;
181
+ case "quarter":
182
+ d.setMonth(Math.floor(d.getMonth() / 3) * 3, 1);
183
+ d.setHours(0, 0, 0, 0);
184
+ break;
185
+ case "year":
186
+ d.setMonth(0, 1);
187
+ d.setHours(0, 0, 0, 0);
188
+ break;
189
+ default:
190
+ throw new Error(`Unsupported unit: ${unit}`);
191
+ }
192
+ return d;
193
+ }
194
+ __name(startOf, "startOf");
195
+ function endOf(date, unit) {
196
+ const d = date instanceof Date ? new Date(date) : new Date(date);
197
+ switch (unit) {
198
+ case "second":
199
+ d.setMilliseconds(999);
200
+ break;
201
+ case "minute":
202
+ d.setSeconds(59, 999);
203
+ break;
204
+ case "hour":
205
+ d.setMinutes(59, 59, 999);
206
+ break;
207
+ case "day":
208
+ d.setHours(23, 59, 59, 999);
209
+ break;
210
+ case "week":
211
+ d.setDate(d.getDate() - d.getDay() + 6);
212
+ d.setHours(23, 59, 59, 999);
213
+ break;
214
+ case "month":
215
+ d.setMonth(d.getMonth() + 1, 0);
216
+ d.setHours(23, 59, 59, 999);
217
+ break;
218
+ case "quarter":
219
+ d.setMonth(Math.floor(d.getMonth() / 3) * 3 + 3, 0);
220
+ d.setHours(23, 59, 59, 999);
221
+ break;
222
+ case "year":
223
+ d.setMonth(11, 31);
224
+ d.setHours(23, 59, 59, 999);
225
+ break;
226
+ default:
227
+ throw new Error(`Unsupported unit: ${unit}`);
228
+ }
229
+ return d;
230
+ }
231
+ __name(endOf, "endOf");
232
+ function isBetween(date, start, end, inclusive = true) {
233
+ const d = date instanceof Date ? date.getTime() : date;
234
+ const s = start instanceof Date ? start.getTime() : start;
235
+ const e = end instanceof Date ? end.getTime() : end;
236
+ return inclusive ? d >= s && d <= e : d > s && d < e;
237
+ }
238
+ __name(isBetween, "isBetween");
239
+ function isLeapYear(year) {
240
+ const y = year instanceof Date ? year.getFullYear() : year;
241
+ return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0;
242
+ }
243
+ __name(isLeapYear, "isLeapYear");
244
+ function daysInMonth(year, month) {
245
+ if (year instanceof Date) {
246
+ month = year.getMonth();
247
+ year = year.getFullYear();
248
+ }
249
+ if (month === void 0) {
250
+ throw new Error("Month is required when year is a number");
251
+ }
252
+ return new Date(year, month + 1, 0).getDate();
253
+ }
254
+ __name(daysInMonth, "daysInMonth");
255
+ function formatDuration(ms) {
256
+ if (!Number.isFinite(ms) || ms <= 0) return "0ms";
257
+ const parts = [];
258
+ const days = Math.floor(ms / (24 * 60 * 60 * 1e3));
259
+ if (days) {
260
+ parts.push(`${days}d`);
261
+ ms -= days * 24 * 60 * 60 * 1e3;
262
+ }
263
+ const hours = Math.floor(ms / (60 * 60 * 1e3));
264
+ if (hours) {
265
+ parts.push(`${hours}h`);
266
+ ms -= hours * 60 * 60 * 1e3;
267
+ }
268
+ const minutes = Math.floor(ms / (60 * 1e3));
269
+ if (minutes) {
270
+ parts.push(`${minutes}m`);
271
+ ms -= minutes * 60 * 1e3;
272
+ }
273
+ const seconds = Math.floor(ms / 1e3);
274
+ if (seconds) {
275
+ parts.push(`${seconds}s`);
276
+ ms -= seconds * 1e3;
277
+ }
278
+ if (ms > 0) parts.push(`${ms}ms`);
279
+ return parts.join(" ");
280
+ }
281
+ __name(formatDuration, "formatDuration");
282
+
283
+ export { addToDate, dateDiff, daysInMonth, endOf, formatDate, formatDuration, formatRelativeTime, isBetween, isLeapYear, parseDate, startOf };