@cloudhal/schedules 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +103 -2
- package/dist/src/automation.d.ts +82 -0
- package/dist/src/automation.js +199 -0
- package/dist/src/budget-meter.d.ts +58 -0
- package/dist/src/budget-meter.js +130 -0
- package/dist/src/budget.d.ts +41 -0
- package/dist/src/budget.js +76 -0
- package/dist/src/builtins.d.ts +132 -0
- package/dist/src/builtins.js +473 -0
- package/dist/src/engine.d.ts +16 -0
- package/dist/src/engine.js +30 -0
- package/dist/src/errors.d.ts +22 -0
- package/dist/src/errors.js +56 -0
- package/dist/src/feed-check.d.ts +56 -0
- package/dist/src/feed-check.js +365 -0
- package/dist/src/findings.d.ts +185 -0
- package/dist/src/findings.js +287 -0
- package/dist/src/ids.d.ts +24 -0
- package/dist/src/ids.js +96 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +24 -0
- package/dist/src/instant.d.ts +19 -0
- package/dist/src/instant.js +50 -0
- package/dist/src/notify.d.ts +50 -0
- package/dist/src/notify.js +60 -0
- package/dist/src/recurrence.d.ts +155 -0
- package/dist/src/recurrence.js +939 -0
- package/dist/src/release-when.d.ts +23 -0
- package/dist/src/release-when.js +55 -0
- package/dist/src/release.d.ts +198 -0
- package/dist/src/release.js +552 -0
- package/dist/src/scheduler.d.ts +490 -0
- package/dist/src/scheduler.js +1343 -0
- package/dist/src/sql/finish.d.ts +32 -0
- package/dist/src/sql/finish.js +103 -0
- package/dist/src/sql/index.d.ts +6 -0
- package/dist/src/sql/index.js +15 -0
- package/dist/src/sql/rows.d.ts +125 -0
- package/dist/src/sql/rows.js +297 -0
- package/dist/src/sql/store.d.ts +467 -0
- package/dist/src/sql/store.js +2166 -0
- package/dist/src/taint.d.ts +65 -0
- package/dist/src/taint.js +138 -0
- package/dist/src/text.d.ts +6 -0
- package/dist/src/text.js +25 -0
- package/dist/src/time-entry.d.ts +6 -0
- package/dist/src/time-entry.js +6 -0
- package/dist/src/time.d.ts +61 -0
- package/dist/src/time.js +140 -0
- package/package.json +69 -3
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The RRULE subset (RFC-0008 §9.3, D3) — parse, validate, expand, presets and sentences.
|
|
3
|
+
* `@cloudhal/schedules/recurrence`; Worker-safe, no clock (callers pass `after`).
|
|
4
|
+
*
|
|
5
|
+
* Accepted: FREQ DAILY | WEEKLY | MONTHLY; INTERVAL 1–999; BYDAY (ordinals such as `2TU` and `-1FR`
|
|
6
|
+
* only with MONTHLY); BYMONTHDAY 1–31 or -1 (not with WEEKLY); BYHOUR 0–23; BYMINUTE 0–59 (up to
|
|
7
|
+
* four values); COUNT 1–9999 or UNTIL (`YYYYMMDDTHHMMSSZ`, UTC, inclusive). Week start is Monday.
|
|
8
|
+
* Refused: every other part (SECONDLY, MINUTELY, HOURLY, YEARLY, BYSECOND, BYWEEKNO, BYYEARDAY,
|
|
9
|
+
* BYSETPOS, BYMONTH, WKST, RSCALE, SKIP), the `RRULE:` prefix, a part given twice, a value given
|
|
10
|
+
* twice, COUNT with UNTIL, and rules whose runs come closer than the job's minimum interval
|
|
11
|
+
* (1 hour, at most 24 a day).
|
|
12
|
+
*
|
|
13
|
+
* Semantics:
|
|
14
|
+
* - The expander works in floating local time from `start` and resolves each candidate in the
|
|
15
|
+
* zone with D2: a candidate inside a DST gap moves to the next valid time (`adjusted: "dst-gap"`),
|
|
16
|
+
* a repeated wall-clock time runs once, at its first instant. RFC 5545 §3.3.10 drops gap
|
|
17
|
+
* occurrences instead; this is the documented deviation (§9.2). If a moved candidate lands on an
|
|
18
|
+
* instant that already ran (the hourly preset at 03:30 after 02:30 moved to 03:30), it counts once.
|
|
19
|
+
* - `start` is an occurrence only when it matches the rule (as in `rrule-temporal` without
|
|
20
|
+
* `includeDtstart`); parts the rule omits default to `start`'s (weekday, day of month, hour,
|
|
21
|
+
* minute), so preset rules always include it. A month without the day (the 31st) is skipped.
|
|
22
|
+
* - COUNT counts emitted occurrences; UNTIL compares instants.
|
|
23
|
+
*/
|
|
24
|
+
import { Temporal } from "temporal-polyfill/implementation";
|
|
25
|
+
import { ScheduleError } from "./errors.js";
|
|
26
|
+
import { fnv1a32 } from "./ids.js";
|
|
27
|
+
import {
|
|
28
|
+
assertTimeZone,
|
|
29
|
+
canonicalInstant,
|
|
30
|
+
instantMs,
|
|
31
|
+
isLocalDateTime,
|
|
32
|
+
resolvePlain,
|
|
33
|
+
} from "./time.js";
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/** ISO order (Monday = 1). */
|
|
37
|
+
export const WEEKDAYS = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/** A parsed rule; `formatRule` turns it back into its canonical text. */
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
/** What `occurrences` needs: the contract's `AutomationRecurrence` without `preset`. */
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
export const RULE_MAX_LENGTH = 300;
|
|
75
|
+
export const MAX_BYMINUTE = 4;
|
|
76
|
+
/** Built-in jobs and agent tasks (§9.3). */
|
|
77
|
+
export const DEFAULT_MIN_INTERVAL_MINUTES = 60;
|
|
78
|
+
export const MAX_OCCURRENCES_PER_DAY = 24;
|
|
79
|
+
|
|
80
|
+
const SUPPORTED = new Set([
|
|
81
|
+
"FREQ",
|
|
82
|
+
"INTERVAL",
|
|
83
|
+
"BYDAY",
|
|
84
|
+
"BYMONTHDAY",
|
|
85
|
+
"BYHOUR",
|
|
86
|
+
"BYMINUTE",
|
|
87
|
+
"COUNT",
|
|
88
|
+
"UNTIL",
|
|
89
|
+
]);
|
|
90
|
+
const KNOWN_UNSUPPORTED = new Set([
|
|
91
|
+
"BYSECOND",
|
|
92
|
+
"BYWEEKNO",
|
|
93
|
+
"BYYEARDAY",
|
|
94
|
+
"BYSETPOS",
|
|
95
|
+
"BYMONTH",
|
|
96
|
+
"WKST",
|
|
97
|
+
"RSCALE",
|
|
98
|
+
"SKIP",
|
|
99
|
+
"RDATE",
|
|
100
|
+
"EXDATE",
|
|
101
|
+
]);
|
|
102
|
+
|
|
103
|
+
function bad(message ) {
|
|
104
|
+
throw new ScheduleError("invalid_rule", message);
|
|
105
|
+
}
|
|
106
|
+
function unsupported(message ) {
|
|
107
|
+
throw new ScheduleError("unsupported_rule_part", message);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function intList(
|
|
111
|
+
value ,
|
|
112
|
+
part ,
|
|
113
|
+
min ,
|
|
114
|
+
max ,
|
|
115
|
+
allow ,
|
|
116
|
+
) {
|
|
117
|
+
const out = [];
|
|
118
|
+
for (const raw of value.split(",")) {
|
|
119
|
+
if (!/^[+-]?[0-9]{1,4}$/.test(raw)) bad(`${part}: "${raw}" is not a number`);
|
|
120
|
+
const n = Number(raw);
|
|
121
|
+
if (!(allow ? allow(n) : n >= min && n <= max)) bad(`${part}: ${raw} is out of range`);
|
|
122
|
+
if (out.includes(n)) bad(`${part}: ${raw} is given twice`);
|
|
123
|
+
out.push(n);
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Parses a rule of the subset (without `RRULE:`); throws `ScheduleError` naming the problem. */
|
|
129
|
+
export function parseRule(rule ) {
|
|
130
|
+
if (typeof rule !== "string" || rule.length === 0) bad("empty rule");
|
|
131
|
+
if (rule.length > RULE_MAX_LENGTH) bad(`longer than ${RULE_MAX_LENGTH} characters`);
|
|
132
|
+
if (/^RRULE:/i.test(rule)) bad("write the rule without the RRULE: prefix");
|
|
133
|
+
const parts = new Map ();
|
|
134
|
+
const order = [];
|
|
135
|
+
for (const piece of rule.split(";")) {
|
|
136
|
+
const m = /^([A-Z]+)=([^;=]*)$/.exec(piece);
|
|
137
|
+
if (!m) bad(`"${piece}" is not NAME=value`);
|
|
138
|
+
const [, name = "", value = ""] = m;
|
|
139
|
+
if (!SUPPORTED.has(name)) {
|
|
140
|
+
if (KNOWN_UNSUPPORTED.has(name)) unsupported(`${name} is not supported`);
|
|
141
|
+
bad(`unknown part ${name}`);
|
|
142
|
+
}
|
|
143
|
+
if (parts.has(name)) bad(`${name} is given twice`);
|
|
144
|
+
parts.set(name, value);
|
|
145
|
+
order.push(name);
|
|
146
|
+
}
|
|
147
|
+
if (order[0] !== "FREQ") bad("the rule must start with FREQ");
|
|
148
|
+
const freq = parts.get("FREQ");
|
|
149
|
+
if (freq !== "DAILY" && freq !== "WEEKLY" && freq !== "MONTHLY") {
|
|
150
|
+
if (["SECONDLY", "MINUTELY", "HOURLY", "YEARLY"].includes(freq ?? ""))
|
|
151
|
+
unsupported(`FREQ=${freq} is not supported`);
|
|
152
|
+
bad(`unknown FREQ ${freq}`);
|
|
153
|
+
}
|
|
154
|
+
const out = { freq, interval: 1 };
|
|
155
|
+
const interval = parts.get("INTERVAL");
|
|
156
|
+
if (interval !== undefined) {
|
|
157
|
+
if (!/^[1-9][0-9]{0,2}$/.test(interval)) bad("INTERVAL must be 1–999");
|
|
158
|
+
out.interval = Number(interval);
|
|
159
|
+
}
|
|
160
|
+
const byDay = parts.get("BYDAY");
|
|
161
|
+
if (byDay !== undefined) {
|
|
162
|
+
const days = [];
|
|
163
|
+
for (const raw of byDay.split(",")) {
|
|
164
|
+
const m = /^([+-]?[1-5])?(MO|TU|WE|TH|FR|SA|SU)$/.exec(raw);
|
|
165
|
+
if (!m) bad(`BYDAY: "${raw}" is not a weekday`);
|
|
166
|
+
const day = m[2] ;
|
|
167
|
+
const ordinal = m[1] !== undefined ? Number(m[1]) : undefined;
|
|
168
|
+
if (ordinal !== undefined && freq !== "MONTHLY")
|
|
169
|
+
bad("BYDAY ordinals (2TU, -1FR) need FREQ=MONTHLY");
|
|
170
|
+
if (days.some((d) => d.day === day && d.ordinal === ordinal))
|
|
171
|
+
bad(`BYDAY: ${raw} is given twice`);
|
|
172
|
+
days.push(ordinal !== undefined ? { day, ordinal } : { day });
|
|
173
|
+
}
|
|
174
|
+
out.byDay = days;
|
|
175
|
+
}
|
|
176
|
+
const byMonthDay = parts.get("BYMONTHDAY");
|
|
177
|
+
if (byMonthDay !== undefined) {
|
|
178
|
+
if (freq === "WEEKLY") bad("BYMONTHDAY cannot be used with FREQ=WEEKLY");
|
|
179
|
+
out.byMonthDay = intList(
|
|
180
|
+
byMonthDay,
|
|
181
|
+
"BYMONTHDAY",
|
|
182
|
+
1,
|
|
183
|
+
31,
|
|
184
|
+
(n) => n === -1 || (n >= 1 && n <= 31),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
const byHour = parts.get("BYHOUR");
|
|
188
|
+
if (byHour !== undefined) out.byHour = intList(byHour, "BYHOUR", 0, 23);
|
|
189
|
+
const byMinute = parts.get("BYMINUTE");
|
|
190
|
+
if (byMinute !== undefined) {
|
|
191
|
+
out.byMinute = intList(byMinute, "BYMINUTE", 0, 59);
|
|
192
|
+
if (out.byMinute.length > MAX_BYMINUTE) bad(`BYMINUTE takes at most ${MAX_BYMINUTE} values`);
|
|
193
|
+
}
|
|
194
|
+
const count = parts.get("COUNT");
|
|
195
|
+
const until = parts.get("UNTIL");
|
|
196
|
+
if (count !== undefined && until !== undefined) bad("COUNT and UNTIL cannot be used together");
|
|
197
|
+
if (count !== undefined) {
|
|
198
|
+
if (!/^[1-9][0-9]{0,3}$/.test(count)) bad("COUNT must be 1–9999");
|
|
199
|
+
out.count = Number(count);
|
|
200
|
+
}
|
|
201
|
+
if (until !== undefined) {
|
|
202
|
+
const m = /^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z$/.exec(until);
|
|
203
|
+
if (!m) bad("UNTIL must be a UTC time such as 20261231T235959Z");
|
|
204
|
+
const iso = `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}Z`;
|
|
205
|
+
const ms = Date.parse(iso);
|
|
206
|
+
if (!Number.isFinite(ms) || new Date(ms).toISOString().slice(0, 19) !== iso.slice(0, 19))
|
|
207
|
+
bad(`UNTIL ${until} is not a valid time`);
|
|
208
|
+
out.until = canonicalInstant(ms);
|
|
209
|
+
}
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function untilText(until ) {
|
|
214
|
+
const d = new Date(until);
|
|
215
|
+
const p = (n , w = 2) => String(n).padStart(w, "0");
|
|
216
|
+
return `${p(d.getUTCFullYear(), 4)}${p(d.getUTCMonth() + 1)}${p(d.getUTCDate())}T${p(d.getUTCHours())}${p(d.getUTCMinutes())}${p(d.getUTCSeconds())}Z`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The canonical text of a parsed rule (parts in a fixed order). */
|
|
220
|
+
export function formatRule(rule ) {
|
|
221
|
+
const parts = [`FREQ=${rule.freq}`];
|
|
222
|
+
if (rule.interval !== 1) parts.push(`INTERVAL=${rule.interval}`);
|
|
223
|
+
if (rule.byDay)
|
|
224
|
+
parts.push(`BYDAY=${rule.byDay.map((d) => `${d.ordinal ?? ""}${d.day}`).join(",")}`);
|
|
225
|
+
if (rule.byMonthDay) parts.push(`BYMONTHDAY=${rule.byMonthDay.join(",")}`);
|
|
226
|
+
if (rule.byHour) parts.push(`BYHOUR=${rule.byHour.join(",")}`);
|
|
227
|
+
if (rule.byMinute) parts.push(`BYMINUTE=${rule.byMinute.join(",")}`);
|
|
228
|
+
if (rule.count !== undefined) parts.push(`COUNT=${rule.count}`);
|
|
229
|
+
if (rule.until !== undefined) parts.push(`UNTIL=${untilText(rule.until)}`);
|
|
230
|
+
return parts.join(";");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function plainStart(start ) {
|
|
234
|
+
if (!isLocalDateTime(start))
|
|
235
|
+
throw new ScheduleError(
|
|
236
|
+
"invalid_local_time",
|
|
237
|
+
`not a wall-clock time (YYYY-MM-DDTHH:mm): ${start}`,
|
|
238
|
+
);
|
|
239
|
+
return Temporal.PlainDateTime.from(start, { overflow: "reject" });
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Minutes of the day the rule runs at, sorted. */
|
|
243
|
+
function timesOfDay(rule , start ) {
|
|
244
|
+
const hours = rule?.byHour ?? [start.hour];
|
|
245
|
+
const minutes = rule?.byMinute ?? [start.minute];
|
|
246
|
+
const out = [];
|
|
247
|
+
for (const h of hours) for (const m of minutes) out.push(h * 60 + m);
|
|
248
|
+
return out.sort((a, b) => a - b);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Validates a recurrence for storage: the zone, the start, the rule and the job's minimum interval.
|
|
260
|
+
* Returns the parsed rule (undefined for a one-off).
|
|
261
|
+
*/
|
|
262
|
+
export function validateRecurrence(
|
|
263
|
+
input ,
|
|
264
|
+
options = {},
|
|
265
|
+
) {
|
|
266
|
+
assertTimeZone(input.timeZone);
|
|
267
|
+
const start = plainStart(input.start);
|
|
268
|
+
if (input.rule === undefined) return undefined;
|
|
269
|
+
const rule = parseRule(input.rule);
|
|
270
|
+
const minGap = options.minIntervalMinutes ?? DEFAULT_MIN_INTERVAL_MINUTES;
|
|
271
|
+
const maxPerDay = options.maxPerDay ?? MAX_OCCURRENCES_PER_DAY;
|
|
272
|
+
const times = timesOfDay(rule, start);
|
|
273
|
+
if (times.length > maxPerDay)
|
|
274
|
+
throw new ScheduleError(
|
|
275
|
+
"rule_too_frequent",
|
|
276
|
+
`at most ${maxPerDay} runs a day (this rule has ${times.length})`,
|
|
277
|
+
);
|
|
278
|
+
let gap = Number.POSITIVE_INFINITY;
|
|
279
|
+
for (let i = 1; i < times.length; i++) gap = Math.min(gap, (times[i] ?? 0) - (times[i - 1] ?? 0));
|
|
280
|
+
const dayTokens = (rule.byDay?.length ?? 0) + (rule.byMonthDay?.length ?? 0);
|
|
281
|
+
const consecutiveDays = (rule.freq === "DAILY" && rule.interval === 1) || dayTokens >= 2;
|
|
282
|
+
if (consecutiveDays && times.length > 0)
|
|
283
|
+
gap = Math.min(gap, (times[0] ?? 0) + 1440 - (times[times.length - 1] ?? 0));
|
|
284
|
+
if (gap < minGap)
|
|
285
|
+
throw new ScheduleError(
|
|
286
|
+
"rule_too_frequent",
|
|
287
|
+
`runs must be at least ${minGap} minutes apart (this rule has ${gap})`,
|
|
288
|
+
);
|
|
289
|
+
if (rule.until !== undefined) {
|
|
290
|
+
const first = occurrences(input, { limit: 1 });
|
|
291
|
+
if (first.length === 0) bad("UNTIL is before the first run");
|
|
292
|
+
}
|
|
293
|
+
return rule;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Plain dates of one MONTHLY period. */
|
|
297
|
+
function monthDays(rule , ym , startDay ) {
|
|
298
|
+
const dim = ym.daysInMonth;
|
|
299
|
+
const first = ym.toPlainDate({ day: 1 });
|
|
300
|
+
let fromMonthDay ;
|
|
301
|
+
if (rule.byMonthDay) {
|
|
302
|
+
fromMonthDay = new Set();
|
|
303
|
+
for (const d of rule.byMonthDay) {
|
|
304
|
+
const day = d === -1 ? dim : d;
|
|
305
|
+
if (day <= dim) fromMonthDay.add(day);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
let fromByDay ;
|
|
309
|
+
if (rule.byDay) {
|
|
310
|
+
fromByDay = new Set();
|
|
311
|
+
for (const { day, ordinal } of rule.byDay) {
|
|
312
|
+
const iso = WEEKDAYS.indexOf(day) + 1;
|
|
313
|
+
const firstMatch = 1 + ((iso - first.dayOfWeek + 7) % 7);
|
|
314
|
+
const all = [];
|
|
315
|
+
for (let d = firstMatch; d <= dim; d += 7) all.push(d);
|
|
316
|
+
if (ordinal === undefined) for (const d of all) fromByDay.add(d);
|
|
317
|
+
else {
|
|
318
|
+
const pick = ordinal > 0 ? all[ordinal - 1] : all[all.length + ordinal];
|
|
319
|
+
if (pick !== undefined) fromByDay.add(pick);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
let days ;
|
|
324
|
+
if (fromMonthDay && fromByDay) days = [...fromMonthDay].filter((d) => fromByDay.has(d));
|
|
325
|
+
else if (fromMonthDay) days = [...fromMonthDay];
|
|
326
|
+
else if (fromByDay) days = [...fromByDay];
|
|
327
|
+
else days = startDay <= dim ? [startDay] : [];
|
|
328
|
+
return days.sort((a, b) => a - b);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function dayMatchesFilters(rule , date ) {
|
|
332
|
+
if (rule.byDay && !rule.byDay.some((d) => WEEKDAYS.indexOf(d.day) + 1 === date.dayOfWeek))
|
|
333
|
+
return false;
|
|
334
|
+
if (
|
|
335
|
+
rule.byMonthDay &&
|
|
336
|
+
!rule.byMonthDay.some((d) => (d === -1 ? date.daysInMonth : d) === date.day)
|
|
337
|
+
)
|
|
338
|
+
return false;
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Candidate dates of period `k` (chronological). */
|
|
343
|
+
function periodDates(
|
|
344
|
+
rule ,
|
|
345
|
+
start ,
|
|
346
|
+
k ,
|
|
347
|
+
) {
|
|
348
|
+
const startDate = start.toPlainDate();
|
|
349
|
+
if (rule.freq === "DAILY") {
|
|
350
|
+
const date = startDate.add({ days: k * rule.interval });
|
|
351
|
+
return dayMatchesFilters(rule, date) ? [date] : [];
|
|
352
|
+
}
|
|
353
|
+
if (rule.freq === "WEEKLY") {
|
|
354
|
+
const monday = startDate
|
|
355
|
+
.subtract({ days: startDate.dayOfWeek - 1 })
|
|
356
|
+
.add({ weeks: k * rule.interval });
|
|
357
|
+
const isoDays = rule.byDay
|
|
358
|
+
? [...new Set(rule.byDay.map((d) => WEEKDAYS.indexOf(d.day) + 1))].sort((a, b) => a - b)
|
|
359
|
+
: [startDate.dayOfWeek];
|
|
360
|
+
return isoDays.map((iso) => monday.add({ days: iso - 1 }));
|
|
361
|
+
}
|
|
362
|
+
const ym = Temporal.PlainYearMonth.from({ year: start.year, month: start.month }).add({
|
|
363
|
+
months: k * rule.interval,
|
|
364
|
+
});
|
|
365
|
+
return monthDays(rule, ym, start.day).map((day) => ym.toPlainDate({ day }));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** The period index whose dates come at or before `date` (a safe place to start iterating). */
|
|
369
|
+
function periodBefore(
|
|
370
|
+
rule ,
|
|
371
|
+
start ,
|
|
372
|
+
date ,
|
|
373
|
+
) {
|
|
374
|
+
const startDate = start.toPlainDate();
|
|
375
|
+
if (Temporal.PlainDate.compare(date, startDate) <= 0) return 0;
|
|
376
|
+
let units ;
|
|
377
|
+
if (rule.freq === "DAILY") units = startDate.until(date, { largestUnit: "days" }).days;
|
|
378
|
+
else if (rule.freq === "WEEKLY")
|
|
379
|
+
units = Math.floor(startDate.until(date, { largestUnit: "days" }).days / 7);
|
|
380
|
+
else units = (date.year - start.year) * 12 + (date.month - start.month);
|
|
381
|
+
return Math.max(0, Math.floor(units / rule.interval) - 1);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Expands a recurrence (a one-off when `rule` is absent) into instants, in order. Pure: pass the
|
|
397
|
+
* clock as `after`.
|
|
398
|
+
*/
|
|
399
|
+
export function occurrences(input , query ) {
|
|
400
|
+
assertTimeZone(input.timeZone);
|
|
401
|
+
const start = plainStart(input.start);
|
|
402
|
+
const after = query.after !== undefined ? instantMs(query.after) : Number.NEGATIVE_INFINITY;
|
|
403
|
+
const before = query.before !== undefined ? instantMs(query.before) : Number.POSITIVE_INFINITY;
|
|
404
|
+
const out = [];
|
|
405
|
+
if (query.limit <= 0) return out;
|
|
406
|
+
if (input.rule === undefined) {
|
|
407
|
+
const r = resolvePlain(start, input.timeZone);
|
|
408
|
+
const ms = Date.parse(r.at);
|
|
409
|
+
if (ms > after && ms < before)
|
|
410
|
+
out.push({ at: r.at, local: input.start, ...(r.adjusted ? { adjusted: r.adjusted } : {}) });
|
|
411
|
+
return out;
|
|
412
|
+
}
|
|
413
|
+
const rule = parseRule(input.rule);
|
|
414
|
+
const times = timesOfDay(rule, start);
|
|
415
|
+
const until = rule.until !== undefined ? Date.parse(rule.until) : Number.POSITIVE_INFINITY;
|
|
416
|
+
const maxPeriods =
|
|
417
|
+
query.maxPeriods ?? (rule.freq === "DAILY" ? 36_600 : rule.freq === "WEEKLY" ? 5_220 : 1_200);
|
|
418
|
+
// Without COUNT the expansion may begin near `after`; with COUNT it must count from the start.
|
|
419
|
+
let k = 0;
|
|
420
|
+
if (rule.count === undefined && Number.isFinite(after)) {
|
|
421
|
+
const afterLocal = Temporal.Instant.fromEpochMilliseconds(after)
|
|
422
|
+
.toZonedDateTimeISO(input.timeZone)
|
|
423
|
+
.toPlainDate();
|
|
424
|
+
k = periodBefore(rule, start, afterLocal);
|
|
425
|
+
}
|
|
426
|
+
let emitted = 0;
|
|
427
|
+
let last = Number.NEGATIVE_INFINITY;
|
|
428
|
+
for (let periods = 0; periods < maxPeriods; periods++, k++) {
|
|
429
|
+
for (const date of periodDates(rule, start, k)) {
|
|
430
|
+
for (const minutes of times) {
|
|
431
|
+
const local = date.toPlainDateTime({
|
|
432
|
+
hour: Math.floor(minutes / 60),
|
|
433
|
+
minute: minutes % 60,
|
|
434
|
+
});
|
|
435
|
+
if (Temporal.PlainDateTime.compare(local, start) < 0) continue;
|
|
436
|
+
const r = resolvePlain(local, input.timeZone);
|
|
437
|
+
const ms = Date.parse(r.at);
|
|
438
|
+
if (ms <= last) continue; // a DST move onto an instant that already ran counts once
|
|
439
|
+
if (ms > until) return out;
|
|
440
|
+
last = ms;
|
|
441
|
+
emitted++;
|
|
442
|
+
if (ms > after) {
|
|
443
|
+
if (ms >= before) return out;
|
|
444
|
+
out.push({
|
|
445
|
+
at: r.at,
|
|
446
|
+
local: local.toString({ smallestUnit: "minute" }),
|
|
447
|
+
...(r.adjusted ? { adjusted: r.adjusted } : {}),
|
|
448
|
+
});
|
|
449
|
+
if (out.length >= query.limit) return out;
|
|
450
|
+
}
|
|
451
|
+
if (rule.count !== undefined && emitted >= rule.count) return out;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return out;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** The first occurrence strictly after `after`, or undefined when the recurrence has ended. */
|
|
459
|
+
export function nextOccurrence(
|
|
460
|
+
input ,
|
|
461
|
+
after ,
|
|
462
|
+
) {
|
|
463
|
+
return occurrences(input, { after, limit: 1 })[0];
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Occurrences in `(after, upTo]` — what a tick that last ran at `after` has to account for. Only
|
|
478
|
+
* the first `limit` are listed, but `latest` is always the true latest (only it runs, §8.6).
|
|
479
|
+
*/
|
|
480
|
+
export function occurrencesBetween(
|
|
481
|
+
input ,
|
|
482
|
+
after ,
|
|
483
|
+
upTo ,
|
|
484
|
+
limit = 1_000,
|
|
485
|
+
) {
|
|
486
|
+
const end = instantMs(upTo) + 1;
|
|
487
|
+
const first = occurrences(input, { after, before: end, limit: limit + 1 });
|
|
488
|
+
if (first.length <= limit)
|
|
489
|
+
return { occurrences: first, ...latestOf(first), count: first.length, truncated: false };
|
|
490
|
+
// Many occurrences: look for the latest in a growing window before `upTo` (a dense rule finds it
|
|
491
|
+
// in the first window; a sparse one never has many occurrences to expand).
|
|
492
|
+
const from = instantMs(after);
|
|
493
|
+
for (const days of [2, 64, 800, Number.POSITIVE_INFINITY]) {
|
|
494
|
+
const recent = occurrences(input, {
|
|
495
|
+
after: Math.max(from, instantMs(upTo) - days * 86_400_000),
|
|
496
|
+
before: end,
|
|
497
|
+
limit: Number.MAX_SAFE_INTEGER,
|
|
498
|
+
});
|
|
499
|
+
const latest = recent[recent.length - 1];
|
|
500
|
+
if (latest)
|
|
501
|
+
return { occurrences: first.slice(0, limit), latest, count: limit + 1, truncated: true };
|
|
502
|
+
}
|
|
503
|
+
return { occurrences: first.slice(0, limit), count: limit + 1, truncated: true };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function latestOf(list ) {
|
|
507
|
+
const latest = list[list.length - 1];
|
|
508
|
+
return latest ? { latest } : {};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// ───────────────────────────── presets ─────────────────────────────
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* The rule a Studio/MCP preset stands for, given the wall-clock `start` (§9.3), with every part
|
|
524
|
+
* spelled out so the rule reads the same whatever `start` later becomes: `hourly` is
|
|
525
|
+
* `FREQ=DAILY;BYHOUR=0,…,23;BYMINUTE=m` (HOURLY stays out of the vocabulary), `daily` adds
|
|
526
|
+
* `BYHOUR`/`BYMINUTE`, `weekly` the start's weekday, `monthly` its day of the month. `once` has no
|
|
527
|
+
* rule; `custom` has none of its own (the caller supplies it).
|
|
528
|
+
*/
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
export function presetRule(
|
|
539
|
+
preset ,
|
|
540
|
+
start ,
|
|
541
|
+
) {
|
|
542
|
+
const s = plainStart(start);
|
|
543
|
+
const at = `BYHOUR=${s.hour};BYMINUTE=${s.minute}`;
|
|
544
|
+
switch (preset) {
|
|
545
|
+
case "once":
|
|
546
|
+
return undefined;
|
|
547
|
+
case "hourly":
|
|
548
|
+
return `FREQ=DAILY;BYHOUR=${Array.from({ length: 24 }, (_, h) => h).join(",")};BYMINUTE=${s.minute}`;
|
|
549
|
+
case "daily":
|
|
550
|
+
return `FREQ=DAILY;${at}`;
|
|
551
|
+
case "weekdays":
|
|
552
|
+
return `FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;${at}`;
|
|
553
|
+
case "weekly":
|
|
554
|
+
return `FREQ=WEEKLY;BYDAY=${WEEKDAYS[s.dayOfWeek - 1]};${at}`;
|
|
555
|
+
case "monthly":
|
|
556
|
+
return `FREQ=MONTHLY;BYMONTHDAY=${s.day};${at}`;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/** The preset a rule matches for `start`, or `custom`. */
|
|
561
|
+
export function presetOf(rule , start ) {
|
|
562
|
+
if (rule === undefined) return "once";
|
|
563
|
+
const canonical = formatRule(parseRule(rule));
|
|
564
|
+
for (const p of ["hourly", "daily", "weekdays", "weekly", "monthly"] )
|
|
565
|
+
if (presetRule(p, start) === canonical) return p;
|
|
566
|
+
return "custom";
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* The deterministic minute offset (0–9) a built-in job created from a preset gets, so many sites do
|
|
571
|
+
* not all wake at :00 (§9.5): FNV-1a 32 of the automation id, modulo 10.
|
|
572
|
+
*/
|
|
573
|
+
export function presetMinuteOffset(automationId ) {
|
|
574
|
+
return fnv1a32(automationId) % 10;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// ───────────────────────────── sentences ─────────────────────────────
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* A recurrence in words, as data: Studio and the API render it with `renderRecurrence` (or their
|
|
595
|
+
* own templates from the same keys); nothing here is an English string.
|
|
596
|
+
*/
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
const hhmm = (minutes ) =>
|
|
620
|
+
`${String(Math.floor(minutes / 60)).padStart(2, "0")}:${String(minutes % 60).padStart(2, "0")}`;
|
|
621
|
+
|
|
622
|
+
/** Describes a recurrence as a `RecurrenceSentence`. */
|
|
623
|
+
export function describeRecurrence(input ) {
|
|
624
|
+
const start = plainStart(input.start);
|
|
625
|
+
const base = { timeZone: input.timeZone } ;
|
|
626
|
+
if (input.rule === undefined)
|
|
627
|
+
return {
|
|
628
|
+
...base,
|
|
629
|
+
key: "once",
|
|
630
|
+
date: start.toPlainDate().toString(),
|
|
631
|
+
times: [hhmm(start.hour * 60 + start.minute)],
|
|
632
|
+
};
|
|
633
|
+
const rule = parseRule(input.rule);
|
|
634
|
+
const times = timesOfDay(rule, start).map(hhmm);
|
|
635
|
+
const limit = {
|
|
636
|
+
...(rule.count !== undefined ? { count: rule.count } : {}),
|
|
637
|
+
...(rule.until !== undefined
|
|
638
|
+
? {
|
|
639
|
+
until: Temporal.Instant.fromEpochMilliseconds(Date.parse(rule.until))
|
|
640
|
+
.toZonedDateTimeISO(input.timeZone)
|
|
641
|
+
.toPlainDate()
|
|
642
|
+
.toString(),
|
|
643
|
+
}
|
|
644
|
+
: {}),
|
|
645
|
+
};
|
|
646
|
+
const n = rule.interval;
|
|
647
|
+
const plainDays = rule.byDay?.every((d) => d.ordinal === undefined)
|
|
648
|
+
? rule.byDay.map((d) => d.day)
|
|
649
|
+
: undefined;
|
|
650
|
+
const custom = () => ({
|
|
651
|
+
...base,
|
|
652
|
+
key: "custom",
|
|
653
|
+
times,
|
|
654
|
+
rule: formatRule(rule),
|
|
655
|
+
...limit,
|
|
656
|
+
});
|
|
657
|
+
if (rule.freq === "DAILY") {
|
|
658
|
+
if (rule.byMonthDay) return custom();
|
|
659
|
+
if (rule.byDay) {
|
|
660
|
+
if (n !== 1 || !plainDays) return custom();
|
|
661
|
+
return weekly(plainDays);
|
|
662
|
+
}
|
|
663
|
+
if (rule.byHour?.length === 24 && (rule.byMinute?.length ?? 1) === 1 && n === 1)
|
|
664
|
+
return {
|
|
665
|
+
...base,
|
|
666
|
+
key: "hourly",
|
|
667
|
+
times,
|
|
668
|
+
minute: String(rule.byMinute?.[0] ?? start.minute).padStart(2, "0"),
|
|
669
|
+
...limit,
|
|
670
|
+
};
|
|
671
|
+
return n === 1
|
|
672
|
+
? { ...base, key: "daily", times, ...limit }
|
|
673
|
+
: { ...base, key: "everyNDays", n, times, ...limit };
|
|
674
|
+
}
|
|
675
|
+
if (rule.freq === "WEEKLY") {
|
|
676
|
+
const days = plainDays ?? [WEEKDAYS[start.dayOfWeek - 1] ];
|
|
677
|
+
if (n !== 1) return { ...base, key: "everyNWeeks", n, days: sortDays(days), times, ...limit };
|
|
678
|
+
return weekly(days);
|
|
679
|
+
}
|
|
680
|
+
if (rule.byDay && rule.byMonthDay) return custom();
|
|
681
|
+
const monthly = n === 1;
|
|
682
|
+
if (rule.byDay)
|
|
683
|
+
return {
|
|
684
|
+
...base,
|
|
685
|
+
key: monthly ? "monthlyOnWeekdays" : "everyNMonthsOnWeekdays",
|
|
686
|
+
...(monthly ? {} : { n }),
|
|
687
|
+
weekdays: rule.byDay,
|
|
688
|
+
times,
|
|
689
|
+
...limit,
|
|
690
|
+
};
|
|
691
|
+
return {
|
|
692
|
+
...base,
|
|
693
|
+
key: monthly ? "monthlyOnDays" : "everyNMonthsOnDays",
|
|
694
|
+
...(monthly ? {} : { n }),
|
|
695
|
+
monthDays: rule.byMonthDay
|
|
696
|
+
? [...rule.byMonthDay].sort((a, b) => (a === -1 ? 99 : a) - (b === -1 ? 99 : b))
|
|
697
|
+
: [start.day],
|
|
698
|
+
times,
|
|
699
|
+
...limit,
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
function weekly(days ) {
|
|
703
|
+
const sorted = sortDays(days);
|
|
704
|
+
if (sorted.join(",") === "MO,TU,WE,TH,FR") return { ...base, key: "weekdays", times, ...limit };
|
|
705
|
+
if (sorted.length === 7) return { ...base, key: "daily", times, ...limit };
|
|
706
|
+
return { ...base, key: "weekly", days: sorted, times, ...limit };
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function sortDays(days ) {
|
|
711
|
+
return [...new Set(days)].sort((a, b) => WEEKDAYS.indexOf(a) - WEEKDAYS.indexOf(b));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
|
|
716
|
+
/** Templates and words for `renderRecurrence`, per locale. `{name}` is a placeholder. */
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
const enOrdinalSuffix = (n ) => {
|
|
737
|
+
const t = n % 100;
|
|
738
|
+
if (t >= 11 && t <= 13) return `${n}th`;
|
|
739
|
+
return `${n}${n % 10 === 1 ? "st" : n % 10 === 2 ? "nd" : n % 10 === 3 ? "rd" : "th"}`;
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
/** The recurrence words in English and Turkish (§15.8: our own templates, not a cron-to-text library). */
|
|
743
|
+
export const RECURRENCE_MESSAGES = {
|
|
744
|
+
en: {
|
|
745
|
+
templates: {
|
|
746
|
+
once: "Once, on {date} at {times}",
|
|
747
|
+
hourly: "Every hour at :{minute}",
|
|
748
|
+
daily: "Every day at {times}",
|
|
749
|
+
everyNDays: "Every {n} days at {times}",
|
|
750
|
+
weekdays: "Weekdays at {times}",
|
|
751
|
+
weekly: "Every {days} at {times}",
|
|
752
|
+
everyNWeeks: "Every {n} weeks on {days} at {times}",
|
|
753
|
+
monthlyOnDays: "Monthly on the {monthDays} at {times}",
|
|
754
|
+
everyNMonthsOnDays: "Every {n} months on the {monthDays} at {times}",
|
|
755
|
+
monthlyOnWeekdays: "On the {weekdays} of every month at {times}",
|
|
756
|
+
everyNMonthsOnWeekdays: "On the {weekdays} of every {n} months at {times}",
|
|
757
|
+
custom: "Custom schedule ({rule}) at {times}",
|
|
758
|
+
},
|
|
759
|
+
count: "{count} times",
|
|
760
|
+
until: "until {until}",
|
|
761
|
+
and: " and ",
|
|
762
|
+
listSeparator: ", ",
|
|
763
|
+
dayNames: {
|
|
764
|
+
MO: "Monday",
|
|
765
|
+
TU: "Tuesday",
|
|
766
|
+
WE: "Wednesday",
|
|
767
|
+
TH: "Thursday",
|
|
768
|
+
FR: "Friday",
|
|
769
|
+
SA: "Saturday",
|
|
770
|
+
SU: "Sunday",
|
|
771
|
+
},
|
|
772
|
+
dayNamesOrdinal: {
|
|
773
|
+
MO: "Monday",
|
|
774
|
+
TU: "Tuesday",
|
|
775
|
+
WE: "Wednesday",
|
|
776
|
+
TH: "Thursday",
|
|
777
|
+
FR: "Friday",
|
|
778
|
+
SA: "Saturday",
|
|
779
|
+
SU: "Sunday",
|
|
780
|
+
},
|
|
781
|
+
ordinals: {
|
|
782
|
+
"1": "first",
|
|
783
|
+
"2": "second",
|
|
784
|
+
"3": "third",
|
|
785
|
+
"4": "fourth",
|
|
786
|
+
"5": "fifth",
|
|
787
|
+
"-1": "last",
|
|
788
|
+
"-2": "second to last",
|
|
789
|
+
"-3": "third to last",
|
|
790
|
+
"-4": "fourth to last",
|
|
791
|
+
"-5": "fifth to last",
|
|
792
|
+
every: "every",
|
|
793
|
+
},
|
|
794
|
+
monthDay: enOrdinalSuffix,
|
|
795
|
+
lastMonthDay: "last day",
|
|
796
|
+
monthNames: [
|
|
797
|
+
"January",
|
|
798
|
+
"February",
|
|
799
|
+
"March",
|
|
800
|
+
"April",
|
|
801
|
+
"May",
|
|
802
|
+
"June",
|
|
803
|
+
"July",
|
|
804
|
+
"August",
|
|
805
|
+
"September",
|
|
806
|
+
"October",
|
|
807
|
+
"November",
|
|
808
|
+
"December",
|
|
809
|
+
],
|
|
810
|
+
date: "{d} {month} {y}",
|
|
811
|
+
},
|
|
812
|
+
tr: {
|
|
813
|
+
templates: {
|
|
814
|
+
once: "Bir kez, {date} {times}",
|
|
815
|
+
hourly: "Her saat :{minute}",
|
|
816
|
+
daily: "Her gün {times}",
|
|
817
|
+
everyNDays: "{n} günde bir {times}",
|
|
818
|
+
weekdays: "Hafta içi her gün {times}",
|
|
819
|
+
weekly: "Her {days} {times}",
|
|
820
|
+
everyNWeeks: "{n} haftada bir {days} {times}",
|
|
821
|
+
monthlyOnDays: "Her ayın {monthDays} günü {times}",
|
|
822
|
+
everyNMonthsOnDays: "{n} ayda bir, ayın {monthDays} günü {times}",
|
|
823
|
+
monthlyOnWeekdays: "Her ayın {weekdays} {times}",
|
|
824
|
+
everyNMonthsOnWeekdays: "{n} ayda bir, ayın {weekdays} {times}",
|
|
825
|
+
custom: "Özel tekrar ({rule}) {times}",
|
|
826
|
+
},
|
|
827
|
+
count: "{count} kez",
|
|
828
|
+
until: "{until} tarihine kadar",
|
|
829
|
+
and: " ve ",
|
|
830
|
+
listSeparator: ", ",
|
|
831
|
+
dayNames: {
|
|
832
|
+
MO: "pazartesi",
|
|
833
|
+
TU: "salı",
|
|
834
|
+
WE: "çarşamba",
|
|
835
|
+
TH: "perşembe",
|
|
836
|
+
FR: "cuma",
|
|
837
|
+
SA: "cumartesi",
|
|
838
|
+
SU: "pazar",
|
|
839
|
+
},
|
|
840
|
+
dayNamesOrdinal: {
|
|
841
|
+
MO: "pazartesisi",
|
|
842
|
+
TU: "salısı",
|
|
843
|
+
WE: "çarşambası",
|
|
844
|
+
TH: "perşembesi",
|
|
845
|
+
FR: "cuması",
|
|
846
|
+
SA: "cumartesisi",
|
|
847
|
+
SU: "pazarı",
|
|
848
|
+
},
|
|
849
|
+
ordinals: {
|
|
850
|
+
"1": "birinci",
|
|
851
|
+
"2": "ikinci",
|
|
852
|
+
"3": "üçüncü",
|
|
853
|
+
"4": "dördüncü",
|
|
854
|
+
"5": "beşinci",
|
|
855
|
+
"-1": "son",
|
|
856
|
+
"-2": "sondan ikinci",
|
|
857
|
+
"-3": "sondan üçüncü",
|
|
858
|
+
"-4": "sondan dördüncü",
|
|
859
|
+
"-5": "sondan beşinci",
|
|
860
|
+
every: "her",
|
|
861
|
+
},
|
|
862
|
+
monthDay: (n) => `${n}.`,
|
|
863
|
+
lastMonthDay: "son",
|
|
864
|
+
monthNames: [
|
|
865
|
+
"Ocak",
|
|
866
|
+
"Şubat",
|
|
867
|
+
"Mart",
|
|
868
|
+
"Nisan",
|
|
869
|
+
"Mayıs",
|
|
870
|
+
"Haziran",
|
|
871
|
+
"Temmuz",
|
|
872
|
+
"Ağustos",
|
|
873
|
+
"Eylül",
|
|
874
|
+
"Ekim",
|
|
875
|
+
"Kasım",
|
|
876
|
+
"Aralık",
|
|
877
|
+
],
|
|
878
|
+
date: "{d} {month} {y}",
|
|
879
|
+
},
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
function list(items , m ) {
|
|
883
|
+
if (items.length <= 1) return items[0] ?? "";
|
|
884
|
+
return `${items.slice(0, -1).join(m.listSeparator)}${m.and}${items[items.length - 1]}`;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
function fill(template , values ) {
|
|
888
|
+
return template.replace(/\{([a-zA-Z]+)\}/g, (_, k ) => values[k] ?? `{${k}}`);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function dateWords(iso , m ) {
|
|
892
|
+
const [y = "", mo = "1", d = "1"] = iso.split("-");
|
|
893
|
+
return fill(m.date, { d: String(Number(d)), month: m.monthNames[Number(mo) - 1] ?? mo, y });
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/** Renders a sentence with the locale's templates ("Her ayın ikinci salısı 09:07"). */
|
|
897
|
+
export function renderRecurrence(sentence , locale ) {
|
|
898
|
+
const m = RECURRENCE_MESSAGES[locale];
|
|
899
|
+
const values = {
|
|
900
|
+
times: list(sentence.times, m),
|
|
901
|
+
...(sentence.n !== undefined ? { n: String(sentence.n) } : {}),
|
|
902
|
+
...(sentence.minute !== undefined ? { minute: sentence.minute } : {}),
|
|
903
|
+
...(sentence.date !== undefined ? { date: dateWords(sentence.date, m) } : {}),
|
|
904
|
+
...(sentence.rule !== undefined ? { rule: sentence.rule } : {}),
|
|
905
|
+
...(sentence.days
|
|
906
|
+
? {
|
|
907
|
+
days: list(
|
|
908
|
+
sentence.days.map((d) => m.dayNames[d]),
|
|
909
|
+
m,
|
|
910
|
+
),
|
|
911
|
+
}
|
|
912
|
+
: {}),
|
|
913
|
+
...(sentence.monthDays
|
|
914
|
+
? {
|
|
915
|
+
monthDays: list(
|
|
916
|
+
sentence.monthDays.map((d) => (d === -1 ? m.lastMonthDay : m.monthDay(d))),
|
|
917
|
+
m,
|
|
918
|
+
),
|
|
919
|
+
}
|
|
920
|
+
: {}),
|
|
921
|
+
...(sentence.weekdays
|
|
922
|
+
? {
|
|
923
|
+
weekdays: list(
|
|
924
|
+
sentence.weekdays.map((w) =>
|
|
925
|
+
w.ordinal === undefined
|
|
926
|
+
? `${m.ordinals.every} ${m.dayNamesOrdinal[w.day]}`
|
|
927
|
+
: `${m.ordinals[String(w.ordinal)]} ${m.dayNamesOrdinal[w.day]}`,
|
|
928
|
+
),
|
|
929
|
+
m,
|
|
930
|
+
),
|
|
931
|
+
}
|
|
932
|
+
: {}),
|
|
933
|
+
};
|
|
934
|
+
let text = fill(m.templates[sentence.key], values);
|
|
935
|
+
if (sentence.count !== undefined) text += `, ${fill(m.count, { count: String(sentence.count) })}`;
|
|
936
|
+
if (sentence.until !== undefined)
|
|
937
|
+
text += `, ${fill(m.until, { until: dateWords(sentence.until, m) })}`;
|
|
938
|
+
return text;
|
|
939
|
+
}
|