rrule-rust 3.0.0-alpha.3 → 3.0.0-alpha.5

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.
@@ -5,9 +5,11 @@ export interface DateTimeLike {
5
5
  readonly hour: number;
6
6
  readonly minute: number;
7
7
  readonly second: number;
8
+ readonly utc: boolean;
8
9
  }
9
- export interface FromObjectOptions {
10
- utc?: boolean;
10
+ export type DateTimeLikeWithoutUtc = Omit<DateTimeLike, 'utc'>;
11
+ export interface ToPlainOptions {
12
+ stripUtc?: boolean;
11
13
  }
12
14
  /**
13
15
  * Represents a date and time. Either local or UTC.
@@ -37,7 +39,7 @@ export declare class DateTime implements DateTimeLike {
37
39
  /**
38
40
  * Creates a new DateTime object from the given plain object.
39
41
  */
40
- static fromObject(object: DateTimeLike, options?: FromObjectOptions): DateTime;
42
+ static fromPlain(plain: DateTimeLike): DateTime;
41
43
  /**
42
44
  * Creates a new DateTime object from provided string representation of a date according to RFC 5545.
43
45
  */
@@ -45,7 +47,13 @@ export declare class DateTime implements DateTimeLike {
45
47
  /**
46
48
  * Converts DateTime into a plain object.
47
49
  */
48
- toObject(): DateTimeLike;
50
+ toPlain(options: {
51
+ stripUtc: false;
52
+ } & ToPlainOptions): DateTimeLike;
53
+ toPlain(options: {
54
+ stripUtc: true;
55
+ } & ToPlainOptions): DateTimeLikeWithoutUtc;
56
+ toPlain(): DateTimeLike;
49
57
  /**
50
58
  * Converts DateTime into ISO 8601 string:
51
59
  * - `YYYYMMDDTHHMMSSZ` for UTC
@@ -54,8 +54,8 @@ export class DateTime {
54
54
  /**
55
55
  * Creates a new DateTime object from the given plain object.
56
56
  */
57
- static fromObject(object, options) {
58
- return DateTime.create(object.year, object.month, object.day, object.hour, object.minute, object.second, !!options?.utc);
57
+ static fromPlain(plain) {
58
+ return DateTime.create(plain.year, plain.month, plain.day, plain.hour, plain.minute, plain.second, plain.utc);
59
59
  }
60
60
  /**
61
61
  * Creates a new DateTime object from provided string representation of a date according to RFC 5545.
@@ -86,10 +86,7 @@ export class DateTime {
86
86
  static fromNumeric(numeric) {
87
87
  return new DateTime(numeric);
88
88
  }
89
- /**
90
- * Converts DateTime into a plain object.
91
- */
92
- toObject() {
89
+ toPlain(options) {
93
90
  return {
94
91
  year: this.year,
95
92
  month: this.month,
@@ -97,6 +94,7 @@ export class DateTime {
97
94
  hour: this.hour,
98
95
  minute: this.minute,
99
96
  second: this.second,
97
+ utc: options?.stripUtc ? undefined : this.utc,
100
98
  };
101
99
  }
102
100
  /**
@@ -1,12 +1,20 @@
1
- import { RRule } from './rrule';
2
- import { DateTime } from './datetime';
3
- export interface RRuleSetLike {
1
+ import { RRule, type RRuleLike } from './rrule';
2
+ import { DateTime, type DateTimeLike } from './datetime';
3
+ export interface RRuleSetOptions {
4
4
  readonly dtstart: DateTime;
5
5
  readonly tzid?: string;
6
- readonly rrules: readonly RRule[];
7
- readonly exrules: readonly RRule[];
8
- readonly exdates: readonly DateTime[];
9
- readonly rdates: readonly DateTime[];
6
+ readonly rrules?: readonly RRule[];
7
+ readonly exrules?: readonly RRule[];
8
+ readonly exdates?: readonly DateTime[];
9
+ readonly rdates?: readonly DateTime[];
10
+ }
11
+ export interface RRuleSetLike {
12
+ readonly dtstart: DateTimeLike;
13
+ readonly tzid?: string;
14
+ readonly rrules: readonly RRuleLike[];
15
+ readonly exrules: readonly RRuleLike[];
16
+ readonly exdates: readonly DateTimeLike[];
17
+ readonly rdates: readonly DateTimeLike[];
10
18
  }
11
19
  export declare class RRuleSet implements Iterable<DateTime> {
12
20
  readonly dtstart: DateTime;
@@ -15,12 +23,13 @@ export declare class RRuleSet implements Iterable<DateTime> {
15
23
  readonly exrules: readonly RRule[];
16
24
  readonly exdates: readonly DateTime[];
17
25
  readonly rdates: readonly DateTime[];
18
- constructor(dtstart: DateTime, tzid?: string);
19
- constructor(options: Partial<RRuleSetLike>);
26
+ constructor(dtstart: DateTime | DateTimeLike, tzid?: string);
27
+ constructor(options: RRuleSetOptions);
20
28
  /**
21
29
  * Parses a string into an RRuleSet.
22
30
  */
23
31
  static parse(str: string): RRuleSet;
32
+ static fromPlain(plain: RRuleSetLike): RRuleSet;
24
33
  setDtstart(dtstart: DateTime): RRuleSet;
25
34
  setTzid(tzid: string): RRuleSet;
26
35
  addRrule(rrule: RRule): RRuleSet;
@@ -55,6 +64,7 @@ export declare class RRuleSet implements Iterable<DateTime> {
55
64
  /**
56
65
  * Converts the RRuleSet to a plain object.
57
66
  */
58
- toObject(): RRuleSetLike;
67
+ toPlain(): RRuleSetLike;
59
68
  [Symbol.iterator](): Iterator<DateTime, any, any>;
69
+ private toOptions;
60
70
  }
@@ -2,24 +2,20 @@ import { RRule } from './rrule';
2
2
  import { RRuleSet as Rust } from './lib';
3
3
  import { DateTime } from './datetime';
4
4
  export class RRuleSet {
5
- constructor(setOrDtstart, tzid) {
6
- if (!(setOrDtstart instanceof DateTime)) {
7
- if (setOrDtstart?.dtstart) {
8
- this.dtstart = setOrDtstart?.dtstart;
9
- }
10
- else {
11
- const date = new Date();
12
- this.dtstart = DateTime.create(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), true);
13
- }
14
- this.tzid = setOrDtstart?.tzid ?? 'UTC';
15
- this.rrules = setOrDtstart?.rrules ?? [];
16
- this.exrules = setOrDtstart?.exrules ?? [];
17
- this.exdates = setOrDtstart?.exdates ?? [];
18
- this.rdates = setOrDtstart?.rdates ?? [];
5
+ constructor(optionsOrDtstart, tzid) {
6
+ if (optionsOrDtstart !== undefined &&
7
+ !(optionsOrDtstart instanceof DateTime) &&
8
+ 'dtstart' in optionsOrDtstart) {
9
+ this.dtstart = optionsOrDtstart.dtstart;
10
+ this.tzid = optionsOrDtstart.tzid ?? 'UTC';
11
+ this.rrules = optionsOrDtstart?.rrules ?? [];
12
+ this.exrules = optionsOrDtstart?.exrules ?? [];
13
+ this.exdates = optionsOrDtstart?.exdates ?? [];
14
+ this.rdates = optionsOrDtstart?.rdates ?? [];
19
15
  }
20
- else if (setOrDtstart instanceof DateTime && typeof tzid === 'string') {
21
- this.dtstart = setOrDtstart;
22
- this.tzid = tzid;
16
+ else if (optionsOrDtstart instanceof DateTime) {
17
+ this.dtstart = optionsOrDtstart;
18
+ this.tzid = tzid ?? 'UTC';
23
19
  this.rrules = [];
24
20
  this.exrules = [];
25
21
  this.exdates = [];
@@ -36,6 +32,16 @@ export class RRuleSet {
36
32
  const rust = Rust.parse(str);
37
33
  return this.fromRust(rust);
38
34
  }
35
+ static fromPlain(plain) {
36
+ return new RRuleSet({
37
+ dtstart: DateTime.fromPlain(plain.dtstart),
38
+ tzid: plain.tzid,
39
+ rrules: plain.rrules.map((rrule) => RRule.fromPlain(rrule)),
40
+ exrules: plain.exrules.map((rrule) => RRule.fromPlain(rrule)),
41
+ exdates: plain.exdates.map((datetime) => DateTime.fromPlain(datetime)),
42
+ rdates: plain.rdates.map((datetime) => DateTime.fromPlain(datetime)),
43
+ });
44
+ }
39
45
  /**
40
46
  * @internal
41
47
  */
@@ -53,61 +59,61 @@ export class RRuleSet {
53
59
  }
54
60
  setDtstart(dtstart) {
55
61
  return new RRuleSet({
56
- ...this.toObject(),
62
+ ...this.toOptions(),
57
63
  dtstart: dtstart,
58
64
  });
59
65
  }
60
66
  setTzid(tzid) {
61
67
  return new RRuleSet({
62
- ...this.toObject(),
68
+ ...this.toOptions(),
63
69
  tzid: tzid,
64
70
  });
65
71
  }
66
72
  addRrule(rrule) {
67
73
  return new RRuleSet({
68
- ...this.toObject(),
74
+ ...this.toOptions(),
69
75
  rrules: [...this.rrules, rrule],
70
76
  });
71
77
  }
72
78
  setRrules(rrules) {
73
79
  return new RRuleSet({
74
- ...this.toObject(),
80
+ ...this.toOptions(),
75
81
  rrules: rrules,
76
82
  });
77
83
  }
78
84
  addExrule(rrule) {
79
85
  return new RRuleSet({
80
- ...this.toObject(),
86
+ ...this.toOptions(),
81
87
  exrules: [...this.exrules, rrule],
82
88
  });
83
89
  }
84
90
  setExrules(rrules) {
85
91
  return new RRuleSet({
86
- ...this.toObject(),
92
+ ...this.toOptions(),
87
93
  exrules: rrules,
88
94
  });
89
95
  }
90
96
  addExdate(datetime) {
91
97
  return new RRuleSet({
92
- ...this.toObject(),
98
+ ...this.toOptions(),
93
99
  exdates: [...this.exdates, datetime],
94
100
  });
95
101
  }
96
102
  setExdates(datetimes) {
97
103
  return new RRuleSet({
98
- ...this.toObject(),
104
+ ...this.toOptions(),
99
105
  exdates: datetimes,
100
106
  });
101
107
  }
102
108
  addRdate(datetime) {
103
109
  return new RRuleSet({
104
- ...this.toObject(),
110
+ ...this.toOptions(),
105
111
  rdates: [...this.rdates, datetime],
106
112
  });
107
113
  }
108
114
  setRdates(datetimes) {
109
115
  return new RRuleSet({
110
- ...this.toObject(),
116
+ ...this.toOptions(),
111
117
  rdates: datetimes,
112
118
  });
113
119
  }
@@ -154,14 +160,14 @@ export class RRuleSet {
154
160
  /**
155
161
  * Converts the RRuleSet to a plain object.
156
162
  */
157
- toObject() {
163
+ toPlain() {
158
164
  return {
159
- dtstart: this.dtstart,
165
+ dtstart: this.dtstart.toPlain(),
160
166
  tzid: this.tzid,
161
- rrules: this.rrules,
162
- exrules: this.exrules,
163
- exdates: this.exdates,
164
- rdates: this.rdates,
167
+ rrules: this.rrules.map((rrule) => rrule.toPlain()),
168
+ exrules: this.exrules.map((rrule) => rrule.toPlain()),
169
+ exdates: this.exdates.map((rrule) => rrule.toPlain()),
170
+ rdates: this.rdates.map((rrule) => rrule.toPlain()),
165
171
  };
166
172
  }
167
173
  [Symbol.iterator]() {
@@ -182,4 +188,14 @@ export class RRuleSet {
182
188
  },
183
189
  };
184
190
  }
191
+ toOptions() {
192
+ return {
193
+ dtstart: this.dtstart,
194
+ tzid: this.tzid,
195
+ rrules: this.rrules,
196
+ exrules: this.exrules,
197
+ exdates: this.exdates,
198
+ rdates: this.rdates,
199
+ };
200
+ }
185
201
  }
@@ -1,4 +1,4 @@
1
- import { DateTime } from './datetime';
1
+ import { DateTime, type DateTimeLike } from './datetime';
2
2
  export interface NWeekday {
3
3
  /**
4
4
  * If set, this represents the nth occurrence of the weekday.
@@ -41,11 +41,27 @@ export declare enum Weekday {
41
41
  Saturday = 5,
42
42
  Sunday = 6
43
43
  }
44
- export interface RRuleLike {
44
+ export interface RRuleOptions {
45
45
  readonly frequency: Frequency;
46
46
  readonly interval?: number;
47
47
  readonly count?: number;
48
48
  readonly until?: DateTime;
49
+ readonly byWeekday?: readonly (NWeekday | Weekday)[];
50
+ readonly byHour?: readonly number[];
51
+ readonly byMinute?: readonly number[];
52
+ readonly bySecond?: readonly number[];
53
+ readonly byMonthday?: readonly number[];
54
+ readonly bySetpos?: readonly number[];
55
+ readonly byMonth?: readonly Month[];
56
+ readonly byWeekno?: readonly number[];
57
+ readonly byYearday?: readonly number[];
58
+ readonly weekstart?: Weekday;
59
+ }
60
+ export interface RRuleLike {
61
+ readonly frequency: Frequency;
62
+ readonly interval?: number;
63
+ readonly count?: number;
64
+ readonly until?: DateTimeLike;
49
65
  readonly byWeekday: readonly (NWeekday | Weekday)[];
50
66
  readonly byHour: readonly number[];
51
67
  readonly byMinute: readonly number[];
@@ -73,11 +89,12 @@ export declare class RRule {
73
89
  readonly byYearday: readonly number[];
74
90
  readonly weekstart?: Weekday;
75
91
  constructor(frequency: Frequency);
76
- constructor(rrule?: Partial<RRuleLike>);
92
+ constructor(options: RRuleOptions);
77
93
  /**
78
94
  * Parses a string into an RRule.
79
95
  */
80
96
  static parse(str: string): RRule;
97
+ static fromPlain(rrule: RRuleLike): RRule;
81
98
  setFrequency(frequency: Frequency): RRule;
82
99
  setInterval(interval: number): RRule;
83
100
  setCount(count: number): RRule;
@@ -93,5 +110,6 @@ export declare class RRule {
93
110
  setWeekstart(day: Weekday): RRule;
94
111
  setUntil(datetime: DateTime): RRule;
95
112
  toString(): string;
96
- toObject(): RRuleLike;
113
+ toPlain(): RRuleLike;
114
+ private toOptions;
97
115
  }
@@ -36,25 +36,27 @@ export var Weekday;
36
36
  Weekday[Weekday["Sunday"] = 6] = "Sunday";
37
37
  })(Weekday || (Weekday = {}));
38
38
  export class RRule {
39
- constructor(rruleOrFrequency = {}) {
40
- if (typeof rruleOrFrequency === 'object' && rruleOrFrequency !== null) {
41
- this.frequency = rruleOrFrequency.frequency ?? Frequency.Daily;
42
- this.interval = rruleOrFrequency.interval;
43
- this.until = rruleOrFrequency.until;
44
- this.count = rruleOrFrequency.count;
45
- this.byWeekday = rruleOrFrequency.byWeekday ?? [];
46
- this.byHour = rruleOrFrequency.byHour ?? [];
47
- this.byMinute = rruleOrFrequency.byMinute ?? [];
48
- this.bySecond = rruleOrFrequency.bySecond ?? [];
49
- this.byMonthday = rruleOrFrequency.byMonthday ?? [];
50
- this.bySetpos = rruleOrFrequency.bySetpos ?? [];
51
- this.byMonth = rruleOrFrequency.byMonth ?? [];
52
- this.byWeekno = rruleOrFrequency.byWeekno ?? [];
53
- this.byYearday = rruleOrFrequency.byYearday ?? [];
54
- this.weekstart = rruleOrFrequency.weekstart;
39
+ constructor(frequencyOrOptions) {
40
+ if (typeof frequencyOrOptions === 'object' && frequencyOrOptions !== null) {
41
+ this.frequency = frequencyOrOptions.frequency;
42
+ this.interval = frequencyOrOptions.interval;
43
+ this.until =
44
+ frequencyOrOptions.until &&
45
+ DateTime.fromPlain(frequencyOrOptions.until);
46
+ this.count = frequencyOrOptions.count;
47
+ this.byWeekday = frequencyOrOptions.byWeekday ?? [];
48
+ this.byHour = frequencyOrOptions.byHour ?? [];
49
+ this.byMinute = frequencyOrOptions.byMinute ?? [];
50
+ this.bySecond = frequencyOrOptions.bySecond ?? [];
51
+ this.byMonthday = frequencyOrOptions.byMonthday ?? [];
52
+ this.bySetpos = frequencyOrOptions.bySetpos ?? [];
53
+ this.byMonth = frequencyOrOptions.byMonth ?? [];
54
+ this.byWeekno = frequencyOrOptions.byWeekno ?? [];
55
+ this.byYearday = frequencyOrOptions.byYearday ?? [];
56
+ this.weekstart = frequencyOrOptions.weekstart;
55
57
  }
56
58
  else {
57
- this.frequency = rruleOrFrequency;
59
+ this.frequency = frequencyOrOptions;
58
60
  this.byWeekday = [];
59
61
  this.byHour = [];
60
62
  this.byMinute = [];
@@ -73,6 +75,24 @@ export class RRule {
73
75
  const rust = Rust.parse(str);
74
76
  return this.fromRust(rust);
75
77
  }
78
+ static fromPlain(rrule) {
79
+ return new this({
80
+ frequency: rrule.frequency,
81
+ interval: rrule.interval,
82
+ until: rrule.until && DateTime.fromPlain(rrule.until),
83
+ count: rrule.count,
84
+ byWeekday: rrule.byWeekday,
85
+ byHour: rrule.byHour,
86
+ byMinute: rrule.byMinute,
87
+ bySecond: rrule.bySecond,
88
+ byMonthday: rrule.byMonthday,
89
+ bySetpos: rrule.bySetpos,
90
+ byMonth: rrule.byMonth,
91
+ byWeekno: rrule.byWeekno,
92
+ byYearday: rrule.byYearday,
93
+ weekstart: rrule.weekstart,
94
+ });
95
+ }
76
96
  /**
77
97
  * @internal
78
98
  */
@@ -97,46 +117,46 @@ export class RRule {
97
117
  return rrule;
98
118
  }
99
119
  setFrequency(frequency) {
100
- return new RRule({ ...this.toObject(), frequency });
120
+ return new RRule({ ...this.toOptions(), frequency });
101
121
  }
102
122
  setInterval(interval) {
103
- return new RRule({ ...this.toObject(), interval });
123
+ return new RRule({ ...this.toOptions(), interval });
104
124
  }
105
125
  setCount(count) {
106
- return new RRule({ ...this.toObject(), count });
126
+ return new RRule({ ...this.toOptions(), count });
107
127
  }
108
128
  setByWeekday(weekdays) {
109
- return new RRule({ ...this.toObject(), byWeekday: weekdays });
129
+ return new RRule({ ...this.toOptions(), byWeekday: weekdays });
110
130
  }
111
131
  setByHour(hours) {
112
- return new RRule({ ...this.toObject(), byHour: hours });
132
+ return new RRule({ ...this.toOptions(), byHour: hours });
113
133
  }
114
134
  setByMinute(minutes) {
115
- return new RRule({ ...this.toObject(), byMinute: minutes });
135
+ return new RRule({ ...this.toOptions(), byMinute: minutes });
116
136
  }
117
137
  setBySecond(seconds) {
118
- return new RRule({ ...this.toObject(), bySecond: seconds });
138
+ return new RRule({ ...this.toOptions(), bySecond: seconds });
119
139
  }
120
140
  setByMonthday(days) {
121
- return new RRule({ ...this.toObject(), byMonthday: days });
141
+ return new RRule({ ...this.toOptions(), byMonthday: days });
122
142
  }
123
143
  setBySetpos(poses) {
124
- return new RRule({ ...this.toObject(), bySetpos: poses });
144
+ return new RRule({ ...this.toOptions(), bySetpos: poses });
125
145
  }
126
146
  setByMonth(months) {
127
- return new RRule({ ...this.toObject(), byMonth: months });
147
+ return new RRule({ ...this.toOptions(), byMonth: months });
128
148
  }
129
149
  setByWeekno(weekNumbers) {
130
- return new RRule({ ...this.toObject(), byWeekno: weekNumbers });
150
+ return new RRule({ ...this.toOptions(), byWeekno: weekNumbers });
131
151
  }
132
152
  setByYearday(days) {
133
- return new RRule({ ...this.toObject(), byYearday: days });
153
+ return new RRule({ ...this.toOptions(), byYearday: days });
134
154
  }
135
155
  setWeekstart(day) {
136
- return new RRule({ ...this.toObject(), weekstart: day });
156
+ return new RRule({ ...this.toOptions(), weekstart: day });
137
157
  }
138
158
  setUntil(datetime) {
139
- return new RRule({ ...this.toObject(), until: datetime });
159
+ return new RRule({ ...this.toOptions(), until: datetime });
140
160
  }
141
161
  toString() {
142
162
  return this.toRust().toString();
@@ -148,7 +168,25 @@ export class RRule {
148
168
  this.rust ??= new Rust(this.frequency, this.interval, this.count, this.weekstart, this.until?.toNumeric(), this.byWeekday, this.byHour, this.byMinute, this.bySecond, this.byMonthday, this.bySetpos, this.byMonth, this.byWeekno, this.byYearday);
149
169
  return this.rust;
150
170
  }
151
- toObject() {
171
+ toPlain() {
172
+ return {
173
+ frequency: this.frequency,
174
+ interval: this.interval,
175
+ count: this.count,
176
+ byWeekday: this.byWeekday,
177
+ byHour: this.byHour,
178
+ byMinute: this.byMinute,
179
+ bySecond: this.bySecond,
180
+ byMonthday: this.byMonthday,
181
+ bySetpos: this.bySetpos,
182
+ byMonth: this.byMonth,
183
+ byWeekno: this.byWeekno,
184
+ byYearday: this.byYearday,
185
+ weekstart: this.weekstart,
186
+ until: this.until?.toPlain(),
187
+ };
188
+ }
189
+ toOptions() {
152
190
  return {
153
191
  frequency: this.frequency,
154
192
  interval: this.interval,
@@ -5,9 +5,11 @@ export interface DateTimeLike {
5
5
  readonly hour: number;
6
6
  readonly minute: number;
7
7
  readonly second: number;
8
+ readonly utc: boolean;
8
9
  }
9
- export interface FromObjectOptions {
10
- utc?: boolean;
10
+ export type DateTimeLikeWithoutUtc = Omit<DateTimeLike, 'utc'>;
11
+ export interface ToPlainOptions {
12
+ stripUtc?: boolean;
11
13
  }
12
14
  /**
13
15
  * Represents a date and time. Either local or UTC.
@@ -37,7 +39,7 @@ export declare class DateTime implements DateTimeLike {
37
39
  /**
38
40
  * Creates a new DateTime object from the given plain object.
39
41
  */
40
- static fromObject(object: DateTimeLike, options?: FromObjectOptions): DateTime;
42
+ static fromPlain(plain: DateTimeLike): DateTime;
41
43
  /**
42
44
  * Creates a new DateTime object from provided string representation of a date according to RFC 5545.
43
45
  */
@@ -45,7 +47,13 @@ export declare class DateTime implements DateTimeLike {
45
47
  /**
46
48
  * Converts DateTime into a plain object.
47
49
  */
48
- toObject(): DateTimeLike;
50
+ toPlain(options: {
51
+ stripUtc: false;
52
+ } & ToPlainOptions): DateTimeLike;
53
+ toPlain(options: {
54
+ stripUtc: true;
55
+ } & ToPlainOptions): DateTimeLikeWithoutUtc;
56
+ toPlain(): DateTimeLike;
49
57
  /**
50
58
  * Converts DateTime into ISO 8601 string:
51
59
  * - `YYYYMMDDTHHMMSSZ` for UTC
@@ -57,8 +57,8 @@ class DateTime {
57
57
  /**
58
58
  * Creates a new DateTime object from the given plain object.
59
59
  */
60
- static fromObject(object, options) {
61
- return DateTime.create(object.year, object.month, object.day, object.hour, object.minute, object.second, !!options?.utc);
60
+ static fromPlain(plain) {
61
+ return DateTime.create(plain.year, plain.month, plain.day, plain.hour, plain.minute, plain.second, plain.utc);
62
62
  }
63
63
  /**
64
64
  * Creates a new DateTime object from provided string representation of a date according to RFC 5545.
@@ -89,10 +89,7 @@ class DateTime {
89
89
  static fromNumeric(numeric) {
90
90
  return new DateTime(numeric);
91
91
  }
92
- /**
93
- * Converts DateTime into a plain object.
94
- */
95
- toObject() {
92
+ toPlain(options) {
96
93
  return {
97
94
  year: this.year,
98
95
  month: this.month,
@@ -100,6 +97,7 @@ class DateTime {
100
97
  hour: this.hour,
101
98
  minute: this.minute,
102
99
  second: this.second,
100
+ utc: options?.stripUtc ? undefined : this.utc,
103
101
  };
104
102
  }
105
103
  /**
@@ -1,12 +1,20 @@
1
- import { RRule } from './rrule';
2
- import { DateTime } from './datetime';
3
- export interface RRuleSetLike {
1
+ import { RRule, type RRuleLike } from './rrule';
2
+ import { DateTime, type DateTimeLike } from './datetime';
3
+ export interface RRuleSetOptions {
4
4
  readonly dtstart: DateTime;
5
5
  readonly tzid?: string;
6
- readonly rrules: readonly RRule[];
7
- readonly exrules: readonly RRule[];
8
- readonly exdates: readonly DateTime[];
9
- readonly rdates: readonly DateTime[];
6
+ readonly rrules?: readonly RRule[];
7
+ readonly exrules?: readonly RRule[];
8
+ readonly exdates?: readonly DateTime[];
9
+ readonly rdates?: readonly DateTime[];
10
+ }
11
+ export interface RRuleSetLike {
12
+ readonly dtstart: DateTimeLike;
13
+ readonly tzid?: string;
14
+ readonly rrules: readonly RRuleLike[];
15
+ readonly exrules: readonly RRuleLike[];
16
+ readonly exdates: readonly DateTimeLike[];
17
+ readonly rdates: readonly DateTimeLike[];
10
18
  }
11
19
  export declare class RRuleSet implements Iterable<DateTime> {
12
20
  readonly dtstart: DateTime;
@@ -15,12 +23,13 @@ export declare class RRuleSet implements Iterable<DateTime> {
15
23
  readonly exrules: readonly RRule[];
16
24
  readonly exdates: readonly DateTime[];
17
25
  readonly rdates: readonly DateTime[];
18
- constructor(dtstart: DateTime, tzid?: string);
19
- constructor(options: Partial<RRuleSetLike>);
26
+ constructor(dtstart: DateTime | DateTimeLike, tzid?: string);
27
+ constructor(options: RRuleSetOptions);
20
28
  /**
21
29
  * Parses a string into an RRuleSet.
22
30
  */
23
31
  static parse(str: string): RRuleSet;
32
+ static fromPlain(plain: RRuleSetLike): RRuleSet;
24
33
  setDtstart(dtstart: DateTime): RRuleSet;
25
34
  setTzid(tzid: string): RRuleSet;
26
35
  addRrule(rrule: RRule): RRuleSet;
@@ -55,6 +64,7 @@ export declare class RRuleSet implements Iterable<DateTime> {
55
64
  /**
56
65
  * Converts the RRuleSet to a plain object.
57
66
  */
58
- toObject(): RRuleSetLike;
67
+ toPlain(): RRuleSetLike;
59
68
  [Symbol.iterator](): Iterator<DateTime, any, any>;
69
+ private toOptions;
60
70
  }
@@ -5,24 +5,20 @@ const rrule_1 = require("./rrule");
5
5
  const lib_1 = require("./lib");
6
6
  const datetime_1 = require("./datetime");
7
7
  class RRuleSet {
8
- constructor(setOrDtstart, tzid) {
9
- if (!(setOrDtstart instanceof datetime_1.DateTime)) {
10
- if (setOrDtstart?.dtstart) {
11
- this.dtstart = setOrDtstart?.dtstart;
12
- }
13
- else {
14
- const date = new Date();
15
- this.dtstart = datetime_1.DateTime.create(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), true);
16
- }
17
- this.tzid = setOrDtstart?.tzid ?? 'UTC';
18
- this.rrules = setOrDtstart?.rrules ?? [];
19
- this.exrules = setOrDtstart?.exrules ?? [];
20
- this.exdates = setOrDtstart?.exdates ?? [];
21
- this.rdates = setOrDtstart?.rdates ?? [];
8
+ constructor(optionsOrDtstart, tzid) {
9
+ if (optionsOrDtstart !== undefined &&
10
+ !(optionsOrDtstart instanceof datetime_1.DateTime) &&
11
+ 'dtstart' in optionsOrDtstart) {
12
+ this.dtstart = optionsOrDtstart.dtstart;
13
+ this.tzid = optionsOrDtstart.tzid ?? 'UTC';
14
+ this.rrules = optionsOrDtstart?.rrules ?? [];
15
+ this.exrules = optionsOrDtstart?.exrules ?? [];
16
+ this.exdates = optionsOrDtstart?.exdates ?? [];
17
+ this.rdates = optionsOrDtstart?.rdates ?? [];
22
18
  }
23
- else if (setOrDtstart instanceof datetime_1.DateTime && typeof tzid === 'string') {
24
- this.dtstart = setOrDtstart;
25
- this.tzid = tzid;
19
+ else if (optionsOrDtstart instanceof datetime_1.DateTime) {
20
+ this.dtstart = optionsOrDtstart;
21
+ this.tzid = tzid ?? 'UTC';
26
22
  this.rrules = [];
27
23
  this.exrules = [];
28
24
  this.exdates = [];
@@ -39,6 +35,16 @@ class RRuleSet {
39
35
  const rust = lib_1.RRuleSet.parse(str);
40
36
  return this.fromRust(rust);
41
37
  }
38
+ static fromPlain(plain) {
39
+ return new RRuleSet({
40
+ dtstart: datetime_1.DateTime.fromPlain(plain.dtstart),
41
+ tzid: plain.tzid,
42
+ rrules: plain.rrules.map((rrule) => rrule_1.RRule.fromPlain(rrule)),
43
+ exrules: plain.exrules.map((rrule) => rrule_1.RRule.fromPlain(rrule)),
44
+ exdates: plain.exdates.map((datetime) => datetime_1.DateTime.fromPlain(datetime)),
45
+ rdates: plain.rdates.map((datetime) => datetime_1.DateTime.fromPlain(datetime)),
46
+ });
47
+ }
42
48
  /**
43
49
  * @internal
44
50
  */
@@ -56,61 +62,61 @@ class RRuleSet {
56
62
  }
57
63
  setDtstart(dtstart) {
58
64
  return new RRuleSet({
59
- ...this.toObject(),
65
+ ...this.toOptions(),
60
66
  dtstart: dtstart,
61
67
  });
62
68
  }
63
69
  setTzid(tzid) {
64
70
  return new RRuleSet({
65
- ...this.toObject(),
71
+ ...this.toOptions(),
66
72
  tzid: tzid,
67
73
  });
68
74
  }
69
75
  addRrule(rrule) {
70
76
  return new RRuleSet({
71
- ...this.toObject(),
77
+ ...this.toOptions(),
72
78
  rrules: [...this.rrules, rrule],
73
79
  });
74
80
  }
75
81
  setRrules(rrules) {
76
82
  return new RRuleSet({
77
- ...this.toObject(),
83
+ ...this.toOptions(),
78
84
  rrules: rrules,
79
85
  });
80
86
  }
81
87
  addExrule(rrule) {
82
88
  return new RRuleSet({
83
- ...this.toObject(),
89
+ ...this.toOptions(),
84
90
  exrules: [...this.exrules, rrule],
85
91
  });
86
92
  }
87
93
  setExrules(rrules) {
88
94
  return new RRuleSet({
89
- ...this.toObject(),
95
+ ...this.toOptions(),
90
96
  exrules: rrules,
91
97
  });
92
98
  }
93
99
  addExdate(datetime) {
94
100
  return new RRuleSet({
95
- ...this.toObject(),
101
+ ...this.toOptions(),
96
102
  exdates: [...this.exdates, datetime],
97
103
  });
98
104
  }
99
105
  setExdates(datetimes) {
100
106
  return new RRuleSet({
101
- ...this.toObject(),
107
+ ...this.toOptions(),
102
108
  exdates: datetimes,
103
109
  });
104
110
  }
105
111
  addRdate(datetime) {
106
112
  return new RRuleSet({
107
- ...this.toObject(),
113
+ ...this.toOptions(),
108
114
  rdates: [...this.rdates, datetime],
109
115
  });
110
116
  }
111
117
  setRdates(datetimes) {
112
118
  return new RRuleSet({
113
- ...this.toObject(),
119
+ ...this.toOptions(),
114
120
  rdates: datetimes,
115
121
  });
116
122
  }
@@ -157,14 +163,14 @@ class RRuleSet {
157
163
  /**
158
164
  * Converts the RRuleSet to a plain object.
159
165
  */
160
- toObject() {
166
+ toPlain() {
161
167
  return {
162
- dtstart: this.dtstart,
168
+ dtstart: this.dtstart.toPlain(),
163
169
  tzid: this.tzid,
164
- rrules: this.rrules,
165
- exrules: this.exrules,
166
- exdates: this.exdates,
167
- rdates: this.rdates,
170
+ rrules: this.rrules.map((rrule) => rrule.toPlain()),
171
+ exrules: this.exrules.map((rrule) => rrule.toPlain()),
172
+ exdates: this.exdates.map((rrule) => rrule.toPlain()),
173
+ rdates: this.rdates.map((rrule) => rrule.toPlain()),
168
174
  };
169
175
  }
170
176
  [Symbol.iterator]() {
@@ -185,5 +191,15 @@ class RRuleSet {
185
191
  },
186
192
  };
187
193
  }
194
+ toOptions() {
195
+ return {
196
+ dtstart: this.dtstart,
197
+ tzid: this.tzid,
198
+ rrules: this.rrules,
199
+ exrules: this.exrules,
200
+ exdates: this.exdates,
201
+ rdates: this.rdates,
202
+ };
203
+ }
188
204
  }
189
205
  exports.RRuleSet = RRuleSet;
@@ -1,4 +1,4 @@
1
- import { DateTime } from './datetime';
1
+ import { DateTime, type DateTimeLike } from './datetime';
2
2
  export interface NWeekday {
3
3
  /**
4
4
  * If set, this represents the nth occurrence of the weekday.
@@ -41,11 +41,27 @@ export declare enum Weekday {
41
41
  Saturday = 5,
42
42
  Sunday = 6
43
43
  }
44
- export interface RRuleLike {
44
+ export interface RRuleOptions {
45
45
  readonly frequency: Frequency;
46
46
  readonly interval?: number;
47
47
  readonly count?: number;
48
48
  readonly until?: DateTime;
49
+ readonly byWeekday?: readonly (NWeekday | Weekday)[];
50
+ readonly byHour?: readonly number[];
51
+ readonly byMinute?: readonly number[];
52
+ readonly bySecond?: readonly number[];
53
+ readonly byMonthday?: readonly number[];
54
+ readonly bySetpos?: readonly number[];
55
+ readonly byMonth?: readonly Month[];
56
+ readonly byWeekno?: readonly number[];
57
+ readonly byYearday?: readonly number[];
58
+ readonly weekstart?: Weekday;
59
+ }
60
+ export interface RRuleLike {
61
+ readonly frequency: Frequency;
62
+ readonly interval?: number;
63
+ readonly count?: number;
64
+ readonly until?: DateTimeLike;
49
65
  readonly byWeekday: readonly (NWeekday | Weekday)[];
50
66
  readonly byHour: readonly number[];
51
67
  readonly byMinute: readonly number[];
@@ -73,11 +89,12 @@ export declare class RRule {
73
89
  readonly byYearday: readonly number[];
74
90
  readonly weekstart?: Weekday;
75
91
  constructor(frequency: Frequency);
76
- constructor(rrule?: Partial<RRuleLike>);
92
+ constructor(options: RRuleOptions);
77
93
  /**
78
94
  * Parses a string into an RRule.
79
95
  */
80
96
  static parse(str: string): RRule;
97
+ static fromPlain(rrule: RRuleLike): RRule;
81
98
  setFrequency(frequency: Frequency): RRule;
82
99
  setInterval(interval: number): RRule;
83
100
  setCount(count: number): RRule;
@@ -93,5 +110,6 @@ export declare class RRule {
93
110
  setWeekstart(day: Weekday): RRule;
94
111
  setUntil(datetime: DateTime): RRule;
95
112
  toString(): string;
96
- toObject(): RRuleLike;
113
+ toPlain(): RRuleLike;
114
+ private toOptions;
97
115
  }
@@ -39,25 +39,27 @@ var Weekday;
39
39
  Weekday[Weekday["Sunday"] = 6] = "Sunday";
40
40
  })(Weekday || (exports.Weekday = Weekday = {}));
41
41
  class RRule {
42
- constructor(rruleOrFrequency = {}) {
43
- if (typeof rruleOrFrequency === 'object' && rruleOrFrequency !== null) {
44
- this.frequency = rruleOrFrequency.frequency ?? Frequency.Daily;
45
- this.interval = rruleOrFrequency.interval;
46
- this.until = rruleOrFrequency.until;
47
- this.count = rruleOrFrequency.count;
48
- this.byWeekday = rruleOrFrequency.byWeekday ?? [];
49
- this.byHour = rruleOrFrequency.byHour ?? [];
50
- this.byMinute = rruleOrFrequency.byMinute ?? [];
51
- this.bySecond = rruleOrFrequency.bySecond ?? [];
52
- this.byMonthday = rruleOrFrequency.byMonthday ?? [];
53
- this.bySetpos = rruleOrFrequency.bySetpos ?? [];
54
- this.byMonth = rruleOrFrequency.byMonth ?? [];
55
- this.byWeekno = rruleOrFrequency.byWeekno ?? [];
56
- this.byYearday = rruleOrFrequency.byYearday ?? [];
57
- this.weekstart = rruleOrFrequency.weekstart;
42
+ constructor(frequencyOrOptions) {
43
+ if (typeof frequencyOrOptions === 'object' && frequencyOrOptions !== null) {
44
+ this.frequency = frequencyOrOptions.frequency;
45
+ this.interval = frequencyOrOptions.interval;
46
+ this.until =
47
+ frequencyOrOptions.until &&
48
+ datetime_1.DateTime.fromPlain(frequencyOrOptions.until);
49
+ this.count = frequencyOrOptions.count;
50
+ this.byWeekday = frequencyOrOptions.byWeekday ?? [];
51
+ this.byHour = frequencyOrOptions.byHour ?? [];
52
+ this.byMinute = frequencyOrOptions.byMinute ?? [];
53
+ this.bySecond = frequencyOrOptions.bySecond ?? [];
54
+ this.byMonthday = frequencyOrOptions.byMonthday ?? [];
55
+ this.bySetpos = frequencyOrOptions.bySetpos ?? [];
56
+ this.byMonth = frequencyOrOptions.byMonth ?? [];
57
+ this.byWeekno = frequencyOrOptions.byWeekno ?? [];
58
+ this.byYearday = frequencyOrOptions.byYearday ?? [];
59
+ this.weekstart = frequencyOrOptions.weekstart;
58
60
  }
59
61
  else {
60
- this.frequency = rruleOrFrequency;
62
+ this.frequency = frequencyOrOptions;
61
63
  this.byWeekday = [];
62
64
  this.byHour = [];
63
65
  this.byMinute = [];
@@ -76,6 +78,24 @@ class RRule {
76
78
  const rust = lib_1.RRule.parse(str);
77
79
  return this.fromRust(rust);
78
80
  }
81
+ static fromPlain(rrule) {
82
+ return new this({
83
+ frequency: rrule.frequency,
84
+ interval: rrule.interval,
85
+ until: rrule.until && datetime_1.DateTime.fromPlain(rrule.until),
86
+ count: rrule.count,
87
+ byWeekday: rrule.byWeekday,
88
+ byHour: rrule.byHour,
89
+ byMinute: rrule.byMinute,
90
+ bySecond: rrule.bySecond,
91
+ byMonthday: rrule.byMonthday,
92
+ bySetpos: rrule.bySetpos,
93
+ byMonth: rrule.byMonth,
94
+ byWeekno: rrule.byWeekno,
95
+ byYearday: rrule.byYearday,
96
+ weekstart: rrule.weekstart,
97
+ });
98
+ }
79
99
  /**
80
100
  * @internal
81
101
  */
@@ -100,46 +120,46 @@ class RRule {
100
120
  return rrule;
101
121
  }
102
122
  setFrequency(frequency) {
103
- return new RRule({ ...this.toObject(), frequency });
123
+ return new RRule({ ...this.toOptions(), frequency });
104
124
  }
105
125
  setInterval(interval) {
106
- return new RRule({ ...this.toObject(), interval });
126
+ return new RRule({ ...this.toOptions(), interval });
107
127
  }
108
128
  setCount(count) {
109
- return new RRule({ ...this.toObject(), count });
129
+ return new RRule({ ...this.toOptions(), count });
110
130
  }
111
131
  setByWeekday(weekdays) {
112
- return new RRule({ ...this.toObject(), byWeekday: weekdays });
132
+ return new RRule({ ...this.toOptions(), byWeekday: weekdays });
113
133
  }
114
134
  setByHour(hours) {
115
- return new RRule({ ...this.toObject(), byHour: hours });
135
+ return new RRule({ ...this.toOptions(), byHour: hours });
116
136
  }
117
137
  setByMinute(minutes) {
118
- return new RRule({ ...this.toObject(), byMinute: minutes });
138
+ return new RRule({ ...this.toOptions(), byMinute: minutes });
119
139
  }
120
140
  setBySecond(seconds) {
121
- return new RRule({ ...this.toObject(), bySecond: seconds });
141
+ return new RRule({ ...this.toOptions(), bySecond: seconds });
122
142
  }
123
143
  setByMonthday(days) {
124
- return new RRule({ ...this.toObject(), byMonthday: days });
144
+ return new RRule({ ...this.toOptions(), byMonthday: days });
125
145
  }
126
146
  setBySetpos(poses) {
127
- return new RRule({ ...this.toObject(), bySetpos: poses });
147
+ return new RRule({ ...this.toOptions(), bySetpos: poses });
128
148
  }
129
149
  setByMonth(months) {
130
- return new RRule({ ...this.toObject(), byMonth: months });
150
+ return new RRule({ ...this.toOptions(), byMonth: months });
131
151
  }
132
152
  setByWeekno(weekNumbers) {
133
- return new RRule({ ...this.toObject(), byWeekno: weekNumbers });
153
+ return new RRule({ ...this.toOptions(), byWeekno: weekNumbers });
134
154
  }
135
155
  setByYearday(days) {
136
- return new RRule({ ...this.toObject(), byYearday: days });
156
+ return new RRule({ ...this.toOptions(), byYearday: days });
137
157
  }
138
158
  setWeekstart(day) {
139
- return new RRule({ ...this.toObject(), weekstart: day });
159
+ return new RRule({ ...this.toOptions(), weekstart: day });
140
160
  }
141
161
  setUntil(datetime) {
142
- return new RRule({ ...this.toObject(), until: datetime });
162
+ return new RRule({ ...this.toOptions(), until: datetime });
143
163
  }
144
164
  toString() {
145
165
  return this.toRust().toString();
@@ -151,7 +171,25 @@ class RRule {
151
171
  this.rust ??= new lib_1.RRule(this.frequency, this.interval, this.count, this.weekstart, this.until?.toNumeric(), this.byWeekday, this.byHour, this.byMinute, this.bySecond, this.byMonthday, this.bySetpos, this.byMonth, this.byWeekno, this.byYearday);
152
172
  return this.rust;
153
173
  }
154
- toObject() {
174
+ toPlain() {
175
+ return {
176
+ frequency: this.frequency,
177
+ interval: this.interval,
178
+ count: this.count,
179
+ byWeekday: this.byWeekday,
180
+ byHour: this.byHour,
181
+ byMinute: this.byMinute,
182
+ bySecond: this.bySecond,
183
+ byMonthday: this.byMonthday,
184
+ bySetpos: this.bySetpos,
185
+ byMonth: this.byMonth,
186
+ byWeekno: this.byWeekno,
187
+ byYearday: this.byYearday,
188
+ weekstart: this.weekstart,
189
+ until: this.until?.toPlain(),
190
+ };
191
+ }
192
+ toOptions() {
155
193
  return {
156
194
  frequency: this.frequency,
157
195
  interval: this.interval,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rrule-rust",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.5",
4
4
  "main": "dist/node/index.js",
5
5
  "browser": "dist/browser/index.js",
6
6
  "keywords": [
@@ -96,19 +96,19 @@
96
96
  "prepublishOnly": "npm run build && napi prepublish -t npm"
97
97
  },
98
98
  "optionalDependencies": {
99
- "@rrule-rust/lib-win32-x64-msvc": "3.0.0-alpha.3",
100
- "@rrule-rust/lib-darwin-x64": "3.0.0-alpha.3",
101
- "@rrule-rust/lib-linux-x64-gnu": "3.0.0-alpha.3",
102
- "@rrule-rust/lib-linux-x64-musl": "3.0.0-alpha.3",
103
- "@rrule-rust/lib-linux-arm64-gnu": "3.0.0-alpha.3",
104
- "@rrule-rust/lib-win32-ia32-msvc": "3.0.0-alpha.3",
105
- "@rrule-rust/lib-linux-arm-gnueabihf": "3.0.0-alpha.3",
106
- "@rrule-rust/lib-darwin-arm64": "3.0.0-alpha.3",
107
- "@rrule-rust/lib-android-arm64": "3.0.0-alpha.3",
108
- "@rrule-rust/lib-freebsd-x64": "3.0.0-alpha.3",
109
- "@rrule-rust/lib-linux-arm64-musl": "3.0.0-alpha.3",
110
- "@rrule-rust/lib-win32-arm64-msvc": "3.0.0-alpha.3",
111
- "@rrule-rust/lib-android-arm-eabi": "3.0.0-alpha.3",
112
- "@rrule-rust/lib-wasm32-wasi": "3.0.0-alpha.3"
99
+ "@rrule-rust/lib-win32-x64-msvc": "3.0.0-alpha.5",
100
+ "@rrule-rust/lib-darwin-x64": "3.0.0-alpha.5",
101
+ "@rrule-rust/lib-linux-x64-gnu": "3.0.0-alpha.5",
102
+ "@rrule-rust/lib-linux-x64-musl": "3.0.0-alpha.5",
103
+ "@rrule-rust/lib-linux-arm64-gnu": "3.0.0-alpha.5",
104
+ "@rrule-rust/lib-win32-ia32-msvc": "3.0.0-alpha.5",
105
+ "@rrule-rust/lib-linux-arm-gnueabihf": "3.0.0-alpha.5",
106
+ "@rrule-rust/lib-darwin-arm64": "3.0.0-alpha.5",
107
+ "@rrule-rust/lib-android-arm64": "3.0.0-alpha.5",
108
+ "@rrule-rust/lib-freebsd-x64": "3.0.0-alpha.5",
109
+ "@rrule-rust/lib-linux-arm64-musl": "3.0.0-alpha.5",
110
+ "@rrule-rust/lib-win32-arm64-msvc": "3.0.0-alpha.5",
111
+ "@rrule-rust/lib-android-arm-eabi": "3.0.0-alpha.5",
112
+ "@rrule-rust/lib-wasm32-wasi": "3.0.0-alpha.5"
113
113
  }
114
114
  }