@c9up/chronos 0.1.15 → 0.1.17

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 (74) hide show
  1. package/README.md +136 -16
  2. package/dist/DateTime.d.ts +209 -8
  3. package/dist/DateTime.d.ts.map +1 -1
  4. package/dist/DateTime.js +815 -116
  5. package/dist/DateTime.js.map +1 -1
  6. package/dist/Duration.d.ts +37 -20
  7. package/dist/Duration.d.ts.map +1 -1
  8. package/dist/Duration.js +287 -120
  9. package/dist/Duration.js.map +1 -1
  10. package/dist/Interval.d.ts +50 -7
  11. package/dist/Interval.d.ts.map +1 -1
  12. package/dist/Interval.js +142 -29
  13. package/dist/Interval.js.map +1 -1
  14. package/dist/atlas.d.ts +11 -15
  15. package/dist/atlas.d.ts.map +1 -1
  16. package/dist/atlas.js +22 -17
  17. package/dist/atlas.js.map +1 -1
  18. package/dist/format.d.ts +47 -0
  19. package/dist/format.d.ts.map +1 -0
  20. package/dist/format.js +387 -0
  21. package/dist/format.js.map +1 -0
  22. package/dist/holidays.d.ts +43 -0
  23. package/dist/holidays.d.ts.map +1 -0
  24. package/dist/holidays.js +102 -0
  25. package/dist/holidays.js.map +1 -0
  26. package/dist/index.d.ts +8 -6
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +4 -3
  29. package/dist/index.js.map +1 -1
  30. package/dist/native/generated.d.ts +11 -2
  31. package/dist/native/generated.d.ts.map +1 -1
  32. package/dist/native.d.ts.map +1 -1
  33. package/dist/native.js +5 -2
  34. package/dist/native.js.map +1 -1
  35. package/dist/parse.d.ts +59 -0
  36. package/dist/parse.d.ts.map +1 -0
  37. package/dist/parse.js +404 -0
  38. package/dist/parse.js.map +1 -0
  39. package/dist/rrule.d.ts +34 -2
  40. package/dist/rrule.d.ts.map +1 -1
  41. package/dist/rrule.js +156 -32
  42. package/dist/rrule.js.map +1 -1
  43. package/index.darwin-arm64.node +0 -0
  44. package/index.darwin-x64.node +0 -0
  45. package/index.linux-arm64-gnu.node +0 -0
  46. package/index.linux-x64-gnu.node +0 -0
  47. package/index.win32-x64-msvc.node +0 -0
  48. package/package.json +2 -3
  49. package/src/DateTime.ts +1089 -124
  50. package/src/Duration.ts +318 -139
  51. package/src/Interval.ts +160 -53
  52. package/src/atlas.ts +23 -17
  53. package/src/format.ts +449 -0
  54. package/src/holidays.ts +161 -0
  55. package/src/index.ts +35 -7
  56. package/src/native/generated.ts +27 -2
  57. package/src/native.ts +7 -3
  58. package/src/parse.ts +559 -0
  59. package/src/rrule.ts +229 -38
  60. package/wasm/chronos_engine_wasm.d.ts +13 -4
  61. package/wasm/chronos_engine_wasm.js +87 -16
  62. package/wasm/chronos_engine_wasm_bg.wasm +0 -0
  63. package/wasm/chronos_engine_wasm_bg.wasm.d.ts +5 -2
  64. package/dist/utils.d.ts +0 -15
  65. package/dist/utils.d.ts.map +0 -1
  66. package/dist/utils.js +0 -17
  67. package/dist/utils.js.map +0 -1
  68. package/scripts/build-napi-types.mjs +0 -69
  69. package/scripts/clean-wasm-pack-meta.mjs +0 -32
  70. package/scripts/copy-napi.mjs +0 -56
  71. package/scripts/generate-napi-types.mjs +0 -156
  72. package/scripts/verify-napi.mjs +0 -46
  73. package/scripts/verify-wasm.mjs +0 -55
  74. package/src/utils.ts +0 -17
package/src/format.ts ADDED
@@ -0,0 +1,449 @@
1
+ /**
2
+ * `toFormat` — Luxon's format tokens (`yyyy-MM-dd HH:mm`, `dd LLL yyyy`,
3
+ * `EEEE`, `ZZ`, …), with Luxon's quoting (`'literal'`, `''` for a quote) and
4
+ * its macro tokens (`D`, `DD`, `t`, `f`, …) rendered through Intl.
5
+ *
6
+ * Numbers are written with Latin digits and names in English unless a locale
7
+ * is given; Luxon reads the host's default locale instead, which made the
8
+ * same call print differently per machine.
9
+ */
10
+
11
+ export interface FormatParts {
12
+ year: number;
13
+ month: number;
14
+ day: number;
15
+ hour: number;
16
+ minute: number;
17
+ second: number;
18
+ millisecond: number;
19
+ /** ISO weekday, 1 = Monday. */
20
+ weekday: number;
21
+ weekNumber: number;
22
+ weekYear: number;
23
+ ordinal: number;
24
+ quarter: number;
25
+ }
26
+
27
+ export interface FormatContext {
28
+ parts: FormatParts;
29
+ /** Epoch milliseconds. */
30
+ ts: number;
31
+ /** UTC offset in minutes. */
32
+ offset: number;
33
+ zone: string;
34
+ locale: string;
35
+ /** Write a zero offset as `Z` (the UTC zone), as Luxon's ISO and SQL output do. */
36
+ allowZ?: boolean;
37
+ }
38
+
39
+ export const MONTHS_LONG = [
40
+ "January",
41
+ "February",
42
+ "March",
43
+ "April",
44
+ "May",
45
+ "June",
46
+ "July",
47
+ "August",
48
+ "September",
49
+ "October",
50
+ "November",
51
+ "December",
52
+ ];
53
+ export const WEEKDAYS_LONG = [
54
+ "Monday",
55
+ "Tuesday",
56
+ "Wednesday",
57
+ "Thursday",
58
+ "Friday",
59
+ "Saturday",
60
+ "Sunday",
61
+ ];
62
+
63
+ type NameLength = "short" | "long" | "narrow";
64
+
65
+ export const MACROS: Readonly<Record<string, Intl.DateTimeFormatOptions>> = {
66
+ D: { year: "numeric", month: "numeric", day: "numeric" },
67
+ DD: { year: "numeric", month: "short", day: "numeric" },
68
+ DDD: { year: "numeric", month: "long", day: "numeric" },
69
+ DDDD: { year: "numeric", month: "long", day: "numeric", weekday: "long" },
70
+ t: { hour: "numeric", minute: "numeric" },
71
+ tt: { hour: "numeric", minute: "numeric", second: "numeric" },
72
+ ttt: {
73
+ hour: "numeric",
74
+ minute: "numeric",
75
+ second: "numeric",
76
+ timeZoneName: "short",
77
+ },
78
+ tttt: {
79
+ hour: "numeric",
80
+ minute: "numeric",
81
+ second: "numeric",
82
+ timeZoneName: "long",
83
+ },
84
+ T: { hour: "numeric", minute: "numeric", hourCycle: "h23" },
85
+ TT: {
86
+ hour: "numeric",
87
+ minute: "numeric",
88
+ second: "numeric",
89
+ hourCycle: "h23",
90
+ },
91
+ TTT: {
92
+ hour: "numeric",
93
+ minute: "numeric",
94
+ second: "numeric",
95
+ hourCycle: "h23",
96
+ timeZoneName: "short",
97
+ },
98
+ TTTT: {
99
+ hour: "numeric",
100
+ minute: "numeric",
101
+ second: "numeric",
102
+ hourCycle: "h23",
103
+ timeZoneName: "long",
104
+ },
105
+ f: {
106
+ year: "numeric",
107
+ month: "numeric",
108
+ day: "numeric",
109
+ hour: "numeric",
110
+ minute: "numeric",
111
+ },
112
+ ff: {
113
+ year: "numeric",
114
+ month: "short",
115
+ day: "numeric",
116
+ hour: "numeric",
117
+ minute: "numeric",
118
+ },
119
+ fff: {
120
+ year: "numeric",
121
+ month: "long",
122
+ day: "numeric",
123
+ hour: "numeric",
124
+ minute: "numeric",
125
+ timeZoneName: "short",
126
+ },
127
+ ffff: {
128
+ year: "numeric",
129
+ month: "long",
130
+ day: "numeric",
131
+ weekday: "long",
132
+ hour: "numeric",
133
+ minute: "numeric",
134
+ timeZoneName: "long",
135
+ },
136
+ F: {
137
+ year: "numeric",
138
+ month: "numeric",
139
+ day: "numeric",
140
+ hour: "numeric",
141
+ minute: "numeric",
142
+ second: "numeric",
143
+ },
144
+ FF: {
145
+ year: "numeric",
146
+ month: "short",
147
+ day: "numeric",
148
+ hour: "numeric",
149
+ minute: "numeric",
150
+ second: "numeric",
151
+ },
152
+ FFF: {
153
+ year: "numeric",
154
+ month: "long",
155
+ day: "numeric",
156
+ hour: "numeric",
157
+ minute: "numeric",
158
+ second: "numeric",
159
+ timeZoneName: "short",
160
+ },
161
+ FFFF: {
162
+ year: "numeric",
163
+ month: "long",
164
+ day: "numeric",
165
+ weekday: "long",
166
+ hour: "numeric",
167
+ minute: "numeric",
168
+ second: "numeric",
169
+ timeZoneName: "long",
170
+ },
171
+ };
172
+
173
+ export interface Token {
174
+ literal: boolean;
175
+ val: string;
176
+ }
177
+
178
+ /** Luxon's `Formatter.parseFormat`: runs of one letter are a token, `'…'` is literal. */
179
+ export function parseFormat(fmt: string): Token[] {
180
+ let current: string | null = null;
181
+ let currentFull = "";
182
+ let bracketed = false;
183
+ const splits: Token[] = [];
184
+ for (const c of fmt) {
185
+ if (c === "'") {
186
+ if (currentFull.length > 0 || bracketed) {
187
+ splits.push({
188
+ literal: bracketed || /^\s+$/.test(currentFull),
189
+ val: currentFull === "" ? "'" : currentFull,
190
+ });
191
+ }
192
+ current = null;
193
+ currentFull = "";
194
+ bracketed = !bracketed;
195
+ } else if (bracketed) {
196
+ currentFull += c;
197
+ } else if (c === current) {
198
+ currentFull += c;
199
+ } else {
200
+ if (currentFull.length > 0) {
201
+ splits.push({ literal: /^\s+$/.test(currentFull), val: currentFull });
202
+ }
203
+ currentFull = c;
204
+ current = c;
205
+ }
206
+ }
207
+ if (currentFull.length > 0) {
208
+ splits.push({
209
+ literal: bracketed || /^\s+$/.test(currentFull),
210
+ val: currentFull,
211
+ });
212
+ }
213
+ return splits;
214
+ }
215
+
216
+ function pad(n: number | string, width = 0): string {
217
+ const value = typeof n === "number" ? n : Number(n);
218
+ const sign = value < 0 ? "-" : "";
219
+ return sign + String(Math.abs(value)).padStart(width, "0");
220
+ }
221
+
222
+ function formatOffset(
223
+ offset: number,
224
+ format: "narrow" | "short" | "techie",
225
+ allowZ = false,
226
+ ): string {
227
+ if (allowZ && offset === 0) return "Z";
228
+ const hours = Math.trunc(Math.abs(offset / 60));
229
+ const minutes = Math.trunc(Math.abs(offset % 60));
230
+ const sign = offset >= 0 ? "+" : "-";
231
+ switch (format) {
232
+ case "short":
233
+ return `${sign}${pad(hours, 2)}:${pad(minutes, 2)}`;
234
+ case "narrow":
235
+ return `${sign}${hours}${minutes > 0 ? `:${minutes}` : ""}`;
236
+ default:
237
+ return `${sign}${pad(hours, 2)}${pad(minutes, 2)}`;
238
+ }
239
+ }
240
+
241
+ export function isEnglish(locale: string): boolean {
242
+ return /^en(?:-|$)/i.test(locale);
243
+ }
244
+
245
+ /** One part of an Intl rendering of the instant, in its zone. */
246
+ function intlPart(
247
+ ctx: FormatContext,
248
+ options: Intl.DateTimeFormatOptions,
249
+ type: Intl.DateTimeFormatPartTypes,
250
+ ): string {
251
+ const parts = new Intl.DateTimeFormat(ctx.locale, {
252
+ ...options,
253
+ timeZone: ctx.zone,
254
+ }).formatToParts(new Date(ctx.ts));
255
+ return parts.find((p) => p.type === type)?.value ?? "";
256
+ }
257
+
258
+ function monthName(
259
+ ctx: FormatContext,
260
+ length: NameLength,
261
+ standalone: boolean,
262
+ ): string {
263
+ if (isEnglish(ctx.locale)) {
264
+ const name = MONTHS_LONG[ctx.parts.month - 1] ?? "";
265
+ if (length === "long") return name;
266
+ return length === "short" ? name.slice(0, 3) : name.slice(0, 1);
267
+ }
268
+ return intlPart(
269
+ ctx,
270
+ standalone ? { month: length } : { month: length, day: "numeric" },
271
+ "month",
272
+ );
273
+ }
274
+
275
+ function weekdayName(
276
+ ctx: FormatContext,
277
+ length: NameLength,
278
+ standalone: boolean,
279
+ ): string {
280
+ if (isEnglish(ctx.locale)) {
281
+ const name = WEEKDAYS_LONG[ctx.parts.weekday - 1] ?? "";
282
+ if (length === "long") return name;
283
+ return length === "short" ? name.slice(0, 3) : name.slice(0, 1);
284
+ }
285
+ return intlPart(
286
+ ctx,
287
+ standalone
288
+ ? { weekday: length }
289
+ : { weekday: length, month: "long", day: "numeric" },
290
+ "weekday",
291
+ );
292
+ }
293
+
294
+ function tokenToString(token: string, ctx: FormatContext): string {
295
+ const p = ctx.parts;
296
+ const english = isEnglish(ctx.locale);
297
+ switch (token) {
298
+ case "S":
299
+ return pad(p.millisecond);
300
+ case "u":
301
+ case "SSS":
302
+ return pad(p.millisecond, 3);
303
+ case "s":
304
+ return pad(p.second);
305
+ case "ss":
306
+ return pad(p.second, 2);
307
+ case "uu":
308
+ return pad(Math.floor(p.millisecond / 10), 2);
309
+ case "uuu":
310
+ return pad(Math.floor(p.millisecond / 100));
311
+ case "m":
312
+ return pad(p.minute);
313
+ case "mm":
314
+ return pad(p.minute, 2);
315
+ case "h":
316
+ return pad(p.hour % 12 === 0 ? 12 : p.hour % 12);
317
+ case "hh":
318
+ return pad(p.hour % 12 === 0 ? 12 : p.hour % 12, 2);
319
+ case "H":
320
+ return pad(p.hour);
321
+ case "HH":
322
+ return pad(p.hour, 2);
323
+ case "Z":
324
+ return formatOffset(ctx.offset, "narrow", ctx.allowZ);
325
+ case "ZZ":
326
+ return formatOffset(ctx.offset, "short", ctx.allowZ);
327
+ case "ZZZ":
328
+ return formatOffset(ctx.offset, "techie", ctx.allowZ);
329
+ case "ZZZZ":
330
+ return intlPart(ctx, { timeZoneName: "short" }, "timeZoneName");
331
+ case "ZZZZZ":
332
+ return intlPart(ctx, { timeZoneName: "long" }, "timeZoneName");
333
+ case "z":
334
+ return ctx.zone;
335
+ case "a":
336
+ return english
337
+ ? p.hour < 12
338
+ ? "AM"
339
+ : "PM"
340
+ : intlPart(ctx, { hour: "numeric", hourCycle: "h12" }, "dayPeriod");
341
+ case "d":
342
+ return pad(p.day);
343
+ case "dd":
344
+ return pad(p.day, 2);
345
+ case "c":
346
+ case "E":
347
+ return pad(p.weekday);
348
+ case "ccc":
349
+ return weekdayName(ctx, "short", true);
350
+ case "cccc":
351
+ return weekdayName(ctx, "long", true);
352
+ case "ccccc":
353
+ return weekdayName(ctx, "narrow", true);
354
+ case "EEE":
355
+ return weekdayName(ctx, "short", false);
356
+ case "EEEE":
357
+ return weekdayName(ctx, "long", false);
358
+ case "EEEEE":
359
+ return weekdayName(ctx, "narrow", false);
360
+ case "L":
361
+ case "M":
362
+ return pad(p.month);
363
+ case "LL":
364
+ case "MM":
365
+ return pad(p.month, 2);
366
+ case "LLL":
367
+ return monthName(ctx, "short", true);
368
+ case "LLLL":
369
+ return monthName(ctx, "long", true);
370
+ case "LLLLL":
371
+ return monthName(ctx, "narrow", true);
372
+ case "MMM":
373
+ return monthName(ctx, "short", false);
374
+ case "MMMM":
375
+ return monthName(ctx, "long", false);
376
+ case "MMMMM":
377
+ return monthName(ctx, "narrow", false);
378
+ case "y":
379
+ return pad(p.year);
380
+ case "yy":
381
+ return pad(p.year.toString().slice(-2), 2);
382
+ case "yyyy":
383
+ return pad(p.year, 4);
384
+ case "yyyyyy":
385
+ return pad(p.year, 6);
386
+ case "G":
387
+ return english
388
+ ? p.year > 0
389
+ ? "AD"
390
+ : "BC"
391
+ : intlPart(ctx, { era: "short" }, "era");
392
+ case "GG":
393
+ return english
394
+ ? p.year > 0
395
+ ? "Anno Domini"
396
+ : "Before Christ"
397
+ : intlPart(ctx, { era: "long" }, "era");
398
+ case "GGGGG":
399
+ return english
400
+ ? p.year > 0
401
+ ? "A"
402
+ : "B"
403
+ : intlPart(ctx, { era: "narrow" }, "era");
404
+ case "kk":
405
+ return pad(p.weekYear.toString().slice(-2), 2);
406
+ case "kkkk":
407
+ return pad(p.weekYear, 4);
408
+ case "W":
409
+ return pad(p.weekNumber);
410
+ case "WW":
411
+ return pad(p.weekNumber, 2);
412
+ case "o":
413
+ return pad(p.ordinal);
414
+ case "ooo":
415
+ return pad(p.ordinal, 3);
416
+ case "q":
417
+ return pad(p.quarter);
418
+ case "qq":
419
+ return pad(p.quarter, 2);
420
+ case "X":
421
+ return pad(Math.floor(ctx.ts / 1000));
422
+ case "x":
423
+ return pad(ctx.ts);
424
+ default: {
425
+ const macro = Object.hasOwn(MACROS, token) ? MACROS[token] : undefined;
426
+ if (macro === undefined) return token;
427
+ const dtf = new Intl.DateTimeFormat(ctx.locale, {
428
+ ...macro,
429
+ timeZone: ctx.zone,
430
+ });
431
+ // Luxon renders a UTC value from its parts, which keep ICU's narrow
432
+ // no-break space before AM/PM; `format()` swaps it for a plain one.
433
+ return ctx.zone === "UTC"
434
+ ? dtf
435
+ .formatToParts(new Date(ctx.ts))
436
+ .map((part) => part.value)
437
+ .join("")
438
+ : dtf.format(new Date(ctx.ts));
439
+ }
440
+ }
441
+ }
442
+
443
+ export function formatLuxon(fmt: string, ctx: FormatContext): string {
444
+ let out = "";
445
+ for (const token of parseFormat(fmt)) {
446
+ out += token.literal ? token.val : tokenToString(token.val, ctx);
447
+ }
448
+ return out;
449
+ }
@@ -0,0 +1,161 @@
1
+ import { DateTime } from "./DateTime.js";
2
+ import { nativeChronos } from "./native.js";
3
+
4
+ /** The countries chronos has a public-holiday calendar for. */
5
+ export type HolidayCountry = "CH" | "FR";
6
+
7
+ /** The 26 Swiss cantons by their two-letter codes, in the federal order. */
8
+ export const SWISS_CANTONS = [
9
+ "ZH",
10
+ "BE",
11
+ "LU",
12
+ "UR",
13
+ "SZ",
14
+ "OW",
15
+ "NW",
16
+ "GL",
17
+ "ZG",
18
+ "FR",
19
+ "SO",
20
+ "BS",
21
+ "BL",
22
+ "SH",
23
+ "AR",
24
+ "AI",
25
+ "SG",
26
+ "GR",
27
+ "AG",
28
+ "TG",
29
+ "TI",
30
+ "VD",
31
+ "VS",
32
+ "NE",
33
+ "GE",
34
+ "JU",
35
+ ] as const;
36
+
37
+ export type SwissCanton = (typeof SWISS_CANTONS)[number];
38
+
39
+ /**
40
+ * Where the holidays apply: a country, and for Switzerland optionally a
41
+ * canton. Without one, Switzerland's answer is its three federal days only.
42
+ */
43
+ export type HolidayOptions =
44
+ | { country: "CH"; region?: SwissCanton }
45
+ | { country: "FR"; region?: undefined };
46
+
47
+ /** The stable keys a holiday carries, for the application to translate. */
48
+ export type HolidayKey =
49
+ | "new_year"
50
+ | "saint_berchtold"
51
+ | "epiphany"
52
+ | "republic_day"
53
+ | "saint_joseph"
54
+ | "naefelser_fahrt"
55
+ | "good_friday"
56
+ | "easter_monday"
57
+ | "labour_day"
58
+ | "victory_in_europe"
59
+ | "ascension"
60
+ | "whit_monday"
61
+ | "corpus_christi"
62
+ | "plebiscite_commemoration"
63
+ | "saints_peter_and_paul"
64
+ | "national_day"
65
+ | "assumption"
66
+ | "geneva_fast"
67
+ | "federal_fast_monday"
68
+ | "nicholas_of_flue"
69
+ | "all_saints"
70
+ | "armistice"
71
+ | "immaculate_conception"
72
+ | "christmas"
73
+ | "saint_stephen"
74
+ | "restoration_of_the_republic";
75
+
76
+ /** One public holiday: its calendar date (`YYYY-MM-DD`) and its key. */
77
+ export interface Holiday {
78
+ date: string;
79
+ key: HolidayKey;
80
+ }
81
+
82
+ const HOLIDAY_KEYS: ReadonlySet<string> = new Set<HolidayKey>([
83
+ "new_year",
84
+ "saint_berchtold",
85
+ "epiphany",
86
+ "republic_day",
87
+ "saint_joseph",
88
+ "naefelser_fahrt",
89
+ "good_friday",
90
+ "easter_monday",
91
+ "labour_day",
92
+ "victory_in_europe",
93
+ "ascension",
94
+ "whit_monday",
95
+ "corpus_christi",
96
+ "plebiscite_commemoration",
97
+ "saints_peter_and_paul",
98
+ "national_day",
99
+ "assumption",
100
+ "geneva_fast",
101
+ "federal_fast_monday",
102
+ "nicholas_of_flue",
103
+ "all_saints",
104
+ "armistice",
105
+ "immaculate_conception",
106
+ "christmas",
107
+ "saint_stephen",
108
+ "restoration_of_the_republic",
109
+ ]);
110
+
111
+ function isHolidayKey(key: string): key is HolidayKey {
112
+ return HOLIDAY_KEYS.has(key);
113
+ }
114
+
115
+ /**
116
+ * Easter Sunday of `year` (Gregorian computus, years 1583-9999), as
117
+ * `YYYY-MM-DD`. Throws `E_CHRONOS_INVALID_YEAR` outside that range.
118
+ */
119
+ export function easterDate(year: number): string {
120
+ return nativeChronos().easterDate(checkYear(year));
121
+ }
122
+
123
+ /**
124
+ * The public holidays of `year`, sorted by date then key. Two holidays on one
125
+ * day are both listed. An unknown country or canton is refused
126
+ * (`E_CHRONOS_UNKNOWN_COUNTRY`, `E_CHRONOS_UNKNOWN_REGION`), never answered
127
+ * with an empty list.
128
+ */
129
+ export function holidays(year: number, options: HolidayOptions): Holiday[] {
130
+ return nativeChronos()
131
+ .holidays(checkYear(year), options.country, options.region)
132
+ .map(({ date, key }) => {
133
+ if (!isHolidayKey(key)) {
134
+ throw new Error(`chronos engine returned an unknown holiday ${key}`);
135
+ }
136
+ return { date, key };
137
+ });
138
+ }
139
+
140
+ /**
141
+ * Whether a day is a public holiday. A `DateTime` is read on its own wall
142
+ * clock (its zone's calendar date); a string must be a calendar date
143
+ * `YYYY-MM-DD` (`E_CHRONOS_INVALID_DATE` otherwise).
144
+ */
145
+ export function isHoliday(
146
+ date: string | DateTime,
147
+ options: HolidayOptions,
148
+ ): boolean {
149
+ const day = date instanceof DateTime ? date.toISODate() : date;
150
+ return nativeChronos().isHoliday(day, options.country, options.region);
151
+ }
152
+
153
+ /** The engine takes an i32: a fraction or a huge number is refused first. */
154
+ function checkYear(year: number): number {
155
+ if (!Number.isInteger(year) || year < 1583 || year > 9999) {
156
+ throw new RangeError(
157
+ `E_CHRONOS_INVALID_YEAR: the Gregorian Easter date is computed for years 1583-9999, got ${year}`,
158
+ );
159
+ }
160
+ return year;
161
+ }
package/src/index.ts CHANGED
@@ -11,6 +11,11 @@ export type {
11
11
  DateUnit,
12
12
  RangeCompareOptions,
13
13
  RangeRelation,
14
+ RelativeRounding,
15
+ RelativeUnit,
16
+ RelativeUnitLike,
17
+ ToRelativeCalendarOptions,
18
+ ToRelativeOptions,
14
19
  } from "./DateTime.js";
15
20
  export {
16
21
  analyzeRange,
@@ -19,10 +24,27 @@ export {
19
24
  inRange,
20
25
  overlapsRange,
21
26
  } from "./DateTime.js";
22
- export type { DurationObject, DurationUnit } from "./Duration.js";
27
+ export type {
28
+ DurationObject,
29
+ DurationUnit,
30
+ DurationUnitLike,
31
+ } from "./Duration.js";
23
32
  export { Duration } from "./Duration.js";
33
+ export type {
34
+ Holiday,
35
+ HolidayCountry,
36
+ HolidayKey,
37
+ HolidayOptions,
38
+ SwissCanton,
39
+ } from "./holidays.js";
40
+ export {
41
+ easterDate,
42
+ holidays,
43
+ isHoliday,
44
+ SWISS_CANTONS,
45
+ } from "./holidays.js";
24
46
  export { Interval } from "./Interval.js";
25
- export type { RRuleBuild } from "./rrule.js";
47
+ export type { ExpandRRuleOptions, RRuleBuild } from "./rrule.js";
26
48
  export { expandRRule, toRRuleString } from "./rrule.js";
27
49
 
28
50
  // `isNativeAvailable` removed — NAPI is now mandatory (no fallback).
@@ -30,6 +52,7 @@ export { expandRRule, toRRuleString } from "./rrule.js";
30
52
  import {
31
53
  analyzeRange,
32
54
  containsRange,
55
+ type DateInput,
33
56
  type DateRange,
34
57
  DateTime,
35
58
  type DateUnit,
@@ -37,7 +60,12 @@ import {
37
60
  overlapsRange,
38
61
  type RangeCompareOptions,
39
62
  } from "./DateTime.js";
40
- import { expandRRule, type RRuleBuild, toRRuleString } from "./rrule.js";
63
+ import {
64
+ type ExpandRRuleOptions,
65
+ expandRRule,
66
+ type RRuleBuild,
67
+ toRRuleString,
68
+ } from "./rrule.js";
41
69
 
42
70
  export function at(input?: string | Date): DateTime {
43
71
  return new DateTime(input);
@@ -52,7 +80,7 @@ export const Chronos = {
52
80
  subtract: (input: string | Date, amount: number, unit: DateUnit): DateTime =>
53
81
  new DateTime(input).minus(amount, unit),
54
82
  diff: (a: string | Date, b: string | Date, unit: DateUnit): number =>
55
- new DateTime(a).diff(b, unit),
83
+ new DateTime(a).diff(b, unit).as(unit),
56
84
  inRange,
57
85
  rangeContains: (
58
86
  outer: DateRange,
@@ -67,9 +95,9 @@ export const Chronos = {
67
95
  rangeRelation: (a: DateRange, b: DateRange, options?: RangeCompareOptions) =>
68
96
  analyzeRange(a, b, options),
69
97
  rrule: (
70
- startIso: string,
98
+ start: DateInput,
71
99
  rrule: string | RRuleBuild,
72
- limit = 100,
73
- ): string[] => expandRRule(startIso, rrule, limit),
100
+ options?: number | ExpandRRuleOptions,
101
+ ): string[] => expandRRule(start, rrule, options),
74
102
  buildRRule: (rule: RRuleBuild): string => toRRuleString(rule),
75
103
  };