ts-time-utils 1.1.0 → 3.0.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.
- package/README.md +567 -12
- package/dist/calculate.d.ts +7 -2
- package/dist/calculate.d.ts.map +1 -1
- package/dist/calculate.js +13 -3
- package/dist/calendar.d.ts +103 -0
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +224 -0
- package/dist/chain.d.ts +269 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +422 -0
- package/dist/compare.d.ts +217 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +417 -0
- package/dist/cron.d.ts +82 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +294 -0
- package/dist/esm/calculate.d.ts +7 -2
- package/dist/esm/calculate.d.ts.map +1 -1
- package/dist/esm/calculate.js +13 -3
- package/dist/esm/calendar.d.ts +103 -0
- package/dist/esm/calendar.d.ts.map +1 -1
- package/dist/esm/calendar.js +224 -0
- package/dist/esm/chain.d.ts +269 -0
- package/dist/esm/chain.d.ts.map +1 -0
- package/dist/esm/chain.js +422 -0
- package/dist/esm/compare.d.ts +217 -0
- package/dist/esm/compare.d.ts.map +1 -0
- package/dist/esm/compare.js +417 -0
- package/dist/esm/cron.d.ts +82 -0
- package/dist/esm/cron.d.ts.map +1 -0
- package/dist/esm/cron.js +294 -0
- package/dist/esm/fiscal.d.ts +195 -0
- package/dist/esm/fiscal.d.ts.map +1 -0
- package/dist/esm/fiscal.js +295 -0
- package/dist/esm/format.d.ts +65 -0
- package/dist/esm/format.d.ts.map +1 -1
- package/dist/esm/format.js +202 -0
- package/dist/esm/holidays.d.ts +62 -0
- package/dist/esm/holidays.d.ts.map +1 -0
- package/dist/esm/holidays.js +793 -0
- package/dist/esm/index.d.ts +18 -6
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +20 -6
- package/dist/esm/iterate.d.ts +212 -0
- package/dist/esm/iterate.d.ts.map +1 -0
- package/dist/esm/iterate.js +409 -0
- package/dist/esm/parse.d.ts +45 -0
- package/dist/esm/parse.d.ts.map +1 -1
- package/dist/esm/parse.js +207 -0
- package/dist/esm/plugins.d.ts +129 -0
- package/dist/esm/plugins.d.ts.map +1 -0
- package/dist/esm/plugins.js +173 -0
- package/dist/esm/timezone.d.ts +52 -0
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +171 -0
- package/dist/esm/validate.d.ts +51 -0
- package/dist/esm/validate.d.ts.map +1 -1
- package/dist/esm/validate.js +92 -0
- package/dist/esm/workingHours.d.ts +70 -0
- package/dist/esm/workingHours.d.ts.map +1 -1
- package/dist/esm/workingHours.js +161 -0
- package/dist/fiscal.d.ts +195 -0
- package/dist/fiscal.d.ts.map +1 -0
- package/dist/fiscal.js +295 -0
- package/dist/format.d.ts +65 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +202 -0
- package/dist/holidays.d.ts +62 -0
- package/dist/holidays.d.ts.map +1 -0
- package/dist/holidays.js +793 -0
- package/dist/index.d.ts +18 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -6
- package/dist/iterate.d.ts +212 -0
- package/dist/iterate.d.ts.map +1 -0
- package/dist/iterate.js +409 -0
- package/dist/parse.d.ts +45 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +207 -0
- package/dist/plugins.d.ts +129 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +173 -0
- package/dist/timezone.d.ts +52 -0
- package/dist/timezone.d.ts.map +1 -1
- package/dist/timezone.js +171 -0
- package/dist/validate.d.ts +51 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +92 -0
- package/dist/workingHours.d.ts +70 -0
- package/dist/workingHours.d.ts.map +1 -1
- package/dist/workingHours.js +161 -0
- package/package.json +40 -1
package/dist/validate.js
CHANGED
|
@@ -106,3 +106,95 @@ export function isValidISOString(dateString) {
|
|
|
106
106
|
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/;
|
|
107
107
|
return isoRegex.test(dateString) && isValidDate(dateString);
|
|
108
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if two dates are in the same week (ISO week, Monday-Sunday)
|
|
111
|
+
* @param date1 - first date
|
|
112
|
+
* @param date2 - second date
|
|
113
|
+
*/
|
|
114
|
+
export function isSameWeek(date1, date2) {
|
|
115
|
+
const getWeekStart = (d) => {
|
|
116
|
+
const date = new Date(d);
|
|
117
|
+
const day = date.getDay();
|
|
118
|
+
const diff = date.getDate() - day + (day === 0 ? -6 : 1); // Adjust for Monday start
|
|
119
|
+
date.setDate(diff);
|
|
120
|
+
date.setHours(0, 0, 0, 0);
|
|
121
|
+
return date;
|
|
122
|
+
};
|
|
123
|
+
return getWeekStart(date1).getTime() === getWeekStart(date2).getTime();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Check if two dates are in the same month
|
|
127
|
+
* @param date1 - first date
|
|
128
|
+
* @param date2 - second date
|
|
129
|
+
*/
|
|
130
|
+
export function isSameMonth(date1, date2) {
|
|
131
|
+
return (date1.getMonth() === date2.getMonth() &&
|
|
132
|
+
date1.getFullYear() === date2.getFullYear());
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if two dates are in the same year
|
|
136
|
+
* @param date1 - first date
|
|
137
|
+
* @param date2 - second date
|
|
138
|
+
*/
|
|
139
|
+
export function isSameYear(date1, date2) {
|
|
140
|
+
return date1.getFullYear() === date2.getFullYear();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Check if a date is in the current week
|
|
144
|
+
* @param date - date to check
|
|
145
|
+
*/
|
|
146
|
+
export function isThisWeek(date) {
|
|
147
|
+
return isSameWeek(date, new Date());
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if a date is in the current month
|
|
151
|
+
* @param date - date to check
|
|
152
|
+
*/
|
|
153
|
+
export function isThisMonth(date) {
|
|
154
|
+
return isSameMonth(date, new Date());
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Check if a date is in the current year
|
|
158
|
+
* @param date - date to check
|
|
159
|
+
*/
|
|
160
|
+
export function isThisYear(date) {
|
|
161
|
+
return isSameYear(date, new Date());
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Check if a date is a business day (weekday, optionally excluding holidays)
|
|
165
|
+
* @param date - date to check
|
|
166
|
+
* @param holidays - optional array of holiday dates to exclude
|
|
167
|
+
*/
|
|
168
|
+
export function isBusinessDay(date, holidays = []) {
|
|
169
|
+
if (isWeekend(date))
|
|
170
|
+
return false;
|
|
171
|
+
return !holidays.some(holiday => isSameDay(date, holiday));
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Check if a date is in the last N days (including today)
|
|
175
|
+
* @param date - date to check
|
|
176
|
+
* @param n - number of days
|
|
177
|
+
*/
|
|
178
|
+
export function isInLastNDays(date, n) {
|
|
179
|
+
const now = new Date();
|
|
180
|
+
const nDaysAgo = new Date(now);
|
|
181
|
+
nDaysAgo.setDate(nDaysAgo.getDate() - n);
|
|
182
|
+
nDaysAgo.setHours(0, 0, 0, 0);
|
|
183
|
+
const endOfToday = new Date(now);
|
|
184
|
+
endOfToday.setHours(23, 59, 59, 999);
|
|
185
|
+
return date >= nDaysAgo && date <= endOfToday;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Check if a date is in the next N days (including today)
|
|
189
|
+
* @param date - date to check
|
|
190
|
+
* @param n - number of days
|
|
191
|
+
*/
|
|
192
|
+
export function isInNextNDays(date, n) {
|
|
193
|
+
const now = new Date();
|
|
194
|
+
const startOfToday = new Date(now);
|
|
195
|
+
startOfToday.setHours(0, 0, 0, 0);
|
|
196
|
+
const nDaysFromNow = new Date(now);
|
|
197
|
+
nDaysFromNow.setDate(nDaysFromNow.getDate() + n);
|
|
198
|
+
nDaysFromNow.setHours(23, 59, 59, 999);
|
|
199
|
+
return date >= startOfToday && date <= nDaysFromNow;
|
|
200
|
+
}
|
package/dist/workingHours.d.ts
CHANGED
|
@@ -13,4 +13,74 @@ export declare function nextWorkingTime(date: Date, config?: WorkingHoursConfig)
|
|
|
13
13
|
export declare function workingTimeBetween(start: Date, end: Date, config?: WorkingHoursConfig): number;
|
|
14
14
|
/** Advance by working hours amount (simple iterative approach) */
|
|
15
15
|
export declare function addWorkingHours(start: Date, hours: number, config?: WorkingHoursConfig): Date;
|
|
16
|
+
/**
|
|
17
|
+
* Add working days to a date
|
|
18
|
+
* @param start - start date
|
|
19
|
+
* @param days - number of working days to add
|
|
20
|
+
* @param config - working hours configuration
|
|
21
|
+
*/
|
|
22
|
+
export declare function addWorkingDays(start: Date, days: number, config?: WorkingHoursConfig): Date;
|
|
23
|
+
/**
|
|
24
|
+
* Subtract working days from a date
|
|
25
|
+
* @param start - start date
|
|
26
|
+
* @param days - number of working days to subtract
|
|
27
|
+
* @param config - working hours configuration
|
|
28
|
+
*/
|
|
29
|
+
export declare function subtractWorkingDays(start: Date, days: number, config?: WorkingHoursConfig): Date;
|
|
30
|
+
/**
|
|
31
|
+
* Get the next working day from a given date
|
|
32
|
+
* @param date - start date
|
|
33
|
+
* @param config - working hours configuration
|
|
34
|
+
*/
|
|
35
|
+
export declare function getNextWorkingDay(date: Date, config?: WorkingHoursConfig): Date;
|
|
36
|
+
/**
|
|
37
|
+
* Get the previous working day from a given date
|
|
38
|
+
* @param date - start date
|
|
39
|
+
* @param config - working hours configuration
|
|
40
|
+
*/
|
|
41
|
+
export declare function getPreviousWorkingDay(date: Date, config?: WorkingHoursConfig): Date;
|
|
42
|
+
/**
|
|
43
|
+
* Get the number of working days in a month
|
|
44
|
+
* @param year - year
|
|
45
|
+
* @param month - month (0-11)
|
|
46
|
+
* @param config - working hours configuration
|
|
47
|
+
*/
|
|
48
|
+
export declare function getWorkingDaysInMonth(year: number, month: number, config?: WorkingHoursConfig): number;
|
|
49
|
+
/**
|
|
50
|
+
* Get all working days in a month as an array of dates
|
|
51
|
+
* @param year - year
|
|
52
|
+
* @param month - month (0-11)
|
|
53
|
+
* @param config - working hours configuration
|
|
54
|
+
*/
|
|
55
|
+
export declare function getWorkingDaysInMonthArray(year: number, month: number, config?: WorkingHoursConfig): Date[];
|
|
56
|
+
/**
|
|
57
|
+
* Get working days between two dates (inclusive)
|
|
58
|
+
* @param start - start date
|
|
59
|
+
* @param end - end date
|
|
60
|
+
* @param config - working hours configuration
|
|
61
|
+
*/
|
|
62
|
+
export declare function workingDaysBetween(start: Date, end: Date, config?: WorkingHoursConfig): number;
|
|
63
|
+
/**
|
|
64
|
+
* Check if a date is during work break
|
|
65
|
+
* @param date - date to check
|
|
66
|
+
* @param config - working hours configuration
|
|
67
|
+
*/
|
|
68
|
+
export declare function isBreakTime(date: Date, config?: WorkingHoursConfig): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Get the start of the work day for a given date
|
|
71
|
+
* @param date - date to get work start for
|
|
72
|
+
* @param config - working hours configuration
|
|
73
|
+
*/
|
|
74
|
+
export declare function getWorkDayStart(date: Date, config?: WorkingHoursConfig): Date;
|
|
75
|
+
/**
|
|
76
|
+
* Get the end of the work day for a given date
|
|
77
|
+
* @param date - date to get work end for
|
|
78
|
+
* @param config - working hours configuration
|
|
79
|
+
*/
|
|
80
|
+
export declare function getWorkDayEnd(date: Date, config?: WorkingHoursConfig): Date;
|
|
81
|
+
/**
|
|
82
|
+
* Get the total working hours per day (excluding breaks)
|
|
83
|
+
* @param config - working hours configuration
|
|
84
|
+
*/
|
|
85
|
+
export declare function getWorkingHoursPerDay(config?: WorkingHoursConfig): number;
|
|
16
86
|
//# sourceMappingURL=workingHours.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workingHours.d.ts","sourceRoot":"","sources":["../src/workingHours.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD,e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;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,kBAA0C,GAAG,IAAI,CAIlG;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,GAAE,kBAA0C,GAAG,MAAM,CAUhG"}
|
package/dist/workingHours.js
CHANGED
|
@@ -107,3 +107,164 @@ export function addWorkingHours(start, hours, config = DEFAULT_WORKING_HOURS) {
|
|
|
107
107
|
}
|
|
108
108
|
return cursor;
|
|
109
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Add working days to a date
|
|
112
|
+
* @param start - start date
|
|
113
|
+
* @param days - number of working days to add
|
|
114
|
+
* @param config - working hours configuration
|
|
115
|
+
*/
|
|
116
|
+
export function addWorkingDays(start, days, config = DEFAULT_WORKING_HOURS) {
|
|
117
|
+
if (days === 0)
|
|
118
|
+
return new Date(start);
|
|
119
|
+
const result = new Date(start);
|
|
120
|
+
const direction = days > 0 ? 1 : -1;
|
|
121
|
+
let remaining = Math.abs(days);
|
|
122
|
+
while (remaining > 0) {
|
|
123
|
+
result.setDate(result.getDate() + direction);
|
|
124
|
+
if (isWorkingDay(result, config)) {
|
|
125
|
+
remaining--;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Subtract working days from a date
|
|
132
|
+
* @param start - start date
|
|
133
|
+
* @param days - number of working days to subtract
|
|
134
|
+
* @param config - working hours configuration
|
|
135
|
+
*/
|
|
136
|
+
export function subtractWorkingDays(start, days, config = DEFAULT_WORKING_HOURS) {
|
|
137
|
+
return addWorkingDays(start, -days, config);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get the next working day from a given date
|
|
141
|
+
* @param date - start date
|
|
142
|
+
* @param config - working hours configuration
|
|
143
|
+
*/
|
|
144
|
+
export function getNextWorkingDay(date, config = DEFAULT_WORKING_HOURS) {
|
|
145
|
+
const result = new Date(date);
|
|
146
|
+
result.setDate(result.getDate() + 1);
|
|
147
|
+
while (!isWorkingDay(result, config)) {
|
|
148
|
+
result.setDate(result.getDate() + 1);
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get the previous working day from a given date
|
|
154
|
+
* @param date - start date
|
|
155
|
+
* @param config - working hours configuration
|
|
156
|
+
*/
|
|
157
|
+
export function getPreviousWorkingDay(date, config = DEFAULT_WORKING_HOURS) {
|
|
158
|
+
const result = new Date(date);
|
|
159
|
+
result.setDate(result.getDate() - 1);
|
|
160
|
+
while (!isWorkingDay(result, config)) {
|
|
161
|
+
result.setDate(result.getDate() - 1);
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get the number of working days in a month
|
|
167
|
+
* @param year - year
|
|
168
|
+
* @param month - month (0-11)
|
|
169
|
+
* @param config - working hours configuration
|
|
170
|
+
*/
|
|
171
|
+
export function getWorkingDaysInMonth(year, month, config = DEFAULT_WORKING_HOURS) {
|
|
172
|
+
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
|
173
|
+
let count = 0;
|
|
174
|
+
for (let day = 1; day <= daysInMonth; day++) {
|
|
175
|
+
const date = new Date(year, month, day);
|
|
176
|
+
if (isWorkingDay(date, config)) {
|
|
177
|
+
count++;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return count;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Get all working days in a month as an array of dates
|
|
184
|
+
* @param year - year
|
|
185
|
+
* @param month - month (0-11)
|
|
186
|
+
* @param config - working hours configuration
|
|
187
|
+
*/
|
|
188
|
+
export function getWorkingDaysInMonthArray(year, month, config = DEFAULT_WORKING_HOURS) {
|
|
189
|
+
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
|
190
|
+
const workingDays = [];
|
|
191
|
+
for (let day = 1; day <= daysInMonth; day++) {
|
|
192
|
+
const date = new Date(year, month, day);
|
|
193
|
+
if (isWorkingDay(date, config)) {
|
|
194
|
+
workingDays.push(date);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return workingDays;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get working days between two dates (inclusive)
|
|
201
|
+
* @param start - start date
|
|
202
|
+
* @param end - end date
|
|
203
|
+
* @param config - working hours configuration
|
|
204
|
+
*/
|
|
205
|
+
export function workingDaysBetween(start, end, config = DEFAULT_WORKING_HOURS) {
|
|
206
|
+
if (end < start)
|
|
207
|
+
return 0;
|
|
208
|
+
let count = 0;
|
|
209
|
+
const current = new Date(start);
|
|
210
|
+
current.setHours(0, 0, 0, 0);
|
|
211
|
+
const endDate = new Date(end);
|
|
212
|
+
endDate.setHours(0, 0, 0, 0);
|
|
213
|
+
while (current <= endDate) {
|
|
214
|
+
if (isWorkingDay(current, config)) {
|
|
215
|
+
count++;
|
|
216
|
+
}
|
|
217
|
+
current.setDate(current.getDate() + 1);
|
|
218
|
+
}
|
|
219
|
+
return count;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Check if a date is during work break
|
|
223
|
+
* @param date - date to check
|
|
224
|
+
* @param config - working hours configuration
|
|
225
|
+
*/
|
|
226
|
+
export function isBreakTime(date, config = DEFAULT_WORKING_HOURS) {
|
|
227
|
+
if (!isWorkingDay(date, config))
|
|
228
|
+
return false;
|
|
229
|
+
if (!config.breaks || config.breaks.length === 0)
|
|
230
|
+
return false;
|
|
231
|
+
const h = toHourFraction(date);
|
|
232
|
+
for (const b of config.breaks) {
|
|
233
|
+
if (h >= b.start && h < b.end)
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get the start of the work day for a given date
|
|
240
|
+
* @param date - date to get work start for
|
|
241
|
+
* @param config - working hours configuration
|
|
242
|
+
*/
|
|
243
|
+
export function getWorkDayStart(date, config = DEFAULT_WORKING_HOURS) {
|
|
244
|
+
const result = new Date(date);
|
|
245
|
+
result.setHours(config.hours.start, 0, 0, 0);
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Get the end of the work day for a given date
|
|
250
|
+
* @param date - date to get work end for
|
|
251
|
+
* @param config - working hours configuration
|
|
252
|
+
*/
|
|
253
|
+
export function getWorkDayEnd(date, config = DEFAULT_WORKING_HOURS) {
|
|
254
|
+
const result = new Date(date);
|
|
255
|
+
result.setHours(config.hours.end, 0, 0, 0);
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Get the total working hours per day (excluding breaks)
|
|
260
|
+
* @param config - working hours configuration
|
|
261
|
+
*/
|
|
262
|
+
export function getWorkingHoursPerDay(config = DEFAULT_WORKING_HOURS) {
|
|
263
|
+
let hours = config.hours.end - config.hours.start;
|
|
264
|
+
if (config.breaks) {
|
|
265
|
+
for (const b of config.breaks) {
|
|
266
|
+
hours -= (b.end - b.start);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return hours;
|
|
270
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-time-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A comprehensive TypeScript utility library for time, dates, durations, and calendar operations with full tree-shaking support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -106,6 +106,41 @@
|
|
|
106
106
|
"import": "./dist/esm/naturalLanguage.js",
|
|
107
107
|
"require": "./dist/naturalLanguage.js",
|
|
108
108
|
"types": "./dist/naturalLanguage.d.ts"
|
|
109
|
+
},
|
|
110
|
+
"./cron": {
|
|
111
|
+
"import": "./dist/esm/cron.js",
|
|
112
|
+
"require": "./dist/cron.js",
|
|
113
|
+
"types": "./dist/cron.d.ts"
|
|
114
|
+
},
|
|
115
|
+
"./fiscal": {
|
|
116
|
+
"import": "./dist/esm/fiscal.js",
|
|
117
|
+
"require": "./dist/fiscal.js",
|
|
118
|
+
"types": "./dist/fiscal.d.ts"
|
|
119
|
+
},
|
|
120
|
+
"./compare": {
|
|
121
|
+
"import": "./dist/esm/compare.js",
|
|
122
|
+
"require": "./dist/compare.js",
|
|
123
|
+
"types": "./dist/compare.d.ts"
|
|
124
|
+
},
|
|
125
|
+
"./iterate": {
|
|
126
|
+
"import": "./dist/esm/iterate.js",
|
|
127
|
+
"require": "./dist/iterate.js",
|
|
128
|
+
"types": "./dist/iterate.d.ts"
|
|
129
|
+
},
|
|
130
|
+
"./holidays": {
|
|
131
|
+
"import": "./dist/esm/holidays.js",
|
|
132
|
+
"require": "./dist/holidays.js",
|
|
133
|
+
"types": "./dist/holidays.d.ts"
|
|
134
|
+
},
|
|
135
|
+
"./chain": {
|
|
136
|
+
"import": "./dist/esm/chain.js",
|
|
137
|
+
"require": "./dist/chain.js",
|
|
138
|
+
"types": "./dist/chain.d.ts"
|
|
139
|
+
},
|
|
140
|
+
"./plugins": {
|
|
141
|
+
"import": "./dist/esm/plugins.js",
|
|
142
|
+
"require": "./dist/plugins.js",
|
|
143
|
+
"types": "./dist/plugins.d.ts"
|
|
109
144
|
}
|
|
110
145
|
},
|
|
111
146
|
"files": [
|
|
@@ -118,6 +153,8 @@
|
|
|
118
153
|
"test": "vitest run",
|
|
119
154
|
"test:watch": "vitest",
|
|
120
155
|
"lint": "eslint src --ext .ts",
|
|
156
|
+
"size": "size-limit",
|
|
157
|
+
"size:check": "size-limit --json > docs/bundle-sizes.json",
|
|
121
158
|
"prepublishOnly": "npm run build"
|
|
122
159
|
},
|
|
123
160
|
"keywords": [
|
|
@@ -152,6 +189,8 @@
|
|
|
152
189
|
"author": "",
|
|
153
190
|
"license": "MIT",
|
|
154
191
|
"devDependencies": {
|
|
192
|
+
"@size-limit/file": "^12.0.0",
|
|
193
|
+
"size-limit": "^12.0.0",
|
|
155
194
|
"typescript": "^5.9.2",
|
|
156
195
|
"vitest": "^3.2.4"
|
|
157
196
|
},
|