rrule-temporal 0.3.1 → 1.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,6 @@
1
+ import { RRuleTemporal } from './index.cjs';
2
+ import '@js-temporal/polyfill';
3
+
4
+ declare function toText(input: RRuleTemporal | string, locale?: string): string;
5
+
6
+ export { toText };
@@ -0,0 +1,6 @@
1
+ import { RRuleTemporal } from './index.js';
2
+ import '@js-temporal/polyfill';
3
+
4
+ declare function toText(input: RRuleTemporal | string, locale?: string): string;
5
+
6
+ export { toText };
package/dist/totext.js ADDED
@@ -0,0 +1,192 @@
1
+ import {
2
+ RRuleTemporal
3
+ } from "./chunk-U2SCUIWP.js";
4
+
5
+ // src/totext.ts
6
+ var en = {
7
+ weekdayNames: [
8
+ "Monday",
9
+ "Tuesday",
10
+ "Wednesday",
11
+ "Thursday",
12
+ "Friday",
13
+ "Saturday",
14
+ "Sunday"
15
+ ],
16
+ monthNames: [
17
+ "January",
18
+ "February",
19
+ "March",
20
+ "April",
21
+ "May",
22
+ "June",
23
+ "July",
24
+ "August",
25
+ "September",
26
+ "October",
27
+ "November",
28
+ "December"
29
+ ]
30
+ };
31
+ var ALL_LOCALES = { en };
32
+ var env = process.env.TOTEXT_LANGS;
33
+ var active = env ? env.split(",").map((s) => s.trim()).filter(Boolean) : Object.keys(ALL_LOCALES);
34
+ var LOCALES = {};
35
+ for (const l of active) {
36
+ if (ALL_LOCALES[l]) LOCALES[l] = ALL_LOCALES[l];
37
+ }
38
+ function ordinal(n) {
39
+ const abs = Math.abs(n);
40
+ const suffix = abs % 10 === 1 && abs % 100 !== 11 ? "st" : abs % 10 === 2 && abs % 100 !== 12 ? "nd" : abs % 10 === 3 && abs % 100 !== 13 ? "rd" : "th";
41
+ return n < 0 ? `last` : `${abs}${suffix}`;
42
+ }
43
+ function list(arr, mapFn = (x) => `${x}`, final = "and") {
44
+ const mapped = arr.map(mapFn);
45
+ if (mapped.length === 1) return mapped[0];
46
+ return mapped.slice(0, -1).join(", ") + ` ${final} ` + mapped[mapped.length - 1];
47
+ }
48
+ function formatByDayToken(tok, locale) {
49
+ if (typeof tok === "number") return tok.toString();
50
+ const m = tok.match(/^([+-]?\d+)?(MO|TU|WE|TH|FR|SA|SU)$/);
51
+ if (!m) return tok;
52
+ const ord = m[1] ? parseInt(m[1], 10) : 0;
53
+ const weekdayMap = {
54
+ MO: 0,
55
+ TU: 1,
56
+ WE: 2,
57
+ TH: 3,
58
+ FR: 4,
59
+ SA: 5,
60
+ SU: 6
61
+ };
62
+ const idx = weekdayMap[m[2]];
63
+ const name = locale.weekdayNames[idx];
64
+ if (ord === 0) return name;
65
+ if (ord === -1) return `last ${name}`;
66
+ return `${ordinal(ord)} ${name}`;
67
+ }
68
+ function formatTime(hour, minute = 0, second = 0) {
69
+ const hr12 = (hour + 11) % 12 + 1;
70
+ const ampm = hour < 12 ? "AM" : "PM";
71
+ const mm = String(minute).padStart(2, "0");
72
+ const ss = String(second).padStart(2, "0");
73
+ if (second) {
74
+ return `${hr12}:${mm}:${ss} ${ampm}`;
75
+ }
76
+ if (minute) {
77
+ return `${hr12}:${mm} ${ampm}`;
78
+ }
79
+ return `${hr12} ${ampm}`;
80
+ }
81
+ function tzAbbreviation(zdt) {
82
+ const parts = new Intl.DateTimeFormat("en-US", {
83
+ timeZone: zdt.timeZoneId,
84
+ timeZoneName: "short",
85
+ hour: "numeric"
86
+ }).formatToParts(new Date(zdt.epochMilliseconds));
87
+ const tzPart = parts.find((p) => p.type === "timeZoneName");
88
+ return tzPart?.value || zdt.timeZoneId;
89
+ }
90
+ function toText(input, locale) {
91
+ const rule = typeof input === "string" ? new RRuleTemporal({ rruleString: input }) : input;
92
+ const opts = rule.options();
93
+ const lang = (locale ?? Intl.DateTimeFormat().resolvedOptions().locale).split("-")[0];
94
+ const data = LOCALES[lang] || en;
95
+ const {
96
+ freq,
97
+ interval = 1,
98
+ count,
99
+ until,
100
+ byDay,
101
+ byHour,
102
+ byMinute,
103
+ bySecond,
104
+ byMonth,
105
+ byMonthDay,
106
+ byYearDay,
107
+ byWeekNo,
108
+ bySetPos,
109
+ wkst,
110
+ rDate,
111
+ exDate
112
+ } = opts;
113
+ const parts = ["every"];
114
+ const base = {
115
+ YEARLY: "year",
116
+ MONTHLY: "month",
117
+ WEEKLY: "week",
118
+ DAILY: "day",
119
+ HOURLY: "hour",
120
+ MINUTELY: "minute",
121
+ SECONDLY: "second"
122
+ }[freq];
123
+ const daysNormalized = byDay?.map((d) => d.toUpperCase());
124
+ const isWeekdays = daysNormalized && daysNormalized.length === 5 && ["MO", "TU", "WE", "TH", "FR"].every((d) => daysNormalized.includes(d));
125
+ const isEveryday = daysNormalized && daysNormalized.length === 7 && ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].every((d) => daysNormalized.includes(d));
126
+ if (freq === "WEEKLY" && interval === 1 && isWeekdays) {
127
+ parts.push("weekday");
128
+ } else if (freq === "WEEKLY" && interval === 1 && isEveryday) {
129
+ parts.push("day");
130
+ } else {
131
+ if (interval !== 1) {
132
+ parts.push(interval.toString(), base + "s");
133
+ } else {
134
+ parts.push(base);
135
+ }
136
+ }
137
+ if (freq === "WEEKLY" && byDay && !isWeekdays && !isEveryday) {
138
+ parts.push("on", list(byDay, (t) => formatByDayToken(t, data)));
139
+ } else if (byDay && freq !== "WEEKLY") {
140
+ parts.push("on", list(byDay, (t) => formatByDayToken(t, data)));
141
+ }
142
+ if (byMonth) {
143
+ parts.push("in", list(byMonth, (m) => data.monthNames[m - 1]));
144
+ }
145
+ if (byMonthDay) {
146
+ parts.push("on the", list(byMonthDay, (d) => ordinal(d)), "day of the month");
147
+ }
148
+ if (byYearDay) {
149
+ parts.push("on the", list(byYearDay, (d) => ordinal(d)), "day of the year");
150
+ }
151
+ if (byWeekNo) {
152
+ parts.push("in week", list(byWeekNo, (n) => n.toString()));
153
+ }
154
+ if (byHour) {
155
+ const minutes = byMinute ?? [0];
156
+ const seconds = bySecond ?? [0];
157
+ const times = byHour.flatMap(
158
+ (h) => minutes.flatMap((m) => seconds.map((s) => formatTime(h, m, s)))
159
+ );
160
+ parts.push("at", list(times));
161
+ parts.push(tzAbbreviation(opts.dtstart));
162
+ }
163
+ if (!byHour && byMinute) {
164
+ parts.push("at minute", list(byMinute));
165
+ }
166
+ if (!byHour && !byMinute && bySecond) {
167
+ parts.push("at second", list(bySecond));
168
+ }
169
+ if (until) {
170
+ const monthName = data.monthNames[until.month - 1];
171
+ parts.push("until", `${monthName} ${until.day}, ${until.year}`);
172
+ } else if (count !== void 0) {
173
+ parts.push("for", count.toString(), count === 1 ? "time" : "times");
174
+ }
175
+ if (bySetPos) {
176
+ parts.push("on the", list(bySetPos, (n) => ordinal(n)), "instance");
177
+ }
178
+ if (wkst) {
179
+ const wkName = formatByDayToken(wkst, data);
180
+ parts.push("week starts on", wkName);
181
+ }
182
+ if (rDate && rDate.length) {
183
+ parts.push("with", `${rDate.length}`, rDate.length === 1 ? "additional date" : "additional dates");
184
+ }
185
+ if (exDate && exDate.length) {
186
+ parts.push("excluding", `${exDate.length}`, exDate.length === 1 ? "date" : "dates");
187
+ }
188
+ return parts.join(" ");
189
+ }
190
+ export {
191
+ toText
192
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrule-temporal",
3
- "version": "0.3.1",
3
+ "version": "1.0.0",
4
4
  "description": "Recurrence rule processing using Temporal PlainDate/PlainDateTime",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -9,12 +9,18 @@
9
9
  "types": "./dist/index.d.ts",
10
10
  "import": "./dist/index.js",
11
11
  "require": "./dist/index.cjs"
12
+ },
13
+ "./totext": {
14
+ "types": "./dist/totext.d.ts",
15
+ "import": "./dist/totext.js",
16
+ "require": "./dist/totext.cjs"
12
17
  }
13
18
  },
14
19
  "scripts": {
15
20
  "build": "tsup",
16
- "test": "jest",
17
- "format": "./node_modules/.bin/prettier --write 'src/tests/rrule.test.ts'",
21
+ "test": "jest --coverage=false",
22
+ "coverage": "jest",
23
+ "format": "./node_modules/.bin/prettier --write 'src/tests/{rrule,edge-cases}.test.ts'",
18
24
  "prepare": "npm run build"
19
25
  },
20
26
  "repository": {