@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.
- package/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
package/date/index.d.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
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
|
+
/**
|
|
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
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
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 (Number.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
|
+
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");
|
|
325
|
+
|
|
326
|
+
export { addToDate, dateDiff, daysInMonth, endOf, formatDate, formatDuration, formatRelativeTime, getDateFromDuration, isBetween, isLeapYear, parseDate, parseDuration, startOf };
|