Skip to content

Commit

Permalink
feat(Event): Update bySetPos and byMonthDay
Browse files Browse the repository at this point in the history
This change introduces two fixes to make the repeating rules for bySetPos and byMonthDay consistent with the iCalendar spec.

- bySetPos is now allowed to be passed in as both a number or an array.
- bySetPos now allows values between -366 to -1 and 1 to 366.
- byMonthDay now allows values between -31 to -1 and 1 to 31.
- If multiple days are specified for byDay, they are all used in conjunction with bySetPos and none are discarded.

Thanks @stevenpal for this contribution.

closes #430
  • Loading branch information
sebbo2002 committed Oct 10, 2022
2 parents 767bebf + cc221aa commit b19e94b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
19 changes: 10 additions & 9 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ interface ICalEventInternalRepeatingData {
byDay?: ICalWeekday[];
byMonth?: number[];
byMonthDay?: number[];
bySetPos?: number;
bySetPos?: number[];
exclude?: ICalDateTimeValue[];
startOfWeek?: ICalWeekday;
}
Expand Down Expand Up @@ -655,7 +655,7 @@ export default class ICalEvent {


this.data.repeating.byMonthDay = byMonthDayArray.map(monthDay => {
if (typeof monthDay !== 'number' || monthDay < 1 || monthDay > 31) {
if (typeof monthDay !== 'number' || monthDay < -31 || monthDay > 31 || monthDay === 0) {
throw new Error('`repeating.byMonthDay` contains invalid value `' + monthDay + '`!');
}

Expand All @@ -667,12 +667,13 @@ export default class ICalEvent {
if (!this.data.repeating.byDay) {
throw '`repeating.bySetPos` must be used along with `repeating.byDay`!';
}
if (typeof repeating.bySetPos !== 'number' || repeating.bySetPos < -1 || repeating.bySetPos > 4) {
throw '`repeating.bySetPos` contains invalid value `' + repeating.bySetPos + '`!';
}

this.data.repeating.byDay.splice(1);
this.data.repeating.bySetPos = repeating.bySetPos;
const bySetPosArray = Array.isArray(repeating.bySetPos) ? repeating.bySetPos : [repeating.bySetPos];
this.data.repeating.bySetPos = bySetPosArray.map(bySetPos => {
if (typeof bySetPos !== 'number' || bySetPos < -366 || bySetPos > 366 || bySetPos === 0) {
throw '`repeating.bySetPos` contains invalid value `' + bySetPos + '`!';
}
return bySetPos;
});
}

if (repeating.exclude) {
Expand Down Expand Up @@ -1506,7 +1507,7 @@ export default class ICalEvent {
}

if (this.data.repeating.bySetPos) {
g += ';BYSETPOS=' + this.data.repeating.bySetPos;
g += ';BYSETPOS=' + this.data.repeating.bySetPos.join(',');
}

if (this.data.repeating.startOfWeek) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface ICalRepeatingOptions {
byDay?: ICalWeekday[] | ICalWeekday;
byMonth?: number[] | number;
byMonthDay?: number[] | number;
bySetPos?: number;
bySetPos?: number[] | number;
exclude?: ICalDateTimeValue[] | ICalDateTimeValue;
startOfWeek?: ICalWeekday;
}
Expand Down
66 changes: 60 additions & 6 deletions test/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,36 @@ describe('ical-generator Event', function () {
repeating: {
freq: ICalEventRepeatingFreq.DAILY,
interval: 2,
byMonthDay: [1, 32, 15]
byMonthDay: [1, 32, -15]
}
}, new ICalCalendar());
}, /`repeating\.byMonthDay` contains invalid value `32`/);

assert.throws(function () {
new ICalEvent({
start: moment(),
end: moment(),
summary: 'test',
repeating: {
freq: ICalEventRepeatingFreq.DAILY,
interval: 2,
byMonthDay: [-1, -32, 15]
}
}, new ICalCalendar());
}, /`repeating\.byMonthDay` contains invalid value `-32`/);

assert.throws(function () {
new ICalEvent({
start: moment(),
end: moment(),
summary: 'test',
repeating: {
freq: ICalEventRepeatingFreq.DAILY,
interval: 2,
byMonthDay: [1, 0, 15]
}
}, new ICalCalendar());
}, /`repeating\.byMonthDay` contains invalid value `0`/);
});

it('setter should update repeating.byMonthDay', function () {
Expand All @@ -861,10 +887,38 @@ describe('ical-generator Event', function () {
freq: ICalEventRepeatingFreq.MONTHLY,
interval: 2,
byDay: [ICalWeekday.SU],
bySetPos: 6
bySetPos: [367]
}
}, new ICalCalendar());
}, /`repeating\.bySetPos` contains invalid value `367`/);

assert.throws(function () {
new ICalEvent({
start: moment(),
end: moment(),
summary: 'test',
repeating: {
freq: ICalEventRepeatingFreq.MONTHLY,
interval: 2,
byDay: [ICalWeekday.SU],
bySetPos: [-367]
}
}, new ICalCalendar());
}, /`repeating\.bySetPos` contains invalid value `-367`/);

assert.throws(function () {
new ICalEvent({
start: moment(),
end: moment(),
summary: 'test',
repeating: {
freq: ICalEventRepeatingFreq.MONTHLY,
interval: 2,
byDay: [ICalWeekday.SU],
bySetPos: [0]
}
}, new ICalCalendar());
}, /`repeating\.bySetPos` contains invalid value `6`/);
}, /`repeating\.bySetPos` contains invalid value `0`/);

assert.throws(function () {
new ICalEvent({
Expand All @@ -876,7 +930,7 @@ describe('ical-generator Event', function () {
interval: 2,
byDay: [ICalWeekday.SU],
// @ts-ignore
bySetPos: 'FOO'
bySetPos: ['FOO']
}
}, new ICalCalendar());
}, /`repeating\.bySetPos` contains invalid value `FOO`/);
Expand All @@ -903,13 +957,13 @@ describe('ical-generator Event', function () {
e.repeating({
freq: ICalEventRepeatingFreq.MONTHLY,
byDay: [ICalWeekday.SU],
bySetPos: 2
bySetPos: [2]
});

const result = e.repeating();
assert.ok(result && !isRRule(result) && typeof result !== 'string');
assert.strictEqual(result.byDay?.length, 1);
assert.strictEqual(result.bySetPos, 2);
assert.strictEqual(result.bySetPos?.length, 1);
});

it('should throw error when repeating.exclude is not valid', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Issues', function () {
assert.ok(str.indexOf('RRULE:FREQ=MONTHLY;COUNT=3;INTERVAL=1;BYDAY=SU;BYSETPOS=3') > -1);
});

it('should work with repeating bySetPos by taking the first elemnt of the byDay array', function () {
it('should work with repeating bySetPos by taking all elements of the byDay array', function () {
const calendar = ical({
prodId: '//superman-industries.com//ical-generator//EN',
events: [{
Expand All @@ -79,7 +79,7 @@ describe('Issues', function () {
});

const str = calendar.toString();
assert.ok(str.indexOf('RRULE:FREQ=MONTHLY;COUNT=3;INTERVAL=1;BYDAY=MO;BYSETPOS=3') > -1);
assert.ok(str.indexOf('RRULE:FREQ=MONTHLY;COUNT=3;INTERVAL=1;BYDAY=MO,FR;BYSETPOS=3') > -1);
});
});

Expand Down

0 comments on commit b19e94b

Please sign in to comment.