Skip to content

Instantly share code, notes, and snippets.

@TiE23
Last active August 23, 2022 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TiE23/8c59eb7185ecc1ece9f002d684c95653 to your computer and use it in GitHub Desktop.
Save TiE23/8c59eb7185ecc1ece9f002d684c95653 to your computer and use it in GitHub Desktop.
Rearrange VALARM segment to be after RRULE and DURATION
import { rearrangeCalendarStringsForOutlook } from "../iCalendarUtils";
describe("rearrangeCalendarStringsForOutlook()", () => {
test("should handle a single resort", () => {
const singleBefore = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
END:VEVENT
END:VCALENDAR
`;
const singleAfter = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
`;
expect(rearrangeCalendarStringsForOutlook(singleBefore)).toEqual(
singleAfter
);
});
test("it should handle two resorts", () => {
const multiBefore = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
END:VEVENT
START:VEVENT
SUMMARY: Blah blah
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
END:VEVENT
END:VCALENDAR
`;
const multiAfter = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
START:VEVENT
SUMMARY: Blah blah
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
`;
expect(rearrangeCalendarStringsForOutlook(multiBefore)).toEqual(
multiAfter
);
});
test("it should work if there is no RRULE line", () => {
const singleBeforeNoRRULE = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
DURATION:PT45M
END:VEVENT
END:VCALENDAR
`;
const singleAfterNoRRULE = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
DURATION:PT45M
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
`;
expect(rearrangeCalendarStringsForOutlook(singleBeforeNoRRULE)).toEqual(
singleAfterNoRRULE
);
});
test("it should work if there is no DURATION line", () => {
const singleBeforeNoDURATION = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
END:VEVENT
END:VCALENDAR
`;
const singleAfterNoDURATION = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
`;
expect(
rearrangeCalendarStringsForOutlook(singleBeforeNoDURATION)
).toEqual(singleAfterNoDURATION);
});
test("it should work if there is no DURATION line", () => {
const singleBeforeNoRRULENoDURATION = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
`;
const singleAfterNoRRULENoDURATION = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
SUMMARY: Blah blah
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER:-PT10M
END:VALARM
END:VEVENT
END:VCALENDAR
`;
expect(
rearrangeCalendarStringsForOutlook(singleBeforeNoRRULENoDURATION)
).toEqual(singleAfterNoRRULENoDURATION);
});
test("it should work if there is no ALARMS", () => {
const singleBeforeNoVALARM = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
END:VEVENT
END:VCALENDAR
`;
const singleAfterNoVALARM = dedent`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:TiE23/ical
START:VEVENT
RRULE:FREQ=WEEKLY;BYDAY=MO;UNTIL=20301224T235900Z
DURATION:PT45M
END:VEVENT
END:VCALENDAR
`;
expect(rearrangeCalendarStringsForOutlook(singleBeforeNoVALARM)).toEqual(
singleAfterNoVALARM
);
});
test("it should work if there is nothing in the string", () => {
expect(rearrangeCalendarStringsForOutlook("")).toEqual("");
});
});
export function rearrangeCalendarStringsForOutlook(iCal: string): string {
return iCal.replace(
/(^BEGIN:VALARM(.|\r?\n)*?END:VALARM$\r?\n)(.|\r?\n)*?(^RRULE:.*$\r?\n)?(.|\r?\n)*?(^DURATION:.*$\r?\n)?(.|\r?\n)*?/gm,
"$4$6$1"
);
}
@TiE23
Copy link
Author

TiE23 commented Aug 23, 2022

Oh, must mention that yes, there is one module I'm using in the unit test: dedent. deline (by AirBnB) does the same exact thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment