@c9up/chronos 0.1.16 → 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.
- package/README.md +81 -8
- package/dist/DateTime.d.ts +64 -4
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +314 -32
- package/dist/DateTime.js.map +1 -1
- package/dist/format.d.ts +10 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +5 -5
- package/dist/format.js.map +1 -1
- package/dist/holidays.d.ts +43 -0
- package/dist/holidays.d.ts.map +1 -0
- package/dist/holidays.js +102 -0
- package/dist/holidays.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/native/generated.d.ts +9 -0
- package/dist/native/generated.d.ts.map +1 -1
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +3 -0
- package/dist/native.js.map +1 -1
- package/dist/parse.d.ts +59 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +404 -0
- package/dist/parse.js.map +1 -0
- package/dist/rrule.d.ts +10 -2
- package/dist/rrule.d.ts.map +1 -1
- package/dist/rrule.js +33 -7
- package/dist/rrule.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +2 -3
- package/src/DateTime.ts +448 -32
- package/src/format.ts +6 -6
- package/src/holidays.ts +161 -0
- package/src/index.ts +18 -0
- package/src/native/generated.ts +21 -0
- package/src/native.ts +4 -0
- package/src/parse.ts +559 -0
- package/src/rrule.ts +47 -8
- package/wasm/chronos_engine_wasm.d.ts +9 -0
- package/wasm/chronos_engine_wasm.js +61 -0
- package/wasm/chronos_engine_wasm_bg.wasm +0 -0
- package/wasm/chronos_engine_wasm_bg.wasm.d.ts +3 -0
- package/dist/utils.d.ts +0 -15
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -17
- package/dist/utils.js.map +0 -1
- package/scripts/build-napi-types.mjs +0 -69
- package/scripts/clean-wasm-pack-meta.mjs +0 -32
- package/scripts/copy-napi.mjs +0 -56
- package/scripts/generate-napi-types.mjs +0 -156
- package/scripts/verify-napi.mjs +0 -46
- package/scripts/verify-wasm.mjs +0 -55
- package/src/utils.ts +0 -17
package/src/holidays.ts
ADDED
|
@@ -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,
|
|
@@ -25,6 +30,19 @@ export type {
|
|
|
25
30
|
DurationUnitLike,
|
|
26
31
|
} from "./Duration.js";
|
|
27
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";
|
|
28
46
|
export { Interval } from "./Interval.js";
|
|
29
47
|
export type { ExpandRRuleOptions, RRuleBuild } from "./rrule.js";
|
|
30
48
|
export { expandRRule, toRRuleString } from "./rrule.js";
|
package/src/native/generated.ts
CHANGED
|
@@ -28,6 +28,13 @@ export interface ZonedOutputNapi {
|
|
|
28
28
|
zoneName: string;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
export interface HolidayNapi {
|
|
32
|
+
/** The calendar date, `YYYY-MM-DD`. */
|
|
33
|
+
date: string;
|
|
34
|
+
/** The stable key the application translates. */
|
|
35
|
+
key: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
31
38
|
export declare function add(iso: string, amount: number, unit: string): string;
|
|
32
39
|
|
|
33
40
|
export declare function diff(aIso: string, bIso: string, unit: string): number;
|
|
@@ -89,3 +96,17 @@ export declare function rruleExpand(
|
|
|
89
96
|
limit: number,
|
|
90
97
|
tzid?: string | undefined | null,
|
|
91
98
|
): Array<string>;
|
|
99
|
+
|
|
100
|
+
export declare function easterDate(year: number): string;
|
|
101
|
+
|
|
102
|
+
export declare function holidays(
|
|
103
|
+
year: number,
|
|
104
|
+
country: string,
|
|
105
|
+
region?: string | undefined | null,
|
|
106
|
+
): Array<HolidayNapi>;
|
|
107
|
+
|
|
108
|
+
export declare function isHoliday(
|
|
109
|
+
date: string,
|
|
110
|
+
country: string,
|
|
111
|
+
region?: string | undefined | null,
|
|
112
|
+
): boolean;
|
package/src/native.ts
CHANGED
|
@@ -81,6 +81,10 @@ if (isNode) {
|
|
|
81
81
|
rruleExpand: (start, rrule, limit, tzid) =>
|
|
82
82
|
wasm.rrule_expand(start, rrule, limit, tzid),
|
|
83
83
|
calendarParts: (iso) => wasm.calendar_parts(iso),
|
|
84
|
+
easterDate: (year) => wasm.easter_date(year),
|
|
85
|
+
holidays: (year, country, region) => wasm.holidays(year, country, region),
|
|
86
|
+
isHoliday: (date, country, region) =>
|
|
87
|
+
wasm.is_holiday(date, country, region),
|
|
84
88
|
};
|
|
85
89
|
} catch (e) {
|
|
86
90
|
loadError = e;
|