rrule-temporal 1.0.0 → 1.1.1

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 CHANGED
@@ -1,12 +1,12 @@
1
1
  # rrule-temporal
2
2
 
3
- Recurrence rule (RFC 5545) processing built on the Temporal API.
3
+ The first and only fully compliant Recurrence rule (RFC 5545) processing JS/TS library built on the Temporal API.
4
4
  The library accepts the familiar `RRULE` format and returns
5
5
  `Temporal.ZonedDateTime` instances for easy time‑zone aware scheduling.
6
6
 
7
7
  See the [demo site](https://ggaabe.github.io/rrule-temporal/) for an interactive playground.
8
8
 
9
- > This was created to advance the rrule library to use Temporal, and to provide a more modern API, as the original rrule library is [not maintained anymore](https://github.com/jkbrzt/rrule/issues/615). Maintainers suggested to use Temporal instead of Date:
9
+ > This library was created to advance the rrule library to use Temporal, and to provide a more modern API, as the original rrule library is [not maintained anymore](https://github.com/jkbrzt/rrule/issues/615). Maintainers suggested to use Temporal instead of Date:
10
10
  >https://github.com/jkbrzt/rrule/issues/450#issuecomment-1055853095
11
11
 
12
12
  ## Installation
@@ -30,6 +30,9 @@ rule.all().forEach(dt => console.log(dt.toString()));
30
30
  // 2025-01-01T09:00:00[UTC]
31
31
  // 2025-01-02T09:00:00[UTC]
32
32
  // 2025-01-03T09:00:00[UTC]
33
+
34
+ // Only the first 10 events
35
+ const firstTen = rule.all((_, i) => i < 10);
33
36
  ```
34
37
 
35
38
  ## Creating a rule with options
@@ -56,6 +59,34 @@ const rule = new RRuleTemporal({
56
59
  rule.all().forEach(dt => console.log(dt.toString()));
57
60
  ```
58
61
 
62
+ ### Manual options
63
+
64
+ When creating a rule with individual fields you can specify any of the options
65
+ below. These correspond to the recurrence rule parts defined in RFC&nbsp;5545:
66
+
67
+ | Option | Description |
68
+ | ------ | ----------- |
69
+ | `freq` | Recurrence frequency (`"YEARLY"`, `"MONTHLY"`, `"WEEKLY"`, `"DAILY"`, `"HOURLY"`, `"MINUTELY"`, `"SECONDLY"`). |
70
+ | `interval` | Interval between each occurrence of `freq`. |
71
+ | `count` | Total number of occurrences. |
72
+ | `until` | Last possible occurrence as `Temporal.ZonedDateTime`. |
73
+ | `byHour` | Hours to include (0&ndash;23). |
74
+ | `byMinute` | Minutes to include (0&ndash;59). |
75
+ | `bySecond` | Seconds to include (0&ndash;59). |
76
+ | `byDay` | List of weekday codes, e.g. `["MO", "WE", "FR"]`. |
77
+ | `byMonth` | Months of the year (1&ndash;12). |
78
+ | `byMonthDay` | Days of the month (1&ndash;31 or negative from end). |
79
+ | `byYearDay` | Days of the year (1&ndash;366 or negative from end). |
80
+ | `byWeekNo` | ISO week numbers (1&ndash;53 or negative from end). |
81
+ | `bySetPos` | Select n-th occurrence(s) after other filters. |
82
+ | `wkst` | Weekday on which the week starts (`"MO"`..`"SU"`). |
83
+ | `rDate` | Additional dates to include. |
84
+ | `exDate` | Exception dates to exclude. |
85
+ | `tzid` | Time zone identifier for interpreting dates. |
86
+ | `maxIterations` | Safety cap when generating occurrences. |
87
+ | `includeDtstart` | Include `DTSTART` even if it does not match the pattern. |
88
+ | `dtstart` | First occurrence as `Temporal.ZonedDateTime`. |
89
+
59
90
  ## Querying occurrences
60
91
 
61
92
  Use the provided methods to enumerate or search for occurrences:
@@ -71,13 +102,58 @@ const next = rule.next();
71
102
  const prev = rule.previous(new Date("2025-05-01T00:00Z"));
72
103
  ```
73
104
 
74
- ## Converting back to text
105
+ ## Converting to human-readable text
106
+
107
+ The `toText` helper converts a rule into a human readable description.
75
108
 
76
109
  ```typescript
77
- rule.toString(); // DTSTART and RRULE lines
78
- toText(rule); // human readable description
110
+ import { Temporal } from "@js-temporal/polyfill";
111
+ import { RRuleTemporal } from "rrule-temporal";
112
+ import { toText } from "rrule-temporal/totext";
113
+
114
+ const rule = new RRuleTemporal({
115
+ rruleString: `DTSTART;TZID=UTC:20250101T090000\nRRULE:FREQ=DAILY;COUNT=3`
116
+ });
117
+
118
+ rule.toString();
119
+ // "DTSTART;TZID=UTC:20250101T090000\nRRULE:FREQ=DAILY;COUNT=3"
120
+ toText(rule); // uses the runtime locale, defaults to English
121
+ toText(rule, "es"); // Spanish description
122
+ toText(rule);
123
+ // "every day for 3 times"
124
+
125
+ const weekly = new RRuleTemporal({
126
+ freq: "WEEKLY",
127
+ byDay: ["SU"],
128
+ byHour: [10],
129
+ dtstart: Temporal.ZonedDateTime.from({
130
+ year: 2025, month: 1, day: 1, hour: 10, timeZone: "UTC"
131
+ })
132
+ });
133
+
134
+ toText(weekly);
135
+ // "every week on Sunday at 10 AM UTC"
136
+ toText(weekly, "es");
137
+ // "cada semana en domingo a las 10 AM UTC"
79
138
  ```
80
139
 
140
+ `toText()` currently ships translations for **English (`en`)**,
141
+ **Spanish (`es`)**, **Hindi (`hi`)**, **Cantonese (`yue`)**, **Arabic (`ar`)**,
142
+ **Hebrew (`he`)** and **Mandarin (`zh`)**. At build time you can reduce bundle size by
143
+ defining the `TOTEXT_LANGS` environment variable, e.g. `TOTEXT_LANGS=en,es,ar`.
144
+
145
+ ### `toText` supported languages
146
+
147
+ | Code | Language |
148
+ | ---- | -------- |
149
+ | en | English |
150
+ | es | Spanish |
151
+ | hi | Hindi |
152
+ | yue | Cantonese |
153
+ | ar | Arabic |
154
+ | he | Hebrew |
155
+ | zh | Mandarin |
156
+
81
157
  ## API
82
158
 
83
159
  | Method | Description |
@@ -88,7 +164,7 @@ toText(rule); // human readable description
88
164
  | `next(after?, inclusive?)` | Next occurrence after a given date. |
89
165
  | `previous(before?, inclusive?)` | Previous occurrence before a date. |
90
166
  | `toString()` | Convert the rule back into `DTSTART` and `RRULE` lines. |
91
- | `toText(rule, locale?)` | English description of the rule. |
167
+ | `toText(rule, locale?)` | Human readable description (`en`, `es`, `hi`, `yue`, `ar`, `he`, `zh`). |
92
168
  | `options()` | Return the normalized options object. |
93
169
 
94
170
  ## Further examples
@@ -118,3 +194,30 @@ const ruleB = new RRuleTemporal({
118
194
  });
119
195
  ruleB.all().forEach(dt => console.log(dt.toString()));
120
196
  ```
197
+
198
+ ### Working with extra and excluded dates
199
+
200
+ ```typescript
201
+ import { Temporal } from "@js-temporal/polyfill";
202
+
203
+ const start = Temporal.ZonedDateTime.from({
204
+ year: 2025, month: 1, day: 1, hour: 12, timeZone: "UTC"
205
+ });
206
+ const ruleC = new RRuleTemporal({
207
+ freq: "WEEKLY",
208
+ count: 5,
209
+ rDate: [start.add({ days: 1 })], // add one extra day
210
+ exDate: [start.add({ weeks: 2 })], // skip the third week
211
+ dtstart: start
212
+ });
213
+
214
+ // First five occurrences (with rDate/exDate accounted for)
215
+ ruleC.all((_, i) => i < 5).forEach(dt => console.log(dt.toString()));
216
+
217
+ // Occurrences within a window
218
+ const hits = ruleC.between(
219
+ new Date("2025-01-01T00:00Z"),
220
+ new Date("2025-02-01T00:00Z"),
221
+ true
222
+ );
223
+ ```