@catbee/utils 1.1.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 (90) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +79 -43
  3. package/array/index.cjs +355 -0
  4. package/array/index.d.ts +317 -0
  5. package/array/index.mjs +327 -0
  6. package/async/index.cjs +484 -0
  7. package/async/index.d.ts +299 -0
  8. package/async/index.mjs +463 -0
  9. package/cache/index.cjs +292 -0
  10. package/cache/index.d.ts +179 -0
  11. package/cache/index.mjs +290 -0
  12. package/config/index.cjs +150 -0
  13. package/config/index.d.ts +88 -0
  14. package/config/index.mjs +143 -0
  15. package/context-store/index.cjs +267 -0
  16. package/context-store/index.d.ts +216 -0
  17. package/context-store/index.mjs +261 -0
  18. package/crypto/index.cjs +182 -0
  19. package/crypto/index.d.ts +187 -0
  20. package/crypto/index.mjs +166 -0
  21. package/date/index.cjs +340 -0
  22. package/date/index.d.ts +214 -0
  23. package/date/index.mjs +326 -0
  24. package/decorators/index.cjs +2051 -0
  25. package/decorators/index.d.ts +708 -0
  26. package/decorators/index.mjs +2010 -0
  27. package/dir/index.cjs +417 -0
  28. package/dir/index.d.ts +219 -0
  29. package/dir/index.mjs +390 -0
  30. package/env/index.cjs +745 -0
  31. package/env/index.d.ts +403 -0
  32. package/env/index.mjs +742 -0
  33. package/exception/index.cjs +362 -0
  34. package/exception/index.d.ts +256 -0
  35. package/exception/index.mjs +338 -0
  36. package/fs/index.cjs +287 -0
  37. package/fs/index.d.ts +229 -0
  38. package/fs/index.mjs +258 -0
  39. package/http-status-codes/index.cjs +96 -0
  40. package/http-status-codes/index.d.ts +291 -0
  41. package/http-status-codes/index.mjs +94 -0
  42. package/id/index.cjs +62 -0
  43. package/id/index.d.ts +61 -0
  44. package/id/index.mjs +56 -0
  45. package/index.cjs +218 -0
  46. package/index.d.ts +51 -0
  47. package/index.mjs +51 -0
  48. package/logger/index.cjs +334 -0
  49. package/logger/index.d.ts +213 -0
  50. package/logger/index.mjs +313 -0
  51. package/middleware/index.cjs +177 -0
  52. package/middleware/index.d.ts +127 -0
  53. package/middleware/index.mjs +170 -0
  54. package/obj/index.cjs +305 -0
  55. package/obj/index.d.ts +160 -0
  56. package/obj/index.mjs +289 -0
  57. package/package.json +172 -20
  58. package/performance/index.cjs +231 -0
  59. package/performance/index.d.ts +162 -0
  60. package/performance/index.mjs +225 -0
  61. package/request/index.cjs +202 -0
  62. package/request/index.d.ts +265 -0
  63. package/request/index.mjs +194 -0
  64. package/response/index.cjs +234 -0
  65. package/response/index.d.ts +342 -0
  66. package/response/index.mjs +222 -0
  67. package/server/index.cjs +1631 -0
  68. package/server/index.d.ts +809 -0
  69. package/server/index.mjs +1622 -0
  70. package/stream/index.cjs +151 -0
  71. package/stream/index.d.ts +114 -0
  72. package/stream/index.mjs +144 -0
  73. package/string/index.cjs +109 -0
  74. package/string/index.d.ts +126 -0
  75. package/string/index.mjs +95 -0
  76. package/type/index.cjs +129 -0
  77. package/type/index.d.ts +131 -0
  78. package/type/index.mjs +119 -0
  79. package/types/index.cjs +34 -0
  80. package/types/index.d.ts +798 -0
  81. package/types/index.mjs +32 -0
  82. package/url/index.cjs +199 -0
  83. package/url/index.d.ts +166 -0
  84. package/url/index.mjs +187 -0
  85. package/validation/index.cjs +259 -0
  86. package/validation/index.d.ts +209 -0
  87. package/validation/index.mjs +231 -0
  88. package/build/index.cjs +0 -7694
  89. package/build/index.d.ts +0 -5792
  90. package/build/index.mjs +0 -7386
@@ -0,0 +1,166 @@
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
+ import { scrypt, createHmac, createHash, randomBytes, timingSafeEqual, createCipheriv, createDecipheriv } from 'crypto';
26
+ import { promisify } from 'util';
27
+ import { uuid } from '@catbee/utils/id';
28
+
29
+ var __defProp = Object.defineProperty;
30
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
31
+ function hmac(algorithm, input, secret, encoding = "hex") {
32
+ return createHmac(algorithm, secret).update(input).digest(encoding);
33
+ }
34
+ __name(hmac, "hmac");
35
+ function hash(algorithm, input, encoding = "hex") {
36
+ return createHash(algorithm).update(input).digest(encoding);
37
+ }
38
+ __name(hash, "hash");
39
+ function sha256Hmac(input, secret) {
40
+ return hmac("sha256", input, secret);
41
+ }
42
+ __name(sha256Hmac, "sha256Hmac");
43
+ function sha1(input, encoding = "hex") {
44
+ return hash("sha1", input, encoding);
45
+ }
46
+ __name(sha1, "sha1");
47
+ function sha256(input, encoding = "hex") {
48
+ return hash("sha256", input, encoding);
49
+ }
50
+ __name(sha256, "sha256");
51
+ function md5(input) {
52
+ return hash("md5", input);
53
+ }
54
+ __name(md5, "md5");
55
+ function randomString() {
56
+ return sha256(uuid());
57
+ }
58
+ __name(randomString, "randomString");
59
+ function generateRandomBytes(byteLength = 32) {
60
+ return randomBytes(byteLength);
61
+ }
62
+ __name(generateRandomBytes, "generateRandomBytes");
63
+ function generateRandomBytesAsString(byteLength = 32, encoding = "hex") {
64
+ return randomBytes(byteLength).toString(encoding);
65
+ }
66
+ __name(generateRandomBytesAsString, "generateRandomBytesAsString");
67
+ function generateApiKey(prefix = "", byteLength = 24) {
68
+ const randomString2 = generateRandomBytesAsString(byteLength, "base64");
69
+ const key = randomString2.replace(/[+/=]/g, "").substring(0, 32);
70
+ return prefix ? `${prefix}_${key}` : key;
71
+ }
72
+ __name(generateApiKey, "generateApiKey");
73
+ function safeCompare(a, b) {
74
+ if (typeof a === "string" && typeof b === "string") {
75
+ const bufA = Buffer.from(a);
76
+ const bufB = Buffer.from(b);
77
+ if (bufA.length !== bufB.length) return false;
78
+ return timingSafeEqual(bufA, bufB);
79
+ } else if ((a instanceof Buffer || a instanceof Uint8Array) && (b instanceof Buffer || b instanceof Uint8Array)) {
80
+ if (a.length !== b.length) return false;
81
+ return timingSafeEqual(a instanceof Buffer ? a : Buffer.from(a), b instanceof Buffer ? b : Buffer.from(b));
82
+ }
83
+ throw new Error("Cannot compare: inputs must be strings, Buffers, or Uint8Arrays");
84
+ }
85
+ __name(safeCompare, "safeCompare");
86
+ var scryptAsync = promisify(scrypt);
87
+ async function encrypt(data, key, options = {}) {
88
+ const algorithm = options.algorithm || "aes-256-gcm";
89
+ const inputEncoding = options.inputEncoding || "utf8";
90
+ const outputEncoding = options.outputEncoding || "hex";
91
+ const iv = randomBytes(16);
92
+ const derivedKey = typeof key === "string" ? await scryptAsync(key, iv.slice(0, 8), 32) : key;
93
+ const cipher = createCipheriv(algorithm, derivedKey, iv);
94
+ let ciphertext;
95
+ if (typeof data === "string") {
96
+ ciphertext = cipher.update(data, inputEncoding, outputEncoding);
97
+ ciphertext += cipher.final(outputEncoding);
98
+ } else {
99
+ const encrypted = Buffer.concat([
100
+ cipher.update(data),
101
+ cipher.final()
102
+ ]);
103
+ ciphertext = encrypted.toString(outputEncoding) ;
104
+ }
105
+ const authTag = algorithm.includes("gcm") ? cipher.getAuthTag() : void 0;
106
+ return {
107
+ ciphertext,
108
+ iv,
109
+ authTag,
110
+ algorithm
111
+ };
112
+ }
113
+ __name(encrypt, "encrypt");
114
+ async function decrypt(encryptedData, key, options = {}) {
115
+ const algorithm = options.algorithm || encryptedData.algorithm || "aes-256-gcm";
116
+ const inputEncoding = options.inputEncoding || "hex";
117
+ const outputEncoding = options.outputEncoding || "utf8";
118
+ const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.iv.slice(0, 8), 32) : key;
119
+ const decipher = createDecipheriv(algorithm, derivedKey, encryptedData.iv);
120
+ if (encryptedData.authTag && algorithm.includes("gcm")) {
121
+ decipher.setAuthTag(encryptedData.authTag);
122
+ }
123
+ let decrypted;
124
+ if (typeof encryptedData.ciphertext === "string") {
125
+ decrypted = decipher.update(encryptedData.ciphertext, inputEncoding, outputEncoding);
126
+ decrypted += decipher.final(outputEncoding);
127
+ } else {
128
+ const result = Buffer.concat([
129
+ decipher.update(encryptedData.ciphertext),
130
+ decipher.final()
131
+ ]);
132
+ decrypted = result.toString(outputEncoding) ;
133
+ }
134
+ return decrypted;
135
+ }
136
+ __name(decrypt, "decrypt");
137
+ function createSignedToken(payload, secret, expiresInSeconds = 3600) {
138
+ const tokenPayload = {
139
+ ...payload,
140
+ exp: Math.floor(Date.now() / 1e3) + expiresInSeconds
141
+ };
142
+ const payloadStr = JSON.stringify(tokenPayload);
143
+ const base64Payload = Buffer.from(payloadStr).toString("base64url");
144
+ const signature = hmac("sha256", base64Payload, secret, "base64url");
145
+ return `${base64Payload}.${signature}`;
146
+ }
147
+ __name(createSignedToken, "createSignedToken");
148
+ function verifySignedToken(token, secret) {
149
+ try {
150
+ const [payloadB64, signature] = token.split(".");
151
+ if (!payloadB64 || !signature) return null;
152
+ const expectedSignature = hmac("sha256", payloadB64, secret, "base64url");
153
+ if (!safeCompare(signature, expectedSignature)) return null;
154
+ const payloadStr = Buffer.from(payloadB64, "base64url").toString("utf8");
155
+ const payload = JSON.parse(payloadStr);
156
+ if (payload.exp && payload.exp < Math.floor(Date.now() / 1e3)) {
157
+ return null;
158
+ }
159
+ return payload;
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+ __name(verifySignedToken, "verifySignedToken");
165
+
166
+ export { createSignedToken, decrypt, encrypt, generateApiKey, generateRandomBytes, generateRandomBytesAsString, hash, hmac, md5, randomString, safeCompare, sha1, sha256, sha256Hmac, verifySignedToken };
package/date/index.cjs ADDED
@@ -0,0 +1,340 @@
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
+ 'use strict';
26
+
27
+ var __defProp = Object.defineProperty;
28
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
29
+
30
+ // src/date/date.utils.ts
31
+ function formatDate(date, options = {}) {
32
+ const { format = "yyyy-MM-dd", locale, timeZone } = options;
33
+ const d = date instanceof Date ? date : new Date(date);
34
+ if (format === "yyyy-MM-dd") {
35
+ const yyyy = d.getFullYear();
36
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
37
+ const dd = String(d.getDate()).padStart(2, "0");
38
+ return `${yyyy}-${mm}-${dd}`;
39
+ }
40
+ if (format === "yyyy-MM-dd HH:mm:ss") {
41
+ const yyyy = d.getFullYear();
42
+ const mm = String(d.getMonth() + 1).padStart(2, "0");
43
+ const dd = String(d.getDate()).padStart(2, "0");
44
+ const HH = String(d.getHours()).padStart(2, "0");
45
+ const MM = String(d.getMinutes()).padStart(2, "0");
46
+ const SS = String(d.getSeconds()).padStart(2, "0");
47
+ return `${yyyy}-${mm}-${dd} ${HH}:${MM}:${SS}`;
48
+ }
49
+ if (format === "relative") {
50
+ return formatRelativeTime(date);
51
+ }
52
+ return new Intl.DateTimeFormat(locale, {
53
+ timeZone,
54
+ year: format.includes("yyyy") || format.includes("y") ? "numeric" : void 0,
55
+ month: format.includes("MM") || format.includes("M") ? "numeric" : void 0,
56
+ day: format.includes("dd") || format.includes("d") ? "numeric" : void 0,
57
+ hour: format.includes("HH") || format.includes("H") ? "numeric" : void 0,
58
+ minute: format.includes("mm") || format.includes("m") ? "numeric" : void 0,
59
+ second: format.includes("ss") || format.includes("s") ? "numeric" : void 0,
60
+ hour12: !format.includes("HH") && !format.includes("H")
61
+ }).format(d);
62
+ }
63
+ __name(formatDate, "formatDate");
64
+ function formatRelativeTime(date, now = /* @__PURE__ */ new Date(), locale) {
65
+ const d1 = date instanceof Date ? date : new Date(date);
66
+ const d2 = now instanceof Date ? now : new Date(now);
67
+ const diffMs = d1.getTime() - d2.getTime();
68
+ const diffSecs = Math.round(diffMs / 1e3);
69
+ const diffMins = Math.round(diffSecs / 60);
70
+ const diffHours = Math.round(diffMins / 60);
71
+ const diffDays = Math.round(diffHours / 24);
72
+ const diffMonths = Math.round(diffDays / 30);
73
+ const diffYears = Math.round(diffDays / 365);
74
+ const formatter = new Intl.RelativeTimeFormat(locale, {
75
+ numeric: "auto"
76
+ });
77
+ if (Math.abs(diffSecs) < 60) return formatter.format(diffSecs, "second");
78
+ if (Math.abs(diffMins) < 60) return formatter.format(diffMins, "minute");
79
+ if (Math.abs(diffHours) < 24) return formatter.format(diffHours, "hour");
80
+ if (Math.abs(diffDays) < 30) return formatter.format(diffDays, "day");
81
+ if (Math.abs(diffMonths) < 12) return formatter.format(diffMonths, "month");
82
+ return formatter.format(diffYears, "year");
83
+ }
84
+ __name(formatRelativeTime, "formatRelativeTime");
85
+ function parseDate(input, fallback) {
86
+ if (typeof input === "number") {
87
+ return new Date(input);
88
+ }
89
+ try {
90
+ const date = new Date(input);
91
+ if (Number.isNaN(date.getTime())) {
92
+ return fallback || null;
93
+ }
94
+ return date;
95
+ } catch {
96
+ return fallback || null;
97
+ }
98
+ }
99
+ __name(parseDate, "parseDate");
100
+ function dateDiff(date1, date2 = /* @__PURE__ */ new Date(), unit = "days") {
101
+ const d1 = date1 instanceof Date ? date1 : new Date(date1);
102
+ const d2 = date2 instanceof Date ? date2 : new Date(date2);
103
+ const diffMs = d1.getTime() - d2.getTime();
104
+ switch (unit) {
105
+ case "milliseconds":
106
+ return diffMs;
107
+ case "seconds":
108
+ return diffMs / 1e3;
109
+ case "minutes":
110
+ return diffMs / (1e3 * 60);
111
+ case "hours":
112
+ return diffMs / (1e3 * 60 * 60);
113
+ case "days":
114
+ return diffMs / (1e3 * 60 * 60 * 24);
115
+ case "months":
116
+ return (d1.getFullYear() - d2.getFullYear()) * 12 + d1.getMonth() - d2.getMonth();
117
+ case "years":
118
+ return d1.getFullYear() - d2.getFullYear();
119
+ default:
120
+ throw new Error(`Unsupported unit: ${unit}`);
121
+ }
122
+ }
123
+ __name(dateDiff, "dateDiff");
124
+ function addToDate(date, amount, unit) {
125
+ const d = date instanceof Date ? new Date(date) : new Date(date);
126
+ switch (unit) {
127
+ case "milliseconds":
128
+ d.setMilliseconds(d.getMilliseconds() + amount);
129
+ break;
130
+ case "seconds":
131
+ d.setSeconds(d.getSeconds() + amount);
132
+ break;
133
+ case "minutes":
134
+ d.setMinutes(d.getMinutes() + amount);
135
+ break;
136
+ case "hours":
137
+ d.setHours(d.getHours() + amount);
138
+ break;
139
+ case "days":
140
+ d.setDate(d.getDate() + amount);
141
+ break;
142
+ case "months": {
143
+ const origDate = d.getDate();
144
+ const origMonth = d.getMonth();
145
+ d.setDate(1);
146
+ d.setMonth(origMonth + amount);
147
+ const lastDay = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
148
+ d.setDate(Math.min(origDate, lastDay));
149
+ break;
150
+ }
151
+ case "years":
152
+ d.setFullYear(d.getFullYear() + amount);
153
+ break;
154
+ default:
155
+ throw new Error(`Unsupported unit: ${unit}`);
156
+ }
157
+ return d;
158
+ }
159
+ __name(addToDate, "addToDate");
160
+ function startOf(date, unit) {
161
+ const d = date instanceof Date ? new Date(date) : new Date(date);
162
+ switch (unit) {
163
+ case "second":
164
+ d.setMilliseconds(0);
165
+ break;
166
+ case "minute":
167
+ d.setSeconds(0, 0);
168
+ break;
169
+ case "hour":
170
+ d.setMinutes(0, 0, 0);
171
+ break;
172
+ case "day":
173
+ d.setHours(0, 0, 0, 0);
174
+ break;
175
+ case "week":
176
+ d.setHours(0, 0, 0, 0);
177
+ d.setDate(d.getDate() - d.getDay());
178
+ break;
179
+ case "month":
180
+ d.setDate(1);
181
+ d.setHours(0, 0, 0, 0);
182
+ break;
183
+ case "quarter":
184
+ d.setMonth(Math.floor(d.getMonth() / 3) * 3, 1);
185
+ d.setHours(0, 0, 0, 0);
186
+ break;
187
+ case "year":
188
+ d.setMonth(0, 1);
189
+ d.setHours(0, 0, 0, 0);
190
+ break;
191
+ default:
192
+ throw new Error(`Unsupported unit: ${unit}`);
193
+ }
194
+ return d;
195
+ }
196
+ __name(startOf, "startOf");
197
+ function endOf(date, unit) {
198
+ const d = date instanceof Date ? new Date(date) : new Date(date);
199
+ switch (unit) {
200
+ case "second":
201
+ d.setMilliseconds(999);
202
+ break;
203
+ case "minute":
204
+ d.setSeconds(59, 999);
205
+ break;
206
+ case "hour":
207
+ d.setMinutes(59, 59, 999);
208
+ break;
209
+ case "day":
210
+ d.setHours(23, 59, 59, 999);
211
+ break;
212
+ case "week":
213
+ d.setDate(d.getDate() - d.getDay() + 6);
214
+ d.setHours(23, 59, 59, 999);
215
+ break;
216
+ case "month":
217
+ d.setMonth(d.getMonth() + 1, 0);
218
+ d.setHours(23, 59, 59, 999);
219
+ break;
220
+ case "quarter":
221
+ d.setMonth(Math.floor(d.getMonth() / 3) * 3 + 3, 0);
222
+ d.setHours(23, 59, 59, 999);
223
+ break;
224
+ case "year":
225
+ d.setMonth(11, 31);
226
+ d.setHours(23, 59, 59, 999);
227
+ break;
228
+ default:
229
+ throw new Error(`Unsupported unit: ${unit}`);
230
+ }
231
+ return d;
232
+ }
233
+ __name(endOf, "endOf");
234
+ function isBetween(date, start, end, inclusive = true) {
235
+ const d = date instanceof Date ? date.getTime() : date;
236
+ const s = start instanceof Date ? start.getTime() : start;
237
+ const e = end instanceof Date ? end.getTime() : end;
238
+ return inclusive ? d >= s && d <= e : d > s && d < e;
239
+ }
240
+ __name(isBetween, "isBetween");
241
+ function isLeapYear(year) {
242
+ const y = year instanceof Date ? year.getFullYear() : year;
243
+ return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0;
244
+ }
245
+ __name(isLeapYear, "isLeapYear");
246
+ function daysInMonth(year, month) {
247
+ if (year instanceof Date) {
248
+ month = year.getMonth();
249
+ year = year.getFullYear();
250
+ }
251
+ if (month === void 0) {
252
+ throw new Error("Month is required when year is a number");
253
+ }
254
+ return new Date(year, month + 1, 0).getDate();
255
+ }
256
+ __name(daysInMonth, "daysInMonth");
257
+ function formatDuration(ms) {
258
+ if (!Number.isFinite(ms) || ms <= 0) return "0ms";
259
+ const parts = [];
260
+ const days = Math.floor(ms / (24 * 60 * 60 * 1e3));
261
+ if (days) {
262
+ parts.push(`${days}d`);
263
+ ms -= days * 24 * 60 * 60 * 1e3;
264
+ }
265
+ const hours = Math.floor(ms / (60 * 60 * 1e3));
266
+ if (hours) {
267
+ parts.push(`${hours}h`);
268
+ ms -= hours * 60 * 60 * 1e3;
269
+ }
270
+ const minutes = Math.floor(ms / (60 * 1e3));
271
+ if (minutes) {
272
+ parts.push(`${minutes}m`);
273
+ ms -= minutes * 60 * 1e3;
274
+ }
275
+ const seconds = Math.floor(ms / 1e3);
276
+ if (seconds) {
277
+ parts.push(`${seconds}s`);
278
+ ms -= seconds * 1e3;
279
+ }
280
+ if (ms > 0) parts.push(`${ms}ms`);
281
+ return parts.join(" ");
282
+ }
283
+ __name(formatDuration, "formatDuration");
284
+ function parseDuration(value) {
285
+ const MAX_SAFE = Number.MAX_SAFE_INTEGER;
286
+ if (typeof value === "number" && Number.isFinite(value)) return Math.min(value, MAX_SAFE);
287
+ if (typeof value !== "string" || !value.trim()) return 0;
288
+ if (/^\d+$/.test(value)) return Math.min(Number(value), MAX_SAFE);
289
+ const val = value.replace(/\s+/g, "").toLowerCase();
290
+ const unitMap = {
291
+ y: 31536e6,
292
+ w: 6048e5,
293
+ d: 864e5,
294
+ h: 36e5,
295
+ m: 6e4,
296
+ s: 1e3,
297
+ ms: 1
298
+ };
299
+ const unitKeys = Object.keys(unitMap).sort((a, b) => b.length - a.length);
300
+ let total = 0;
301
+ let i = 0;
302
+ while (i < val.length) {
303
+ const start = i;
304
+ while (i < val.length && /\d/.test(val[i])) i++;
305
+ if (start === i) {
306
+ throw new Error(`Invalid duration at offset ${i} in "${value}"`);
307
+ }
308
+ const num = Number(val.slice(start, i));
309
+ if (!Number.isSafeInteger(num)) throw new Error(`Invalid number at offset ${start} in "${value}"`);
310
+ const unit = unitKeys.find((u) => val.startsWith(u, i));
311
+ if (!unit) throw new Error(`Unknown unit at offset ${i} in "${value}"`);
312
+ const add = num * unitMap[unit];
313
+ total = Math.min(total + add, MAX_SAFE);
314
+ i += unit.length;
315
+ }
316
+ return total;
317
+ }
318
+ __name(parseDuration, "parseDuration");
319
+ function getDateFromDuration(input) {
320
+ const ms = parseDuration(input);
321
+ if (ms <= 0) {
322
+ throw new Error(`Invalid duration: "${input}". Duration must be a positive value.`);
323
+ }
324
+ return new Date(Date.now() + ms);
325
+ }
326
+ __name(getDateFromDuration, "getDateFromDuration");
327
+
328
+ exports.addToDate = addToDate;
329
+ exports.dateDiff = dateDiff;
330
+ exports.daysInMonth = daysInMonth;
331
+ exports.endOf = endOf;
332
+ exports.formatDate = formatDate;
333
+ exports.formatDuration = formatDuration;
334
+ exports.formatRelativeTime = formatRelativeTime;
335
+ exports.getDateFromDuration = getDateFromDuration;
336
+ exports.isBetween = isBetween;
337
+ exports.isLeapYear = isLeapYear;
338
+ exports.parseDate = parseDate;
339
+ exports.parseDuration = parseDuration;
340
+ exports.startOf = startOf;