Skip to content

Commit

Permalink
fix(Event): Run start/end validation only when getting data
Browse files Browse the repository at this point in the history
close #581
  • Loading branch information
sebbo2002 committed Feb 29, 2024
1 parent 8d3a55d commit 9174a32
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,11 @@ export default class ICalEvent {
start(start: ICalDateTimeValue): this;
start(start?: ICalDateTimeValue): this | ICalDateTimeValue {
if (start === undefined) {
this.swapStartAndEndIfRequired();
return this.data.start;
}

this.data.start = checkDate(start, 'start');
if (this.data.start && this.data.end && toDate(this.data.start).getTime() > toDate(this.data.end).getTime()) {
const t = this.data.start;
this.data.start = this.data.end;
this.data.end = t;
}

return this;
}

Expand All @@ -391,6 +386,7 @@ export default class ICalEvent {
end(end: ICalDateTimeValue | null): this;
end(end?: ICalDateTimeValue | null): this | ICalDateTimeValue | null {
if (end === undefined) {
this.swapStartAndEndIfRequired();
return this.data.end;
}
if (end === null) {
Expand All @@ -399,13 +395,19 @@ export default class ICalEvent {
}

this.data.end = checkDate(end, 'end');
return this;
}

/**
* Checks if the start date is after the end date and swaps them if necessary.
* @private
*/
private swapStartAndEndIfRequired(): void {
if (this.data.start && this.data.end && toDate(this.data.start).getTime() > toDate(this.data.end).getTime()) {
const t = this.data.start;
this.data.start = this.data.end;
this.data.end = t;
}

return this;
}

/**
Expand Down Expand Up @@ -1629,6 +1631,7 @@ export default class ICalEvent {
});
}

this.swapStartAndEndIfRequired();
return Object.assign({}, this.data, {
start: toJSON(this.data.start) || null,
end: toJSON(this.data.end) || null,
Expand Down Expand Up @@ -1660,6 +1663,7 @@ export default class ICalEvent {
// SEQUENCE
g += 'SEQUENCE:' + this.data.sequence + '\r\n';

this.swapStartAndEndIfRequired();
g += 'DTSTAMP:' + formatDate(this.calendar.timezone(), this.data.stamp) + '\r\n';
if (this.data.allDay) {
g += 'DTSTART;VALUE=DATE:' + formatDate(this.calendar.timezone(), this.data.start, true) + '\r\n';
Expand Down
22 changes: 22 additions & 0 deletions test/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,26 @@ describe('Issues', function () {
].join('\r\n'));
});
});

describe('Issue #581', function () {
it('event.start and event.end should be swappable', function () {
const calendar = ical();
const event = calendar.createEvent({
summary: 'Test Event',
start: '2024-02-29T17:00:00.000Z',
end: '2024-02-29T17:20:00.000Z'
});

event.start('2024-02-29T19:00:00.000Z');
event.end('2024-02-29T19:20:00.000Z');

const start = event.start();
assert.ok(typeof start === 'string');
assert.strictEqual(start, '2024-02-29T19:00:00.000Z');

const end = event.end();
assert.ok(typeof end === 'string');
assert.strictEqual(end, '2024-02-29T19:20:00.000Z');
});
});
});

0 comments on commit 9174a32

Please sign in to comment.