Skip to content

Commit 848f6f1

Browse files
authored
feat: do not accept plain objects in constructors (#502)
relates-to: #500
1 parent 5d1304b commit 848f6f1

File tree

3 files changed

+84
-70
lines changed

3 files changed

+84
-70
lines changed

src/datetime.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,9 @@ export class DateTime implements DateTimeLike {
156156
return new DateTime(numeric);
157157
}
158158

159-
/** @internal */
160-
public static fromPlainOrInstance(
161-
datetime: DateTime | DateTimeLike,
162-
): DateTime {
163-
return datetime instanceof DateTime ? datetime : this.fromPlain(datetime);
164-
}
165-
166159
/**
167160
* Converts DateTime into a plain object.
168161
*/
169-
170162
public toPlain(options: { stripUtc: false } & ToPlainOptions): DateTimeLike;
171163
public toPlain(
172164
options: { stripUtc: true } & ToPlainOptions,

src/rrule-set.ts

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { RRuleSet as Rust } from './lib';
33
import { DateTime, type DateTimeLike } from './datetime';
44

55
export interface RRuleSetOptions {
6-
readonly dtstart: DateTime | DateTimeLike;
6+
readonly dtstart: DateTime;
77
readonly tzid?: string;
8-
readonly rrules?: readonly (RRule | RRuleLike)[];
9-
readonly exrules?: readonly (RRule | RRuleLike)[];
10-
readonly exdates?: readonly (DateTime | DateTimeLike)[];
11-
readonly rdates?: readonly (DateTime | DateTimeLike)[];
8+
readonly rrules?: readonly RRule[];
9+
readonly exrules?: readonly RRule[];
10+
readonly exdates?: readonly DateTime[];
11+
readonly rdates?: readonly DateTime[];
1212
}
1313

1414
export interface RRuleSetLike {
@@ -42,22 +42,14 @@ export class RRuleSet implements Iterable<DateTime> {
4242
!(optionsOrDtstart instanceof DateTime) &&
4343
'dtstart' in optionsOrDtstart
4444
) {
45-
this.dtstart = DateTime.fromPlainOrInstance(optionsOrDtstart.dtstart);
45+
this.dtstart = optionsOrDtstart.dtstart;
4646

4747
this.tzid = optionsOrDtstart.tzid ?? 'UTC';
4848

49-
this.rrules = (optionsOrDtstart?.rrules ?? []).map(
50-
RRule.fromPlainOrInstance.bind(RRule),
51-
);
52-
this.exrules = (optionsOrDtstart?.exrules ?? []).map(
53-
RRule.fromPlainOrInstance.bind(RRule),
54-
);
55-
this.exdates = (optionsOrDtstart?.exdates ?? []).map(
56-
DateTime.fromPlainOrInstance.bind(DateTime),
57-
);
58-
this.rdates = (optionsOrDtstart?.rdates ?? []).map(
59-
DateTime.fromPlainOrInstance.bind(DateTime),
60-
);
49+
this.rrules = optionsOrDtstart?.rrules ?? [];
50+
this.exrules = optionsOrDtstart?.exrules ?? [];
51+
this.exdates = optionsOrDtstart?.exdates ?? [];
52+
this.rdates = optionsOrDtstart?.rdates ?? [];
6153
} else if (optionsOrDtstart instanceof DateTime) {
6254
this.dtstart = optionsOrDtstart;
6355
this.tzid = tzid ?? 'UTC';
@@ -81,7 +73,14 @@ export class RRuleSet implements Iterable<DateTime> {
8173
}
8274

8375
public static fromPlain(plain: RRuleSetLike): RRuleSet {
84-
return new RRuleSet(plain);
76+
return new RRuleSet({
77+
dtstart: DateTime.fromPlain(plain.dtstart),
78+
tzid: plain.tzid,
79+
rrules: plain.rrules.map((rrule) => RRule.fromPlain(rrule)),
80+
exrules: plain.exrules.map((rrule) => RRule.fromPlain(rrule)),
81+
exdates: plain.exdates.map((datetime) => DateTime.fromPlain(datetime)),
82+
rdates: plain.rdates.map((datetime) => DateTime.fromPlain(datetime)),
83+
});
8584
}
8685

8786
/**
@@ -102,72 +101,72 @@ export class RRuleSet implements Iterable<DateTime> {
102101
return set;
103102
}
104103

105-
public setDtstart(dtstart: DateTime | DateTimeLike): RRuleSet {
104+
public setDtstart(dtstart: DateTime): RRuleSet {
106105
return new RRuleSet({
107-
...this.toPlain(),
106+
...this.toOptions(),
108107
dtstart: dtstart,
109108
});
110109
}
111110

112111
public setTzid(tzid: string): RRuleSet {
113112
return new RRuleSet({
114-
...this.toPlain(),
113+
...this.toOptions(),
115114
tzid: tzid,
116115
});
117116
}
118117

119-
public addRrule(rrule: RRule | RRuleLike): RRuleSet {
118+
public addRrule(rrule: RRule): RRuleSet {
120119
return new RRuleSet({
121-
...this.toPlain(),
120+
...this.toOptions(),
122121
rrules: [...this.rrules, rrule],
123122
});
124123
}
125124

126-
public setRrules(rrules: readonly (RRule | RRuleLike)[]): RRuleSet {
125+
public setRrules(rrules: readonly RRule[]): RRuleSet {
127126
return new RRuleSet({
128-
...this.toPlain(),
127+
...this.toOptions(),
129128
rrules: rrules,
130129
});
131130
}
132131

133-
public addExrule(rrule: RRule | RRuleLike): RRuleSet {
132+
public addExrule(rrule: RRule): RRuleSet {
134133
return new RRuleSet({
135-
...this.toPlain(),
134+
...this.toOptions(),
136135
exrules: [...this.exrules, rrule],
137136
});
138137
}
139138

140-
public setExrules(rrules: readonly (RRule | RRuleLike)[]): RRuleSet {
139+
public setExrules(rrules: readonly RRule[]): RRuleSet {
141140
return new RRuleSet({
142-
...this.toPlain(),
141+
...this.toOptions(),
143142
exrules: rrules,
144143
});
145144
}
146145

147-
public addExdate(datetime: DateTime | DateTimeLike): RRuleSet {
146+
public addExdate(datetime: DateTime): RRuleSet {
148147
return new RRuleSet({
149-
...this.toPlain(),
148+
...this.toOptions(),
150149
exdates: [...this.exdates, datetime],
151150
});
152151
}
153152

154-
public setExdates(datetimes: readonly (DateTime | DateTimeLike)[]): RRuleSet {
153+
public setExdates(datetimes: readonly DateTime[]): RRuleSet {
155154
return new RRuleSet({
156-
...this.toPlain(),
155+
...this.toOptions(),
157156
exdates: datetimes,
158157
});
159158
}
160159

161-
public addRdate(datetime: DateTime | DateTimeLike): RRuleSet {
160+
public addRdate(datetime: DateTime): RRuleSet {
162161
return new RRuleSet({
163-
...this.toPlain(),
162+
...this.toOptions(),
164163
rdates: [...this.rdates, datetime],
165164
});
166165
}
167166

168-
public setRdates(datetimes: readonly (DateTime | DateTimeLike)[]): RRuleSet {
167+
public setRdates(datetimes: readonly DateTime[]): RRuleSet {
169168
return new RRuleSet({
170-
...this.toPlain(),
169+
...this.toOptions(),
171170
rdates: datetimes,
172171
});
173172
}
@@ -264,4 +263,15 @@ export class RRuleSet implements Iterable<DateTime> {
264263
},
265264
};
266265
}
266+
267+
private toOptions(): RRuleSetOptions {
268+
return {
269+
dtstart: this.dtstart,
270+
tzid: this.tzid,
271+
rrules: this.rrules,
272+
exrules: this.exrules,
273+
exdates: this.exdates,
274+
rdates: this.rdates,
275+
};
276+
}
267277
}

src/rrule.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface RRuleOptions {
5151
readonly frequency: Frequency;
5252
readonly interval?: number;
5353
readonly count?: number;
54-
readonly until?: DateTime | DateTimeLike;
54+
readonly until?: DateTime;
5555
readonly byWeekday?: readonly (NWeekday | Weekday)[];
5656
readonly byHour?: readonly number[];
5757
readonly byMinute?: readonly number[];
@@ -147,7 +147,7 @@ export class RRule {
147147
return new this({
148148
frequency: rrule.frequency,
149149
interval: rrule.interval,
150-
until: rrule.until,
150+
until: rrule.until && DateTime.fromPlain(rrule.until),
151151
count: rrule.count,
152152
byWeekday: rrule.byWeekday,
153153
byHour: rrule.byHour,
@@ -188,67 +188,60 @@ export class RRule {
188188
return rrule;
189189
}
190190

191-
/**
192-
* @internal
193-
*/
194-
public static fromPlainOrInstance(rrule: RRule | RRuleLike): RRule {
195-
return rrule instanceof RRule ? rrule : this.fromPlain(rrule);
196-
}
197-
198191
public setFrequency(frequency: Frequency): RRule {
199-
return new RRule({ ...this.toPlain(), frequency });
192+
return new RRule({ ...this.toOptions(), frequency });
200193
}
201194

202195
public setInterval(interval: number): RRule {
203-
return new RRule({ ...this.toPlain(), interval });
196+
return new RRule({ ...this.toOptions(), interval });
204197
}
205198

206199
public setCount(count: number): RRule {
207-
return new RRule({ ...this.toPlain(), count });
200+
return new RRule({ ...this.toOptions(), count });
208201
}
209202

210203
public setByWeekday(weekdays: readonly (NWeekday | Weekday)[]): RRule {
211-
return new RRule({ ...this.toPlain(), byWeekday: weekdays });
204+
return new RRule({ ...this.toOptions(), byWeekday: weekdays });
212205
}
213206

214207
public setByHour(hours: readonly number[]): RRule {
215-
return new RRule({ ...this.toPlain(), byHour: hours });
208+
return new RRule({ ...this.toOptions(), byHour: hours });
216209
}
217210

218211
public setByMinute(minutes: readonly number[]): RRule {
219-
return new RRule({ ...this.toPlain(), byMinute: minutes });
212+
return new RRule({ ...this.toOptions(), byMinute: minutes });
220213
}
221214

222215
public setBySecond(seconds: readonly number[]): RRule {
223-
return new RRule({ ...this.toPlain(), bySecond: seconds });
216+
return new RRule({ ...this.toOptions(), bySecond: seconds });
224217
}
225218

226219
public setByMonthday(days: readonly number[]): RRule {
227-
return new RRule({ ...this.toPlain(), byMonthday: days });
220+
return new RRule({ ...this.toOptions(), byMonthday: days });
228221
}
229222

230223
public setBySetpos(poses: readonly number[]): RRule {
231-
return new RRule({ ...this.toPlain(), bySetpos: poses });
224+
return new RRule({ ...this.toOptions(), bySetpos: poses });
232225
}
233226

234227
public setByMonth(months: readonly Month[]): RRule {
235-
return new RRule({ ...this.toPlain(), byMonth: months });
228+
return new RRule({ ...this.toOptions(), byMonth: months });
236229
}
237230

238231
public setByWeekno(weekNumbers: readonly number[]): RRule {
239-
return new RRule({ ...this.toPlain(), byWeekno: weekNumbers });
232+
return new RRule({ ...this.toOptions(), byWeekno: weekNumbers });
240233
}
241234

242235
public setByYearday(days: readonly number[]): RRule {
243-
return new RRule({ ...this.toPlain(), byYearday: days });
236+
return new RRule({ ...this.toOptions(), byYearday: days });
244237
}
245238

246239
public setWeekstart(day: Weekday): RRule {
247-
return new RRule({ ...this.toPlain(), weekstart: day });
240+
return new RRule({ ...this.toOptions(), weekstart: day });
248241
}
249242

250-
public setUntil(datetime: DateTime | DateTimeLike): RRule {
251-
return new RRule({ ...this.toPlain(), until: datetime });
243+
public setUntil(datetime: DateTime): RRule {
244+
return new RRule({ ...this.toOptions(), until: datetime });
252245
}
253246

254247
public toString(): string {
@@ -297,4 +290,23 @@ export class RRule {
297290
until: this.until?.toPlain(),
298291
};
299292
}
293+
294+
private toOptions(): RRuleOptions {
295+
return {
296+
frequency: this.frequency,
297+
interval: this.interval,
298+
count: this.count,
299+
byWeekday: this.byWeekday,
300+
byHour: this.byHour,
301+
byMinute: this.byMinute,
302+
bySecond: this.bySecond,
303+
byMonthday: this.byMonthday,
304+
bySetpos: this.bySetpos,
305+
byMonth: this.byMonth,
306+
byWeekno: this.byWeekno,
307+
byYearday: this.byYearday,
308+
weekstart: this.weekstart,
309+
until: this.until,
310+
};
311+
}
300312
}

0 commit comments

Comments
 (0)