ts-time-utils 2.0.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.
@@ -0,0 +1,422 @@
1
+ /**
2
+ * Fluent chain API for ts-time-utils
3
+ *
4
+ * Provides a chainable interface for date operations:
5
+ * ```ts
6
+ * chain(new Date())
7
+ * .add(1, 'day')
8
+ * .startOf('month')
9
+ * .format('YYYY-MM-DD')
10
+ * ```
11
+ */
12
+ import { addTime, subtractTime, startOf, endOf, differenceInUnits, isBetween } from './calculate.js';
13
+ import { formatDate, formatDuration, timeAgo, formatTime, formatCalendarDate, formatOrdinal } from './format.js';
14
+ import { isValidDate, isToday, isYesterday, isTomorrow, isPast, isFuture, isWeekend, isWeekday, isSameDay, isSameWeek, isSameMonth, isSameYear, isThisWeek, isThisMonth, isThisYear, isBusinessDay, isLeapYear } from './validate.js';
15
+ /**
16
+ * Immutable chainable date wrapper
17
+ * All transformation methods return a new ChainedDate instance
18
+ */
19
+ export class ChainedDate {
20
+ constructor(date = new Date()) {
21
+ if (date instanceof Date) {
22
+ this._date = new Date(date.getTime());
23
+ }
24
+ else if (typeof date === 'number') {
25
+ this._date = new Date(date);
26
+ }
27
+ else {
28
+ this._date = new Date(date);
29
+ }
30
+ }
31
+ // ============ Transformations (return new ChainedDate) ============
32
+ /**
33
+ * Add time to the date
34
+ * @example chain(date).add(1, 'day').add(2, 'hours')
35
+ */
36
+ add(amount, unit) {
37
+ return new ChainedDate(addTime(this._date, amount, unit));
38
+ }
39
+ /**
40
+ * Subtract time from the date
41
+ * @example chain(date).subtract(1, 'week')
42
+ */
43
+ subtract(amount, unit) {
44
+ return new ChainedDate(subtractTime(this._date, amount, unit));
45
+ }
46
+ /**
47
+ * Get the start of a time period
48
+ * @example chain(date).startOf('month')
49
+ */
50
+ startOf(unit) {
51
+ return new ChainedDate(startOf(this._date, unit));
52
+ }
53
+ /**
54
+ * Get the end of a time period
55
+ * @example chain(date).endOf('day')
56
+ */
57
+ endOf(unit) {
58
+ return new ChainedDate(endOf(this._date, unit));
59
+ }
60
+ /**
61
+ * Set specific date/time components
62
+ * @example chain(date).set({ year: 2025, month: 1 })
63
+ */
64
+ set(values) {
65
+ const d = new Date(this._date.getTime());
66
+ if (values.year !== undefined)
67
+ d.setFullYear(values.year);
68
+ if (values.month !== undefined)
69
+ d.setMonth(values.month - 1); // 1-indexed input
70
+ if (values.day !== undefined)
71
+ d.setDate(values.day);
72
+ if (values.hours !== undefined)
73
+ d.setHours(values.hours);
74
+ if (values.minutes !== undefined)
75
+ d.setMinutes(values.minutes);
76
+ if (values.seconds !== undefined)
77
+ d.setSeconds(values.seconds);
78
+ if (values.milliseconds !== undefined)
79
+ d.setMilliseconds(values.milliseconds);
80
+ return new ChainedDate(d);
81
+ }
82
+ /**
83
+ * Clone the ChainedDate
84
+ */
85
+ clone() {
86
+ return new ChainedDate(this._date);
87
+ }
88
+ // ============ Formatters (return string) ============
89
+ /**
90
+ * Format date using pattern string
91
+ * @example chain(date).format('YYYY-MM-DD') // "2025-01-15"
92
+ */
93
+ format(pattern) {
94
+ return formatDate(this._date, pattern);
95
+ }
96
+ /**
97
+ * Format time in 12h, 24h, or ISO format
98
+ * @example chain(date).formatTime('12h') // "2:30 PM"
99
+ */
100
+ formatTime(fmt = '24h') {
101
+ return formatTime(this._date, fmt);
102
+ }
103
+ /**
104
+ * Format as calendar date (Today, Yesterday, Monday, etc.)
105
+ * @example chain(date).calendar() // "Tomorrow"
106
+ */
107
+ calendar() {
108
+ return formatCalendarDate(this._date);
109
+ }
110
+ /**
111
+ * Get relative time string
112
+ * @example chain(pastDate).ago() // "3 hours ago"
113
+ */
114
+ ago(options) {
115
+ return timeAgo(this._date, options);
116
+ }
117
+ /**
118
+ * Get ISO string
119
+ */
120
+ toISOString() {
121
+ return this._date.toISOString();
122
+ }
123
+ /**
124
+ * Get locale string
125
+ */
126
+ toLocaleString(locale, options) {
127
+ return this._date.toLocaleString(locale, options);
128
+ }
129
+ /**
130
+ * Format day as ordinal
131
+ * @example chain(date).dayOrdinal() // "15th"
132
+ */
133
+ dayOrdinal() {
134
+ return formatOrdinal(this._date.getDate());
135
+ }
136
+ // ============ Comparisons (return boolean) ============
137
+ /**
138
+ * Check if date is valid
139
+ */
140
+ isValid() {
141
+ return isValidDate(this._date);
142
+ }
143
+ /**
144
+ * Check if date is today
145
+ */
146
+ isToday() {
147
+ return isToday(this._date);
148
+ }
149
+ /**
150
+ * Check if date is yesterday
151
+ */
152
+ isYesterday() {
153
+ return isYesterday(this._date);
154
+ }
155
+ /**
156
+ * Check if date is tomorrow
157
+ */
158
+ isTomorrow() {
159
+ return isTomorrow(this._date);
160
+ }
161
+ /**
162
+ * Check if date is in the past
163
+ */
164
+ isPast() {
165
+ return isPast(this._date);
166
+ }
167
+ /**
168
+ * Check if date is in the future
169
+ */
170
+ isFuture() {
171
+ return isFuture(this._date);
172
+ }
173
+ /**
174
+ * Check if date is a weekend
175
+ */
176
+ isWeekend() {
177
+ return isWeekend(this._date);
178
+ }
179
+ /**
180
+ * Check if date is a weekday
181
+ */
182
+ isWeekday() {
183
+ return isWeekday(this._date);
184
+ }
185
+ /**
186
+ * Check if date is in this week
187
+ */
188
+ isThisWeek() {
189
+ return isThisWeek(this._date);
190
+ }
191
+ /**
192
+ * Check if date is in this month
193
+ */
194
+ isThisMonth() {
195
+ return isThisMonth(this._date);
196
+ }
197
+ /**
198
+ * Check if date is in this year
199
+ */
200
+ isThisYear() {
201
+ return isThisYear(this._date);
202
+ }
203
+ /**
204
+ * Check if year is a leap year
205
+ */
206
+ isLeapYear() {
207
+ return isLeapYear(this._date.getFullYear());
208
+ }
209
+ /**
210
+ * Check if date is a business day
211
+ */
212
+ isBusinessDay(holidays) {
213
+ return isBusinessDay(this._date, holidays);
214
+ }
215
+ /**
216
+ * Check if date is same day as another
217
+ */
218
+ isSameDay(other) {
219
+ return isSameDay(this._date, toDate(other));
220
+ }
221
+ /**
222
+ * Check if date is same week as another
223
+ */
224
+ isSameWeek(other) {
225
+ return isSameWeek(this._date, toDate(other));
226
+ }
227
+ /**
228
+ * Check if date is same month as another
229
+ */
230
+ isSameMonth(other) {
231
+ return isSameMonth(this._date, toDate(other));
232
+ }
233
+ /**
234
+ * Check if date is same year as another
235
+ */
236
+ isSameYear(other) {
237
+ return isSameYear(this._date, toDate(other));
238
+ }
239
+ /**
240
+ * Check if date is before another
241
+ */
242
+ isBefore(other) {
243
+ return this._date.getTime() < toDate(other).getTime();
244
+ }
245
+ /**
246
+ * Check if date is after another
247
+ */
248
+ isAfter(other) {
249
+ return this._date.getTime() > toDate(other).getTime();
250
+ }
251
+ /**
252
+ * Check if date is between two dates
253
+ */
254
+ isBetween(start, end, inclusive) {
255
+ return isBetween(this._date, toDate(start), toDate(end), inclusive);
256
+ }
257
+ // ============ Getters (return number) ============
258
+ /**
259
+ * Get difference from another date
260
+ * @example chain(date).diff(other, 'days') // 5
261
+ */
262
+ diff(other, unit = 'milliseconds', precise = true) {
263
+ return differenceInUnits(this._date, toDate(other), unit, precise);
264
+ }
265
+ /**
266
+ * Get the timestamp (milliseconds since epoch)
267
+ */
268
+ valueOf() {
269
+ return this._date.getTime();
270
+ }
271
+ /**
272
+ * Get year
273
+ */
274
+ year() {
275
+ return this._date.getFullYear();
276
+ }
277
+ /**
278
+ * Get month (1-12)
279
+ */
280
+ month() {
281
+ return this._date.getMonth() + 1;
282
+ }
283
+ /**
284
+ * Get day of month (1-31)
285
+ */
286
+ day() {
287
+ return this._date.getDate();
288
+ }
289
+ /**
290
+ * Get day of week (0-6, 0=Sunday)
291
+ */
292
+ weekday() {
293
+ return this._date.getDay();
294
+ }
295
+ /**
296
+ * Get hours (0-23)
297
+ */
298
+ hours() {
299
+ return this._date.getHours();
300
+ }
301
+ /**
302
+ * Get minutes (0-59)
303
+ */
304
+ minutes() {
305
+ return this._date.getMinutes();
306
+ }
307
+ /**
308
+ * Get seconds (0-59)
309
+ */
310
+ seconds() {
311
+ return this._date.getSeconds();
312
+ }
313
+ /**
314
+ * Get milliseconds (0-999)
315
+ */
316
+ milliseconds() {
317
+ return this._date.getMilliseconds();
318
+ }
319
+ /**
320
+ * Get day of year (1-366)
321
+ */
322
+ dayOfYear() {
323
+ const start = new Date(this._date.getFullYear(), 0, 0);
324
+ const diff = this._date.getTime() - start.getTime();
325
+ const oneDay = 1000 * 60 * 60 * 24;
326
+ return Math.floor(diff / oneDay);
327
+ }
328
+ /**
329
+ * Get ISO week number (1-53)
330
+ */
331
+ week() {
332
+ const d = new Date(Date.UTC(this._date.getFullYear(), this._date.getMonth(), this._date.getDate()));
333
+ const dayNum = d.getUTCDay() || 7;
334
+ d.setUTCDate(d.getUTCDate() + 4 - dayNum);
335
+ const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
336
+ return Math.ceil((((d.getTime() - yearStart.getTime()) / 86400000) + 1) / 7);
337
+ }
338
+ /**
339
+ * Get quarter (1-4)
340
+ */
341
+ quarter() {
342
+ return Math.floor(this._date.getMonth() / 3) + 1;
343
+ }
344
+ /**
345
+ * Get days in month
346
+ */
347
+ daysInMonth() {
348
+ return new Date(this._date.getFullYear(), this._date.getMonth() + 1, 0).getDate();
349
+ }
350
+ // ============ Conversion ============
351
+ /**
352
+ * Get the underlying Date object
353
+ */
354
+ toDate() {
355
+ return new Date(this._date.getTime());
356
+ }
357
+ /**
358
+ * Get Unix timestamp (seconds)
359
+ */
360
+ unix() {
361
+ return Math.floor(this._date.getTime() / 1000);
362
+ }
363
+ /**
364
+ * Convert to array [year, month, day, hours, minutes, seconds, ms]
365
+ */
366
+ toArray() {
367
+ return [
368
+ this._date.getFullYear(),
369
+ this._date.getMonth() + 1,
370
+ this._date.getDate(),
371
+ this._date.getHours(),
372
+ this._date.getMinutes(),
373
+ this._date.getSeconds(),
374
+ this._date.getMilliseconds()
375
+ ];
376
+ }
377
+ /**
378
+ * Convert to object
379
+ */
380
+ toObject() {
381
+ return {
382
+ year: this._date.getFullYear(),
383
+ month: this._date.getMonth() + 1,
384
+ day: this._date.getDate(),
385
+ hours: this._date.getHours(),
386
+ minutes: this._date.getMinutes(),
387
+ seconds: this._date.getSeconds(),
388
+ milliseconds: this._date.getMilliseconds()
389
+ };
390
+ }
391
+ }
392
+ /**
393
+ * Helper to convert DateInput to Date
394
+ */
395
+ function toDate(input) {
396
+ if (input instanceof Date)
397
+ return input;
398
+ return new Date(input);
399
+ }
400
+ /**
401
+ * Create a chainable date wrapper
402
+ * @example
403
+ * chain(new Date()).add(1, 'day').format('YYYY-MM-DD')
404
+ * chain('2025-01-15').startOf('month').toDate()
405
+ * chain().add(1, 'week').isWeekend()
406
+ */
407
+ export function chain(date) {
408
+ return new ChainedDate(date);
409
+ }
410
+ /**
411
+ * Format a duration in milliseconds
412
+ * Convenience export for use with chain().diff()
413
+ * @example formatMs(chain(a).diff(b)) // "2 days, 3 hours"
414
+ */
415
+ export function formatMs(ms, options) {
416
+ return formatDuration(ms, options);
417
+ }
418
+ // Initialize plugin system if it's available
419
+ // This allows plugins to extend ChainedDate
420
+ if (typeof globalThis !== 'undefined') {
421
+ globalThis.__chainedDateClass = ChainedDate;
422
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * @fileoverview International holiday utilities
3
+ * Calculate holidays for multiple countries including fixed, movable, and lunar-based holidays
4
+ */
5
+ export type CountryCode = 'UK' | 'NL' | 'DE' | 'CA' | 'AU' | 'IT' | 'ES' | 'CN' | 'IN' | 'US';
6
+ export interface Holiday {
7
+ name: string;
8
+ date: Date;
9
+ countryCode: CountryCode;
10
+ type: 'public' | 'bank' | 'observance';
11
+ }
12
+ export declare function getUKHolidays(year: number): Holiday[];
13
+ export declare function getNetherlandsHolidays(year: number): Holiday[];
14
+ export declare function getGermanyHolidays(year: number): Holiday[];
15
+ export declare function getCanadaHolidays(year: number): Holiday[];
16
+ export declare function getAustraliaHolidays(year: number): Holiday[];
17
+ export declare function getItalyHolidays(year: number): Holiday[];
18
+ export declare function getSpainHolidays(year: number): Holiday[];
19
+ export declare function getChinaHolidays(year: number): Holiday[];
20
+ export declare function getIndiaHolidays(year: number): Holiday[];
21
+ /**
22
+ * Get holidays for a specific country and year
23
+ * @param year - The year
24
+ * @param countryCode - ISO country code
25
+ * @returns Array of holidays
26
+ */
27
+ export declare function getHolidays(year: number, countryCode: CountryCode): Holiday[];
28
+ /**
29
+ * Check if a date is a holiday in a specific country
30
+ * @param date - The date to check
31
+ * @param countryCode - ISO country code
32
+ * @returns True if date is a holiday
33
+ */
34
+ export declare function isHoliday(date: Date, countryCode: CountryCode): boolean;
35
+ /**
36
+ * Get the holiday name for a specific date and country
37
+ * @param date - The date to check
38
+ * @param countryCode - ISO country code
39
+ * @returns Holiday name or null if not a holiday
40
+ */
41
+ export declare function getHolidayName(date: Date, countryCode: CountryCode): string | null;
42
+ /**
43
+ * Get next holiday from a given date
44
+ * @param date - The reference date
45
+ * @param countryCode - ISO country code
46
+ * @returns Next holiday or null
47
+ */
48
+ export declare function getNextHoliday(date: Date, countryCode: CountryCode): Holiday | null;
49
+ /**
50
+ * Get upcoming holidays within N days
51
+ * @param date - The reference date
52
+ * @param days - Number of days to look ahead
53
+ * @param countryCode - ISO country code
54
+ * @returns Array of upcoming holidays
55
+ */
56
+ export declare function getUpcomingHolidays(date: Date, days: number, countryCode: CountryCode): Holiday[];
57
+ /**
58
+ * Get all supported country codes
59
+ * @returns Array of country codes
60
+ */
61
+ export declare function getSupportedCountries(): CountryCode[];
62
+ //# sourceMappingURL=holidays.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"holidays.d.ts","sourceRoot":"","sources":["../../src/holidays.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,WAAW,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAE9F,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,E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