@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,295 +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.hmac = hmac;
27
- exports.hash = hash;
28
- exports.sha256Hmac = sha256Hmac;
29
- exports.sha1 = sha1;
30
- exports.sha256 = sha256;
31
- exports.md5 = md5;
32
- exports.randomString = randomString;
33
- exports.generateRandomBytes = generateRandomBytes;
34
- exports.generateRandomBytesAsString = generateRandomBytesAsString;
35
- exports.generateApiKey = generateApiKey;
36
- exports.safeCompare = safeCompare;
37
- exports.encrypt = encrypt;
38
- exports.decrypt = decrypt;
39
- exports.createSignedToken = createSignedToken;
40
- exports.verifySignedToken = verifySignedToken;
41
- const crypto_1 = require("crypto");
42
- const util_1 = require("util");
43
- const id_utils_1 = require("./id.utils");
44
- /**
45
- * Generates an HMAC digest using the specified algorithm and secret key.
46
- *
47
- * @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'sha1').
48
- * @param {string} input - The string to hash.
49
- * @param {string} secret - The secret key for HMAC.
50
- * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
51
- * @returns {string} HMAC digest as a string.
52
- */
53
- function hmac(algorithm, input, secret, encoding = 'hex') {
54
- return (0, crypto_1.createHmac)(algorithm, secret).update(input).digest(encoding);
55
- }
56
- /**
57
- * Generates a hash digest using the specified algorithm.
58
- *
59
- * @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'md5').
60
- * @param {string} input - The string to hash.
61
- * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
62
- * @returns {string} Hash digest as a string.
63
- */
64
- function hash(algorithm, input, encoding = 'hex') {
65
- return (0, crypto_1.createHash)(algorithm).update(input).digest(encoding);
66
- }
67
- /**
68
- * Generates an HMAC-SHA256 digest.
69
- *
70
- * @param {string} input - The string to hash.
71
- * @param {string} secret - The secret key.
72
- * @returns {string} SHA-256 HMAC digest as a string.
73
- */
74
- function sha256Hmac(input, secret) {
75
- return hmac('sha256', input, secret);
76
- }
77
- /**
78
- * Generates a SHA1 hash digest.
79
- *
80
- * @param {string} input - The string to hash.
81
- * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
82
- * @returns {string} SHA-1 hash as a string.
83
- */
84
- function sha1(input, encoding = 'hex') {
85
- return hash('sha1', input, encoding);
86
- }
87
- /**
88
- * Generates a SHA256 hash digest.
89
- *
90
- * @param {string} input - The string to hash.
91
- * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
92
- * @returns {string} SHA-256 hash as a string.
93
- */
94
- function sha256(input, encoding = 'hex') {
95
- return hash('sha256', input, encoding);
96
- }
97
- /**
98
- * Generates an MD5 hash digest.
99
- *
100
- * @param {string} input - The string to hash.
101
- * @returns {string} MD5 hash as a string.
102
- */
103
- function md5(input) {
104
- return hash('md5', input);
105
- }
106
- /**
107
- * Generates a cryptographically strong random string by hashing a random UUID with SHA-256.
108
- *
109
- * @returns {string} Random string hashed with SHA-256 (hex encoding).
110
- */
111
- function randomString() {
112
- return sha256((0, id_utils_1.uuid)());
113
- }
114
- /**
115
- * Generates a secure random buffer of specified byte length.
116
- *
117
- * @param {number} [byteLength=32] - Number of random bytes to generate.
118
- * @returns {Buffer} Buffer containing random bytes.
119
- */
120
- function generateRandomBytes(byteLength = 32) {
121
- return (0, crypto_1.randomBytes)(byteLength);
122
- }
123
- /**
124
- * Generates a secure random string of specified byte length with specified encoding.
125
- *
126
- * @param {number} [byteLength=32] - Number of random bytes to generate.
127
- * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
128
- * @returns {string} Random string in specified encoding.
129
- */
130
- function generateRandomBytesAsString(byteLength = 32, encoding = 'hex') {
131
- return (0, crypto_1.randomBytes)(byteLength).toString(encoding);
132
- }
133
- /**
134
- * Generates a secure API key with a specified format.
135
- *
136
- * @param {string} [prefix=''] - Optional prefix for the key.
137
- * @param {number} [byteLength=24] - Number of random bytes to generate.
138
- * @returns {string} Formatted API key.
139
- */
140
- function generateApiKey(prefix = '', byteLength = 24) {
141
- const randomString = generateRandomBytesAsString(byteLength, 'base64');
142
- const key = randomString
143
- .replace(/[+/=]/g, '') // Remove non-URL-safe characters
144
- .substring(0, 32); // Limit length
145
- return prefix ? `${prefix}_${key}` : key;
146
- }
147
- /**
148
- * Compares two strings, arrays, or buffers in constant time to prevent timing attacks.
149
- *
150
- * @param {string | Buffer | Uint8Array} a - First value to compare
151
- * @param {string | Buffer | Uint8Array} b - Second value to compare
152
- * @returns {boolean} True if values are equal
153
- */
154
- function safeCompare(a, b) {
155
- if (typeof a === 'string' && typeof b === 'string') {
156
- // Convert strings to buffers
157
- const bufA = Buffer.from(a);
158
- const bufB = Buffer.from(b);
159
- // Compare lengths first (not constant time, but prevents timing attack on contents)
160
- if (bufA.length !== bufB.length)
161
- return false;
162
- return (0, crypto_1.timingSafeEqual)(bufA, bufB);
163
- }
164
- else if ((a instanceof Buffer || a instanceof Uint8Array) && (b instanceof Buffer || b instanceof Uint8Array)) {
165
- // Compare lengths first
166
- if (a.length !== b.length)
167
- return false;
168
- return (0, crypto_1.timingSafeEqual)(a instanceof Buffer ? a : Buffer.from(a), b instanceof Buffer ? b : Buffer.from(b));
169
- }
170
- throw new Error('Cannot compare: inputs must be strings, Buffers, or Uint8Arrays');
171
- }
172
- // Promisified version of scrypt for key derivation
173
- const scryptAsync = (0, util_1.promisify)(crypto_1.scrypt);
174
- /**
175
- * Encrypts data using a symmetric key with secure defaults (AES-256-GCM).
176
- *
177
- * @param {string | Buffer} data - Data to encrypt
178
- * @param {string | Buffer} key - Encryption key or passphrase
179
- * @param {EncryptionOptions} [options] - Encryption options
180
- * @returns {Promise<EncryptionResult>} Encrypted data with metadata
181
- */
182
- async function encrypt(data, key, options = {}) {
183
- const algorithm = options.algorithm || 'aes-256-gcm';
184
- const inputEncoding = options.inputEncoding || 'utf8';
185
- const outputEncoding = options.outputEncoding || 'hex';
186
- // Generate a random IV
187
- const iv = (0, crypto_1.randomBytes)(16);
188
- // Derive key using scrypt if key is a string (passphrase)
189
- const derivedKey = typeof key === 'string' ? await scryptAsync(key, iv.slice(0, 8), 32) : key;
190
- // Create cipher
191
- const cipher = (0, crypto_1.createCipheriv)(algorithm, derivedKey, iv);
192
- // Encrypt the data
193
- let ciphertext;
194
- if (typeof data === 'string') {
195
- ciphertext = cipher.update(data, inputEncoding, outputEncoding);
196
- ciphertext += cipher.final(outputEncoding);
197
- }
198
- else {
199
- const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
200
- ciphertext = outputEncoding ? encrypted.toString(outputEncoding) : encrypted;
201
- }
202
- // Get authentication tag if using GCM mode
203
- const authTag = algorithm.includes('gcm') ? cipher.getAuthTag() : undefined;
204
- return {
205
- ciphertext,
206
- iv,
207
- authTag,
208
- algorithm
209
- };
210
- }
211
- /**
212
- * Decrypts data that was encrypted with the encrypt function.
213
- *
214
- * @param {EncryptionResult} encryptedData - The encrypted data and metadata
215
- * @param {string | Buffer} key - Decryption key or passphrase
216
- * @param {DecryptionOptions} [options] - Decryption options
217
- * @returns {Promise<string | Buffer>} Decrypted data
218
- */
219
- async function decrypt(encryptedData, key, options = {}) {
220
- const algorithm = options.algorithm || encryptedData.algorithm || 'aes-256-gcm';
221
- const inputEncoding = options.inputEncoding || 'hex';
222
- const outputEncoding = options.outputEncoding || 'utf8';
223
- // Derive key using scrypt if key is a string (passphrase)
224
- const derivedKey = typeof key === 'string' ? await scryptAsync(key, encryptedData.iv.slice(0, 8), 32) : key;
225
- // Create decipher
226
- const decipher = (0, crypto_1.createDecipheriv)(algorithm, derivedKey, encryptedData.iv);
227
- // Set auth tag if using GCM mode
228
- if (encryptedData.authTag && algorithm.includes('gcm')) {
229
- decipher.setAuthTag(encryptedData.authTag);
230
- }
231
- // Decrypt the data
232
- let decrypted;
233
- if (typeof encryptedData.ciphertext === 'string') {
234
- decrypted = decipher.update(encryptedData.ciphertext, inputEncoding, outputEncoding);
235
- decrypted += decipher.final(outputEncoding);
236
- }
237
- else {
238
- const result = Buffer.concat([decipher.update(encryptedData.ciphertext), decipher.final()]);
239
- decrypted = outputEncoding ? result.toString(outputEncoding) : result;
240
- }
241
- return decrypted;
242
- }
243
- /**
244
- * Creates a signed token with an expiration time and payload.
245
- *
246
- * @param {object} payload - Data to include in the token
247
- * @param {string} secret - Secret key for signing
248
- * @param {number} [expiresInSeconds=3600] - Token expiration in seconds
249
- * @returns {string} Signed token string
250
- */
251
- function createSignedToken(payload, secret, expiresInSeconds = 3600) {
252
- // Create payload with expiration
253
- const tokenPayload = {
254
- ...payload,
255
- exp: Math.floor(Date.now() / 1000) + expiresInSeconds
256
- };
257
- // Convert to string
258
- const payloadStr = JSON.stringify(tokenPayload);
259
- // Base64 encode the payload
260
- const base64Payload = Buffer.from(payloadStr).toString('base64url');
261
- // Create signature
262
- const signature = hmac('sha256', base64Payload, secret, 'base64url');
263
- // Combine payload and signature
264
- return `${base64Payload}.${signature}`;
265
- }
266
- /**
267
- * Verifies and decodes a signed token.
268
- *
269
- * @param {string} token - The token to verify
270
- * @param {string} secret - Secret key for verification
271
- * @returns {object | null} Decoded payload if valid, null if invalid
272
- */
273
- function verifySignedToken(token, secret) {
274
- try {
275
- // Split token into parts
276
- const [payloadB64, signature] = token.split('.');
277
- if (!payloadB64 || !signature)
278
- return null;
279
- // Verify signature
280
- const expectedSignature = hmac('sha256', payloadB64, secret, 'base64url');
281
- if (!safeCompare(signature, expectedSignature))
282
- return null;
283
- // Decode payload
284
- const payloadStr = Buffer.from(payloadB64, 'base64url').toString('utf8');
285
- const payload = JSON.parse(payloadStr);
286
- // Check expiration
287
- if (payload.exp && payload.exp < Math.floor(Date.now() / 1000)) {
288
- return null;
289
- }
290
- return payload;
291
- }
292
- catch {
293
- return null;
294
- }
295
- }
@@ -1,158 +0,0 @@
1
- /**
2
- * Format options for the formatDate function
3
- */
4
- export interface DateFormatOptions {
5
- /** Date format pattern (default: 'yyyy-MM-dd') */
6
- format?: string;
7
- /** Locale to use for formatting (default: system locale) */
8
- locale?: string | string[];
9
- /** Time zone to use (default: system time zone) */
10
- timeZone?: string;
11
- }
12
- /**
13
- * Format a date according to the specified format pattern.
14
- *
15
- * @param date - Date to format
16
- * @param options - Formatting options
17
- * @returns Formatted date string
18
- *
19
- * @example
20
- * ```typescript
21
- * // Format as ISO date
22
- * formatDate(new Date(), { format: 'yyyy-MM-dd' }); // '2023-05-15'
23
- *
24
- * // Format with time
25
- * formatDate(new Date(), { format: 'yyyy-MM-dd HH:mm:ss' }); // '2023-05-15 14:30:22'
26
- *
27
- * // Format with locale
28
- * formatDate(new Date(), { format: 'PPPP', locale: 'fr-FR' }); // 'lundi 15 mai 2023'
29
- * ```
30
- */
31
- export declare function formatDate(date: Date | number, options?: DateFormatOptions): string;
32
- /**
33
- * Format a date as relative time (e.g., "5 minutes ago", "in 3 days").
34
- *
35
- * @param date - Date to format
36
- * @param now - Reference date (default: current time)
37
- * @param locale - Locale to use for formatting
38
- * @returns Formatted relative time string
39
- */
40
- export declare function formatRelativeTime(date: Date | number, now?: Date | number, locale?: string | string[]): string;
41
- /**
42
- * Parse a date string or timestamp into a Date object.
43
- *
44
- * @param input - Date string or timestamp to parse
45
- * @param fallback - Fallback date if parsing fails
46
- * @returns Parsed Date object or fallback
47
- *
48
- * @example
49
- * ```typescript
50
- * parseDate('2023-05-15'); // Date object for May 15, 2023
51
- * parseDate('invalid', new Date()); // Returns current date as fallback
52
- * ```
53
- */
54
- export declare function parseDate(input: string | number, fallback?: Date): Date | null;
55
- /**
56
- * Calculate the difference between two dates in the specified unit.
57
- *
58
- * @param date1 - First date
59
- * @param date2 - Second date (default: current time)
60
- * @param unit - Unit of time for the difference
61
- * @returns Difference in the specified unit
62
- *
63
- * @example
64
- * ```typescript
65
- * // Get difference in days
66
- * dateDiff(new Date('2023-05-15'), new Date('2023-05-10'), 'days'); // 5
67
- *
68
- * // Get difference in hours
69
- * dateDiff(new Date('2023-05-15T10:00:00'), new Date('2023-05-15T06:00:00'), 'hours'); // 4
70
- * ```
71
- */
72
- export declare function dateDiff(date1: Date | number, date2?: Date | number, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years'): number;
73
- /**
74
- * Add a specified amount of time to a date.
75
- *
76
- * @param date - Base date
77
- * @param amount - Amount to add (can be negative)
78
- * @param unit - Unit of time to add
79
- * @returns New date with the addition
80
- *
81
- * @example
82
- * ```typescript
83
- * // Add 5 days
84
- * addToDate(new Date('2023-05-15'), 5, 'days'); // Date for May 20, 2023
85
- *
86
- * // Subtract 2 hours
87
- * addToDate(new Date('2023-05-15T10:00:00'), -2, 'hours'); // Date for May 15, 2023 08:00:00
88
- * ```
89
- */
90
- export declare function addToDate(date: Date | number, amount: number, unit: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years'): Date;
91
- /**
92
- * Get the start of a time period containing the specified date.
93
- *
94
- * @param date - Date to get the start from
95
- * @param unit - Time unit
96
- * @returns Date object representing the start of the time unit
97
- *
98
- * @example
99
- * ```typescript
100
- * // Get start of day (midnight)
101
- * startOf(new Date('2023-05-15T14:30:00'), 'day'); // Date for May 15, 2023 00:00:00
102
- *
103
- * // Get start of month
104
- * startOf(new Date('2023-05-15'), 'month'); // Date for May 1, 2023
105
- * ```
106
- */
107
- export declare function startOf(date: Date | number, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'): Date;
108
- /**
109
- * Get the end of a time period containing the specified date.
110
- *
111
- * @param date - Date to get the end from
112
- * @param unit - Time unit
113
- * @returns Date object representing the end of the time unit
114
- *
115
- * @example
116
- * ```typescript
117
- * // Get end of day (23:59:59.999)
118
- * endOf(new Date('2023-05-15T14:30:00'), 'day'); // Date for May 15, 2023 23:59:59.999
119
- *
120
- * // Get end of month
121
- * endOf(new Date('2023-05-15'), 'month'); // Date for May 31, 2023 23:59:59.999
122
- * ```
123
- */
124
- export declare function endOf(date: Date | number, unit: 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'): Date;
125
- /**
126
- * Check if a date is between two other dates.
127
- *
128
- * @param date - Date to check
129
- * @param start - Start date of the range
130
- * @param end - End date of the range
131
- * @param inclusive - Whether the comparison should be inclusive of start/end
132
- * @returns True if the date is within the range
133
- *
134
- * @example
135
- * ```typescript
136
- * const date = new Date('2023-05-15');
137
- * const start = new Date('2023-05-10');
138
- * const end = new Date('2023-05-20');
139
- *
140
- * isBetween(date, start, end); // true
141
- * ```
142
- */
143
- export declare function isBetween(date: Date | number, start: Date | number, end: Date | number, inclusive?: boolean): boolean;
144
- /**
145
- * Check if a year is a leap year.
146
- *
147
- * @param year - Year to check (or date object)
148
- * @returns True if the year is a leap year
149
- */
150
- export declare function isLeapYear(year: number | Date): boolean;
151
- /**
152
- * Get the number of days in a month.
153
- *
154
- * @param year - Year
155
- * @param month - Month (0-11)
156
- * @returns Number of days in the month
157
- */
158
- export declare function daysInMonth(year: number | Date, month?: number): number;