Skip to content

Commit

Permalink
feat(Calendar): Add source(), thanks @irfaan
Browse files Browse the repository at this point in the history
Closes #260
  • Loading branch information
sebbo2002 committed May 24, 2021
2 parents c37eb1b + ff725cc commit 91a764f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ICalCalendarData {
name?: string | null;
description?: string | null;
timezone?: ICalTimezone | string | null;
source?: string | null;
url?: string | null;
scale?: string | null;
ttl?: number | Duration | null;
Expand All @@ -35,6 +36,7 @@ interface ICalCalendarInternalData {
name: string | null;
description: string | null;
timezone: ICalTimezone | null;
source: string | null;
url: string | null;
scale: string | null;
ttl: number | null;
Expand All @@ -48,6 +50,7 @@ export interface ICalCalendarJSONData {
name: string | null;
description: string | null;
timezone: string | null;
source: string | null;
url: string | null;
scale: string | null;
ttl: number | null;
Expand Down Expand Up @@ -120,6 +123,7 @@ export default class ICalCalendar {
name: null,
description: null,
timezone: null,
source: null,
url: null,
scale: null,
ttl: null,
Expand All @@ -132,6 +136,7 @@ export default class ICalCalendar {
data.name !== undefined && this.name(data.name);
data.description !== undefined && this.description(data.description);
data.timezone !== undefined && this.timezone(data.timezone);
data.source !== undefined && this.source(data.source);
data.url !== undefined && this.url(data.url);
data.scale !== undefined && this.scale(data.scale);
data.ttl !== undefined && this.ttl(data.ttl);
Expand Down Expand Up @@ -344,6 +349,33 @@ export default class ICalCalendar {
}


/**
* Get current value of the `SOURCE` attribute.
* @since 2.2.0-develop.1
*/
source(): string | null;

/**
* Use this method to set your feed's `SOURCE` attribute.
* This tells the client where to refresh your feed.
*
* ```javascript
* cal.source('http://example.com/my/original_source.ical');
* ```
*
* @since 2.2.0-develop.1
*/
source(source: string | null): this;
source(source?: string | null): this | string | null {
if (source === undefined) {
return this.data.source;
}

this.data.source = source || null;
return this;
}


/**
* Get your feed's URL
* @since 0.2.5
Expand Down Expand Up @@ -750,6 +782,11 @@ export default class ICalCalendar {
g += 'URL:' + this.data.url + '\r\n';
}

// SOURCE
if (this.data.source) {
g += 'SOURCE;VALUE=URI:' + this.data.source + '\r\n';
}

// CALSCALE
if (this.data.scale) {
g += 'CALSCALE:' + this.data.scale + '\r\n';
Expand Down
25 changes: 25 additions & 0 deletions test/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('ical-generator Calendar', function () {
description: 'Hi, I am the description.',
timezone: null,
url: 'https://github.com/sebbo2002/ical-generator',
source: 'http://example.com/my/original_source.ical',
scale: null,
ttl: null,
events: [],
Expand Down Expand Up @@ -229,6 +230,24 @@ describe('ical-generator Calendar', function () {
});
});

describe('source()', function () {
it('setter should return this', function () {
const cal = new ICalCalendar();
assert.deepStrictEqual(cal, cal.source('http://example.com/my/original_source.ical'));
});

it('getter should return value', function () {
const cal = new ICalCalendar();
assert.strictEqual(cal.source(), null);

cal.source('http://example.com/my/original_source.ical');
assert.strictEqual(cal.source(), 'http://example.com/my/original_source.ical');

cal.url(null);
assert.strictEqual(cal.url(), null);
});
});

describe('url()', function () {
it('setter should return this', function () {
const cal = new ICalCalendar();
Expand Down Expand Up @@ -643,6 +662,12 @@ describe('ical-generator Calendar', function () {
assert.ok(cal.toString().indexOf('X-WR-TIMEZONE:TEST') > -1);
});

it('should include the source', function () {
const cal = new ICalCalendar();
cal.source('http://foo.bar.example.com/ical.cal');
assert.ok(cal.toString().includes('http://foo.bar.example.com/ical.cal'));
});

it('should include VTimezone objects if generator was supplied', function () {
const cal = new ICalCalendar();
cal.timezone({name: 'Europe/Berlin', generator: getVtimezoneComponent});
Expand Down

0 comments on commit 91a764f

Please sign in to comment.