Skip to content

Commit

Permalink
✨ Add all day events & improve timezone handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lachenmayer committed May 28, 2019
1 parent 5d15fa5 commit d10afa6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 22 deletions.
50 changes: 47 additions & 3 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ test("generate a google link", () => {
duration: [2, "hour"]
});
expect(link).toBe(
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191228T230000Z%2F20191229T010000Z"
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229T000000Z%2F20191229T020000Z"
);
});

test("generate a google link with time & timezone", () => {
const link = google({
title: "Birthday party",
start: "2019-12-29T12:00:00.000+01:00",
duration: [2, "hour"]
});
expect(link).toBe(
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229T110000Z%2F20191229T130000Z"
);
});

test("generate an all day google link", () => {
const link = google({
title: "Birthday party",
start: "2019-12-29",
allDay: true
});
expect(link).toBe(
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229%2F20191230"
);
});

Expand All @@ -19,7 +41,7 @@ test("generate a google link with guests", () => {
guests: ["hello@example.com", "another@example.com"]
});
expect(link).toBe(
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191228T230000Z%2F20191229T010000Z&add=hello%40example.com%2Canother%40example.com"
"https://calendar.google.com/calendar/render?action=TEMPLATE&text=Birthday%20party&details=&location=&trp=&dates=20191229T000000Z%2F20191229T020000Z&add=hello%40example.com%2Canother%40example.com"
);
});

Expand All @@ -30,7 +52,18 @@ test("generate a yahoo link", () => {
duration: [2, "hour"]
});
expect(link).toBe(
"https://calendar.yahoo.com/?v=60&title=Birthday%20party&st=20191229T000000&et=20191229T020000&desc=&in_loc="
"https://calendar.yahoo.com/?v=60&title=Birthday%20party&st=20191229T000000Z&et=20191229T020000Z&desc=&in_loc="
);
});

test("generate an all day yahoo link", () => {
const link = yahoo({
title: "Birthday party",
start: "2019-12-29",
allDay: true
});
expect(link).toBe(
"https://calendar.yahoo.com/?v=60&title=Birthday%20party&st=20191229&et=20191230&desc=&in_loc="
);
});

Expand All @@ -44,3 +77,14 @@ test("generate a outlook link", () => {
"https://outlook.live.com/owa/?path=%2Fcalendar%2Faction%2Fcompose&rru=addevent&startdt=20191229T000000&enddt=20191229T020000&subject=Birthday%20party&body=&location="
);
});

test("generate an all day outlook link", () => {
const link = outlook({
title: "Birthday party",
start: "2019-12-29",
allDay: true
});
expect(link).toBe(
"https://outlook.live.com/owa/?path=%2Fcalendar%2Faction%2Fcompose&rru=addevent&startdt=20191229&enddt=20191230&subject=Birthday%20party&body=&location="
);
});
59 changes: 40 additions & 19 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import { stringify } from "querystring";
import { CalendarEvent, Google, Outlook, Yahoo } from "./interfaces";

dayjs.extend(utc);

export const eventify = (event: CalendarEvent) => {
event.start = dayjs(event.start).toDate();
if (event.duration && event.duration.length && !event.end) {
Expand All @@ -11,32 +14,36 @@ export const eventify = (event: CalendarEvent) => {
.add(duration, unit)
.toDate();
}
if (event.allDay) {
event.end = dayjs(event.start)
.add(1, "day")
.toDate();
}
return event;
};

const formats = {
dateTime: "YYYYMMDD[T]HHmmss",
dateTimeUTC: "YYYYMMDD[T]HHmmss[Z]",
allDay: "YYYYMMDD"
};

export const google = (event: CalendarEvent) => {
event = eventify(event);
const startDate: string = dayjs(event.start)
.toISOString()
.replace(/-/g, "")
.replace(/:/g, "")
.replace(/\./g, "");
const endDate: string = dayjs(event.end)
.toISOString()
.replace(/-/g, "")
.replace(/:/g, "")
.replace(/\./g, "");
const format = event.allDay ? formats.allDay : formats.dateTimeUTC;
const start: string = dayjs(event.start)
.utc()
.format(format);
const end: string = dayjs(event.end)
.utc()
.format(format);
const details: Google = {
action: "TEMPLATE",
text: event.title,
details: event.description,
location: event.location,
trp: event.busy,
dates:
startDate.substring(0, startDate.length - 4) +
"Z/" +
endDate.substring(0, endDate.length - 4) +
"Z"
dates: start + "/" + end
};
if (event.guests && event.guests.length) {
details.add = event.guests.join();
Expand All @@ -46,11 +53,18 @@ export const google = (event: CalendarEvent) => {

export const outlook = (event: CalendarEvent) => {
event = eventify(event);
const format = event.allDay ? formats.allDay : formats.dateTime;
const start: string = dayjs(event.start)
.utc()
.format(format);
const end: string = dayjs(event.end)
.utc()
.format(format);
const details: Outlook = {
path: "/calendar/action/compose",
rru: "addevent",
startdt: dayjs(event.start).format("YYYYMMDD[T]HHmmss"),
enddt: dayjs(event.end).format("YYYYMMDD[T]HHmmss"),
startdt: start,
enddt: end,
subject: event.title,
body: event.description,
location: event.location
Expand All @@ -60,11 +74,18 @@ export const outlook = (event: CalendarEvent) => {

export const yahoo = (event: CalendarEvent) => {
event = eventify(event);
const format = event.allDay ? formats.allDay : formats.dateTimeUTC;
const start: string = dayjs(event.start)
.utc()
.format(format);
const end: string = dayjs(event.end)
.utc()
.format(format);
const details: Yahoo = {
v: 60,
title: event.title,
st: dayjs(event.start).format("YYYYMMDD[T]HHmmss"),
et: dayjs(event.end).format("YYYYMMDD[T]HHmmss"),
st: start,
et: end,
desc: event.description,
in_loc: event.location
};
Expand Down
1 change: 1 addition & 0 deletions interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface CalendarEvent {
start: any;
end?: any;
duration?: [number, dayjs.UnitType];
allDay?: boolean;
description?: string;
location?: string;
busy?: boolean;
Expand Down

0 comments on commit d10afa6

Please sign in to comment.