Skip to content

Commit

Permalink
Add Recurrence Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
PeronGH committed Sep 7, 2022
1 parent 289ccd5 commit 7e223b8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@
"DTSTAMP",
"DTSTART",
"PRODID",
"rrule",
"VCALENDAR",
"VEVENT"
]
Expand Down
4 changes: 3 additions & 1 deletion date.ts
Original file line number Diff line number Diff line change
@@ -1,4 1,6 @@
export default function parseDate(date: DateData) {
export function parseDate(date: DateData | undefined) {
if (date === undefined) return;

if (date instanceof Date) {
date = [
date.getFullYear(),
Expand Down
12 changes: 7 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
import { crypto } from 'https://deno.land/[email protected]/crypto/mod.ts';
import parseDate from './date.ts';
import { DateData } from './date.ts';
import { parseDate, DateData } from './date.ts';
import { ContentLine, stringifyLines } from './stringify.ts';
import { parseRRule, RecurrenceRule } from './rrule.ts';

const calendarBegin: ContentLine = ['BEGIN', 'VCALENDAR'];

Expand Down Expand Up @@ -39,7 39,7 @@ export class Event {

toLines(): ContentLine[] {
const uid = crypto.randomUUID();
const { title, description } = this.config;
const { title, desc, rrule } = this.config;

return [
eventBegin,
Expand All @@ -48,7 48,8 @@ export class Event {
['DTSTART', parseDate(this.config.beginDate)],
['DTEND', parseDate(this.config.endDate!)],
['SUMMARY', title],
['DESCRIPTION', description],
['DESCRIPTION', desc],
['RRULE', parseRRule(rrule)],
eventEnd,
].filter(line => line[1] !== undefined) as ContentLine[];
}
Expand All @@ -73,5 74,6 @@ export interface EventConfig {
beginDate: DateData;
endDate?: DateData;
duration?: number;
description?: string;
desc?: string;
rrule?: RecurrenceRule;
}
24 changes: 24 additions & 0 deletions rrule.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
import { parseDate, DateData } from './date.ts';

export interface RecurrenceRule {
freq: 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'YEARLY';
until?: DateData;
interval?: number;
count?: number;
}

export function parseRRule(rrule: RecurrenceRule | undefined) {
if (rrule === undefined) return;

const { freq, until, interval, count } = rrule;

return [
['FREQ', freq],
['UNTIL', parseDate(until)],
['INTERVAL', interval],
['COUNT', count],
]
.filter(line => line[1] !== undefined)
.map(line => `${line[0]}=${line[1]};`)
.join('');
}
10 changes: 7 additions & 3 deletions tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 7,7 @@ Deno.test({
title: 'test',
beginDate: [2022, 8, 1, 10, 10],
endDate: [2022, 8, 1, 11, 10],
description: 'Hello',
desc: 'Hello',
};
const evt = new Event(cfg);

Expand Down Expand Up @@ -37,13 37,17 @@ Deno.test({
title: 'Write Typescript',
beginDate: [2022, 9, 6, 9, 30],
endDate: [2022, 9, 6, 10],
description: 'Implement a module to generate .ics files',
desc: 'Implement a module to generate .ics files',
};

const cfg2: EventConfig = {
title: 'Write Rust',
title: 'Write Rust for next 3 days',
beginDate: new Date(),
duration: 3600, // Duration: 3600s, or 1h
rrule: {
freq: 'DAILY',
count: 3,
},
};

const evt1 = new Event(cfg1);
Expand Down

0 comments on commit 7e223b8

Please sign in to comment.