rrule-temporal 0.1.2 → 0.1.3

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/dist/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export declare class RRuleTemporal {
28
28
  private rawAdvance;
29
29
  private applyTimeOverride;
30
30
  private computeFirst;
31
+ private generateWeeklyOccurrences;
31
32
  private matchesByDay;
32
33
  private matchesByMonth;
33
34
  private matchesAll;
package/dist/index.js CHANGED
@@ -128,15 +128,57 @@ export class RRuleTemporal {
128
128
  return dt;
129
129
  }
130
130
  computeFirst() {
131
+ let zdt = this.originalDtstart;
132
+ // If it's WEEKLY with BYDAY, advance zdt to the first matching weekday ≥ dtstart
133
+ if (this.opts.freq === "WEEKLY" && this.opts.byDay?.length) {
134
+ const dayMap = {
135
+ MO: 1,
136
+ TU: 2,
137
+ WE: 3,
138
+ TH: 4,
139
+ FR: 5,
140
+ SA: 6,
141
+ SU: 7,
142
+ };
143
+ const targetDow = dayMap[this.opts.byDay[0]];
144
+ const delta = (targetDow - zdt.dayOfWeek + 7) % 7;
145
+ zdt = zdt.add({ days: delta });
146
+ }
147
+ // then your existing BYHOUR/BYMINUTE override logic:
131
148
  const { byHour, byMinute } = this.opts;
132
- let candidate = this.originalDtstart;
133
149
  if (byHour || byMinute) {
134
- candidate = this.applyTimeOverride(candidate);
135
- if (Temporal.Instant.compare(candidate.toInstant(), this.originalDtstart.toInstant()) < 0) {
136
- candidate = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
150
+ zdt = this.applyTimeOverride(zdt);
151
+ if (Temporal.Instant.compare(zdt.toInstant(), this.originalDtstart.toInstant()) < 0) {
152
+ zdt = this.applyTimeOverride(this.rawAdvance(this.originalDtstart));
137
153
  }
138
154
  }
139
- return candidate;
155
+ return zdt;
156
+ }
157
+ // inside class RRuleTemporal:
158
+ generateWeeklyOccurrences(sample) {
159
+ const { byDay } = this.opts;
160
+ // Map token → ISO dayOfWeek
161
+ const dayMap = {
162
+ MO: 1,
163
+ TU: 2,
164
+ WE: 3,
165
+ TH: 4,
166
+ FR: 5,
167
+ SA: 6,
168
+ SU: 7,
169
+ };
170
+ // If no BYDAY, default to the sample’s own weekday token
171
+ const tokens = byDay && byDay.length
172
+ ? byDay
173
+ : [Object.entries(dayMap).find(([, d]) => d === sample.dayOfWeek)[0]];
174
+ // For each token, find the date in [sample, sample+6d] matching that weekday
175
+ const occs = tokens.map((tok) => {
176
+ const targetDow = dayMap[tok];
177
+ const delta = (targetDow - sample.dayOfWeek + 7) % 7;
178
+ return this.applyTimeOverride(sample.add({ days: delta }));
179
+ });
180
+ // Return in chronological order
181
+ return occs.sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
140
182
  }
141
183
  // --- NEW: constraint checks ---
142
184
  // 2) Replace your matchesByDay with this:
@@ -207,30 +249,25 @@ export class RRuleTemporal {
207
249
  throw new Error("all() requires iterator when no COUNT/UNTIL");
208
250
  }
209
251
  const dates = [];
210
- // 1) MONTHLY + BYDAY (multi‐day expansions)
252
+ // --- 1) MONTHLY + BYDAY (multi-day expansions) ---
211
253
  if (this.opts.freq === "MONTHLY" && this.opts.byDay) {
212
254
  const start = this.originalDtstart;
213
255
  let monthCursor = start.with({ day: 1 });
214
256
  let matchCount = 0;
215
- outer: while (true) {
257
+ outer_month: while (true) {
216
258
  const occs = this.generateMonthlyOccurrences(monthCursor);
217
259
  for (const occ of occs) {
218
- // skip anything before the very first DTSTART
219
- if (Temporal.ZonedDateTime.compare(occ, start) < 0) {
260
+ if (Temporal.ZonedDateTime.compare(occ, start) < 0)
220
261
  continue;
221
- }
222
- // stop on UNTIL
223
262
  if (this.opts.until &&
224
263
  Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
225
- break outer;
264
+ break outer_month;
226
265
  }
227
- // stop on COUNT
228
266
  if (this.opts.count !== undefined && matchCount >= this.opts.count) {
229
- break outer;
267
+ break outer_month;
230
268
  }
231
- // iterator callback
232
269
  if (iterator && !iterator(occ, matchCount)) {
233
- break outer;
270
+ break outer_month;
234
271
  }
235
272
  dates.push(occ);
236
273
  matchCount++;
@@ -239,7 +276,64 @@ export class RRuleTemporal {
239
276
  }
240
277
  return dates;
241
278
  }
242
- // 2) YEARLY + BYMONTH (one per year, rotating through the byMonth array)
279
+ // --- 2) WEEKLY + BYDAY (or default to DTSTART’s weekday) ---
280
+ if (this.opts.freq === "WEEKLY") {
281
+ const start = this.originalDtstart;
282
+ // Build the list of target weekdays (1=Mon..7=Sun)
283
+ const dayMap = {
284
+ MO: 1,
285
+ TU: 2,
286
+ WE: 3,
287
+ TH: 4,
288
+ FR: 5,
289
+ SA: 6,
290
+ SU: 7,
291
+ };
292
+ // If no BYDAY, default to DTSTART’s weekday token
293
+ const tokens = this.opts.byDay
294
+ ? [...this.opts.byDay]
295
+ : [Object.entries(dayMap).find(([, d]) => d === start.dayOfWeek)[0]];
296
+ const dows = tokens
297
+ .map((tok) => dayMap[tok])
298
+ .filter((d) => d !== undefined)
299
+ .sort((a, b) => a - b);
300
+ // Find the very first weekCursor: the earliest of this week’s matching days ≥ start
301
+ const firstWeekDates = dows.map((dw) => {
302
+ const delta = (dw - start.dayOfWeek + 7) % 7;
303
+ return start.add({ days: delta });
304
+ });
305
+ let weekCursor = firstWeekDates.reduce((a, b) => Temporal.ZonedDateTime.compare(a, b) <= 0 ? a : b);
306
+ let matchCount = 0;
307
+ outer_week: while (true) {
308
+ // Generate this week’s occurrences
309
+ const baseDow = weekCursor.dayOfWeek;
310
+ const occs = dows
311
+ .map((dw) => {
312
+ const delta = dw - baseDow;
313
+ return this.applyTimeOverride(weekCursor.add({ days: delta }));
314
+ })
315
+ .sort((a, b) => Temporal.ZonedDateTime.compare(a, b));
316
+ for (const occ of occs) {
317
+ if (Temporal.ZonedDateTime.compare(occ, start) < 0)
318
+ continue;
319
+ if (this.opts.until &&
320
+ Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
321
+ break outer_week;
322
+ }
323
+ if (this.opts.count !== undefined && matchCount >= this.opts.count) {
324
+ break outer_week;
325
+ }
326
+ if (iterator && !iterator(occ, matchCount)) {
327
+ break outer_week;
328
+ }
329
+ dates.push(occ);
330
+ matchCount++;
331
+ }
332
+ weekCursor = weekCursor.add({ weeks: this.opts.interval });
333
+ }
334
+ return dates;
335
+ }
336
+ // --- 3) YEARLY + BYMONTH (one per year, rotating) ---
243
337
  if (this.opts.freq === "YEARLY" && this.opts.byMonth) {
244
338
  const start = this.originalDtstart;
245
339
  const months = [...this.opts.byMonth].sort((a, b) => a - b);
@@ -249,21 +343,17 @@ export class RRuleTemporal {
249
343
  const month = months[i % months.length];
250
344
  let occ = start.with({ year, month });
251
345
  occ = this.applyTimeOverride(occ);
252
- // skip the very first if it's before dtstart
253
346
  if (i === 0 && Temporal.ZonedDateTime.compare(occ, start) < 0) {
254
347
  i++;
255
348
  continue;
256
349
  }
257
- // stop on UNTIL
258
350
  if (this.opts.until &&
259
351
  Temporal.ZonedDateTime.compare(occ, this.opts.until) > 0) {
260
352
  break;
261
353
  }
262
- // stop on COUNT
263
354
  if (this.opts.count !== undefined && i >= this.opts.count) {
264
355
  break;
265
356
  }
266
- // iterator callback
267
357
  if (iterator && !iterator(occ, i)) {
268
358
  break;
269
359
  }
@@ -272,7 +362,7 @@ export class RRuleTemporal {
272
362
  }
273
363
  return dates;
274
364
  }
275
- // 3) fallback: step and filter
365
+ // --- 4) fallback: step + filter ---
276
366
  let current = this.computeFirst();
277
367
  let matchCount = 0;
278
368
  while (true) {
@@ -352,6 +442,34 @@ export class RRuleTemporal {
352
442
  }
353
443
  return results;
354
444
  }
445
+ if (this.opts.freq === "WEEKLY") {
446
+ const startInst = after instanceof Date
447
+ ? Temporal.Instant.from(after.toISOString())
448
+ : after.toInstant();
449
+ const endInst = before instanceof Date
450
+ ? Temporal.Instant.from(before.toISOString())
451
+ : before.toInstant();
452
+ let weekCursor = this.computeFirst();
453
+ const results = [];
454
+ outer: while (true) {
455
+ const occs = this.generateWeeklyOccurrences(weekCursor);
456
+ for (const occ of occs) {
457
+ const inst = occ.toInstant();
458
+ // break when beyond end
459
+ if (inc
460
+ ? Temporal.Instant.compare(inst, endInst) > 0
461
+ : Temporal.Instant.compare(inst, endInst) >= 0) {
462
+ break outer;
463
+ }
464
+ // include if on/after start
465
+ if (Temporal.Instant.compare(inst, startInst) >= 0) {
466
+ results.push(occ);
467
+ }
468
+ }
469
+ weekCursor = weekCursor.add({ weeks: this.opts.interval });
470
+ }
471
+ return results;
472
+ }
355
473
  // 3) fallback
356
474
  let current = this.computeFirst();
357
475
  while (true) {
@@ -194,3 +194,47 @@ describe("RRuleTemporal - BYMONTH with YEARLY freq (manual opts)", () => {
194
194
  }
195
195
  });
196
196
  });
197
+ describe("RRuleTemporal - Weekly BYDAY simple all()", () => {
198
+ const ics = `DTSTART;TZID=America/Chicago:20250406T000000
199
+ RRULE:FREQ=WEEKLY;BYDAY=MO;BYHOUR=0;BYMINUTE=0;COUNT=4`.trim();
200
+ const rule = new RRuleTemporal({ rruleString: ics });
201
+ test("all() returns 4 consecutive Mondays", () => {
202
+ const dates = rule.all();
203
+ // dayOfWeek 1 == Monday
204
+ expect(dates).toHaveLength(4);
205
+ expect(dates.map((d) => d.day)).toEqual([7, 14, 21, 28]);
206
+ dates.forEach((d) => expect(d.dayOfWeek).toBe(1));
207
+ });
208
+ });
209
+ describe("RRuleTemporal - Weekly BYDAY simple between()", () => {
210
+ const ics = `DTSTART;TZID=America/Chicago:20250325T000000
211
+ RRULE:FREQ=WEEKLY;BYDAY=MO;BYHOUR=0;BYMINUTE=0`.trim();
212
+ const rule = new RRuleTemporal({ rruleString: ics });
213
+ // Window: April 22 2025 UTC → May 20 2025 UTC
214
+ const start = new Date(Date.UTC(2025, 3, 22, 0, 0)); // April is month=3
215
+ const end = new Date(Date.UTC(2025, 4, 20, 0, 0)); // May is month=4
216
+ test("between() returns the Mondays falling in that window", () => {
217
+ const arrInc = rule.between(start, end, true);
218
+ // Expect 2025-04-28, 05-05, 05-12, 05-19
219
+ expect(arrInc.map((d) => d.day)).toEqual([28, 5, 12, 19]);
220
+ arrInc.forEach((d) => expect(d.dayOfWeek).toBe(1));
221
+ });
222
+ });
223
+ describe("RRuleTemporal - Weekly BYDAY frequencies without positional prefix", () => {
224
+ const ics = `DTSTART;TZID=America/Chicago:20250325T000000
225
+ RRULE:FREQ=WEEKLY;BYDAY=MO;BYHOUR=0;BYMINUTE=0`.trim();
226
+ const rule = new RRuleTemporal({ rruleString: ics });
227
+ // 2025 Apr 22 00:00 UTC
228
+ const start = new Date(Date.UTC(2025, 3, 22, 0, 0));
229
+ // 2026 Apr 22 00:00 UTC
230
+ const end = new Date(Date.UTC(2026, 3, 22, 0, 0, 0));
231
+ test("between returns occurrences in window", () => {
232
+ const arrInc = rule.between(start, end, true);
233
+ console.log("arrInc: ", arrInc.map((d) => d.toString()));
234
+ expect(arrInc.map((d) => d.day)).toEqual([
235
+ 28, 5, 12, 19, 26, 2, 9, 16, 23, 30, 7, 14, 21, 28, 4, 11, 18, 25, 1, 8,
236
+ 15, 22, 29, 6, 13, 20, 27, 3, 10, 17, 24, 1, 8, 15, 22, 29, 5, 12, 19, 26,
237
+ 2, 9, 16, 23, 2, 9, 16, 23, 30, 6, 13, 20,
238
+ ]);
239
+ });
240
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrule-temporal",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Recurrence rule processing using Temporal PlainDate/PlainDateTime",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",