rrule-rust 1.2.0 → 2.0.0-next.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.
Files changed (3) hide show
  1. package/index.d.ts +12 -2
  2. package/package.json +15 -15
  3. package/src/lib.rs +53 -25
package/index.d.ts CHANGED
@@ -21,6 +21,16 @@ export const enum Weekday {
21
21
  Saturday = 5,
22
22
  Sunday = 6
23
23
  }
24
+ export interface NWeekday {
25
+ /**
26
+ * If set, this represents the nth occurrence of the weekday.
27
+ * Otherwise it represents every occurrence of the weekday.
28
+ *
29
+ * A negative value represents nth occurrence from the end.
30
+ */
31
+ n?: number
32
+ weekday: Weekday
33
+ }
24
34
  export const enum Month {
25
35
  January = 0,
26
36
  February = 1,
@@ -42,7 +52,7 @@ export class RRule {
42
52
  get frequency(): Frequency
43
53
  get interval(): number
44
54
  get count(): number | null
45
- get byWeekday(): Weekday[]
55
+ get byWeekday(): NWeekday[]
46
56
  get byHour(): Array<number>
47
57
  get byMinute(): Array<number>
48
58
  get bySecond(): Array<number>
@@ -56,7 +66,7 @@ export class RRule {
56
66
  toString(): string
57
67
  setInterval(interval: number): this
58
68
  setCount(count: number): this
59
- setByWeekday(weekdays: ReadonlyArray<Weekday>): this
69
+ setByWeekday(weekdays: readonly Array<NWeekday | Weekday>): this
60
70
  setByHour(hours: ReadonlyArray<number>): this
61
71
  setByMinute(minutes: ReadonlyArray<number>): this
62
72
  setBySecond(seconds: ReadonlyArray<number>): this
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrule-rust",
3
- "version": "1.2.0",
3
+ "version": "2.0.0-next.1",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "keywords": [
@@ -88,19 +88,19 @@
88
88
  "conventional-changelog-conventionalcommits": "^7.0.2"
89
89
  },
90
90
  "optionalDependencies": {
91
- "@rrule-rust/lib-win32-x64-msvc": "1.2.0",
92
- "@rrule-rust/lib-darwin-x64": "1.2.0",
93
- "@rrule-rust/lib-linux-x64-gnu": "1.2.0",
94
- "@rrule-rust/lib-darwin-arm64": "1.2.0",
95
- "@rrule-rust/lib-android-arm64": "1.2.0",
96
- "@rrule-rust/lib-linux-arm64-gnu": "1.2.0",
97
- "@rrule-rust/lib-linux-arm64-musl": "1.2.0",
98
- "@rrule-rust/lib-win32-arm64-msvc": "1.2.0",
99
- "@rrule-rust/lib-linux-arm-gnueabihf": "1.2.0",
100
- "@rrule-rust/lib-linux-x64-musl": "1.2.0",
101
- "@rrule-rust/lib-freebsd-x64": "1.2.0",
102
- "@rrule-rust/lib-win32-ia32-msvc": "1.2.0",
103
- "@rrule-rust/lib-android-arm-eabi": "1.2.0",
104
- "@rrule-rust/lib-darwin-universal": "1.2.0"
91
+ "@rrule-rust/lib-win32-x64-msvc": "2.0.0-next.1",
92
+ "@rrule-rust/lib-darwin-x64": "2.0.0-next.1",
93
+ "@rrule-rust/lib-linux-x64-gnu": "2.0.0-next.1",
94
+ "@rrule-rust/lib-darwin-arm64": "2.0.0-next.1",
95
+ "@rrule-rust/lib-android-arm64": "2.0.0-next.1",
96
+ "@rrule-rust/lib-linux-arm64-gnu": "2.0.0-next.1",
97
+ "@rrule-rust/lib-linux-arm64-musl": "2.0.0-next.1",
98
+ "@rrule-rust/lib-win32-arm64-msvc": "2.0.0-next.1",
99
+ "@rrule-rust/lib-linux-arm-gnueabihf": "2.0.0-next.1",
100
+ "@rrule-rust/lib-linux-x64-musl": "2.0.0-next.1",
101
+ "@rrule-rust/lib-freebsd-x64": "2.0.0-next.1",
102
+ "@rrule-rust/lib-win32-ia32-msvc": "2.0.0-next.1",
103
+ "@rrule-rust/lib-android-arm-eabi": "2.0.0-next.1",
104
+ "@rrule-rust/lib-darwin-universal": "2.0.0-next.1"
105
105
  }
106
106
  }
package/src/lib.rs CHANGED
@@ -30,6 +30,40 @@ pub enum JsWeekday {
30
30
  Sunday,
31
31
  }
32
32
 
33
+ #[napi(object, js_name = "NWeekday")]
34
+ pub struct JsNWeekday {
35
+ /// If set, this represents the nth occurrence of the weekday.
36
+ /// Otherwise it represents every occurrence of the weekday.
37
+ ///
38
+ /// A negative value represents nth occurrence from the end.
39
+ pub n: Option<i16>,
40
+ pub weekday: JsWeekday,
41
+ }
42
+
43
+ impl From<NWeekday> for JsNWeekday {
44
+ fn from(nday: NWeekday) -> Self {
45
+ match nday {
46
+ NWeekday::Every(weekday) => JsNWeekday {
47
+ n: None,
48
+ weekday: map_rust_weekday(weekday),
49
+ },
50
+ NWeekday::Nth(n, weekday) => JsNWeekday {
51
+ n: Some(n),
52
+ weekday: map_rust_weekday(weekday),
53
+ },
54
+ }
55
+ }
56
+ }
57
+
58
+ impl Into<NWeekday> for JsNWeekday {
59
+ fn into(self) -> NWeekday {
60
+ match self.n {
61
+ Some(n) => NWeekday::Nth(n, map_js_weekday(self.weekday)),
62
+ None => NWeekday::Every(map_js_weekday(self.weekday)),
63
+ }
64
+ }
65
+ }
66
+
33
67
  #[napi(js_name = "Month")]
34
68
  pub enum JsMonth {
35
69
  January,
@@ -114,21 +148,14 @@ impl JsRRule {
114
148
  Ok(self.rrule.get_count())
115
149
  }
116
150
 
117
- #[napi(getter, ts_return_type = "Weekday[]")]
118
- pub fn by_weekday(&self, env: Env) -> napi::Result<Array> {
119
- let ndays = self.rrule.get_by_weekday();
120
- let mut arr = env.create_array(0)?;
121
-
122
- for nday in ndays.iter() {
123
- let day = match nday {
124
- NWeekday::Every(day) => *day,
125
- _ => panic!("Unsupported"),
126
- };
127
-
128
- arr.insert(map_rust_weekday(day))?;
129
- }
130
-
131
- Ok(arr)
151
+ #[napi(getter, ts_return_type = "NWeekday[]")]
152
+ pub fn by_weekday(&self) -> Vec<JsNWeekday> {
153
+ return self
154
+ .rrule
155
+ .get_by_weekday()
156
+ .iter()
157
+ .map(|nday| JsNWeekday::from(*nday))
158
+ .collect();
132
159
  }
133
160
 
134
161
  #[napi(getter)]
@@ -213,18 +240,19 @@ impl JsRRule {
213
240
  #[napi]
214
241
  pub fn set_by_weekday(
215
242
  &mut self,
216
- #[napi(ts_arg_type = "ReadonlyArray<Weekday>")] weekdays: Array,
243
+ #[napi(ts_arg_type = "readonly Array<NWeekday | Weekday>")] weekdays: Vec<
244
+ Either<JsNWeekday, JsWeekday>,
245
+ >,
217
246
  ) -> napi::Result<&Self> {
218
- let mut vec: Vec<NWeekday> = Vec::new();
219
-
220
- for i in 0..weekdays.len() {
221
- let day: JsWeekday = weekdays.get(i).unwrap().unwrap();
222
- let day = NWeekday::Every(map_js_weekday(day));
223
-
224
- vec.push(day);
225
- }
247
+ let by_weekday = weekdays
248
+ .into_iter()
249
+ .map(|weekday| match weekday {
250
+ Either::A(nday) => nday.into(),
251
+ Either::B(weekday) => NWeekday::Every(map_js_weekday(weekday)),
252
+ })
253
+ .collect();
226
254
 
227
- replace_with_or_abort(&mut self.rrule, |self_| self_.by_weekday(vec));
255
+ replace_with_or_abort(&mut self.rrule, |self_| self_.by_weekday(by_weekday));
228
256
 
229
257
  Ok(self)
230
258
  }