Skip to content

Commit

Permalink
feat: Add Transparency Method
Browse files Browse the repository at this point in the history
* Readd Transparency options

* Add types

* Add test

* Fix for testing a number
  • Loading branch information
Sparticuz committed Jun 7, 2020
1 parent f6fc8bb commit bd2901d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ declare module 'ical-generator' {
busystatus?: string;
timezone?: string;
recurrenceId?: moment.Moment | Date;
transparency?: string;
}

interface RepeatingData {
Expand Down Expand Up @@ -241,6 +242,8 @@ declare module 'ical-generator' {
lastModified(): moment.Moment;
lastModified(lastModified: string | moment.Moment | Date): ICalEvent;
toJSON(): EventData;
transparency(): string;
transparency(transparency: string): string;
}

class ICalAttendee {
Expand Down
34 changes: 33 additions & 1 deletion src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ICalEvent {
status: null,
busystatus: null,
url: null,
transparency: null,
created: null,
lastModified: null,
x: []
Expand Down Expand Up @@ -66,6 +67,7 @@ class ICalEvent {
'status',
'busystatus',
'url',
'transparency',
'created',
'lastModified',
'recurrenceId',
Expand All @@ -74,7 +76,8 @@ class ICalEvent {
this._vars = {
allowedRepeatingFreq: ['SECONDLY', 'MINUTELY', 'HOURLY', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY'],
allowedStatuses: ['CONFIRMED', 'TENTATIVE', 'CANCELLED'],
allowedBusyStatuses: ['FREE', 'TENTATIVE', 'BUSY', 'OOF']
allowedBusyStatuses: ['FREE', 'TENTATIVE', 'BUSY', 'OOF'],
allowedTranspValues: ['TRANSPARENT', 'OPAQUE']
};

this._calendar = _calendar;
Expand Down Expand Up @@ -931,6 +934,30 @@ class ICalEvent {
return this;
}

/**
* Set/Get the event's transparency
*
* @param {String} transparency
* @since 1.7.3
* @returns {ICalEvent|String}
*/
transparency (transparency) {
if(transparency === undefined) {
return this._data.transparency;
}
if(!transparency) {
this._data.transparency = null;
return this;
}

if(this._vars.allowedTranspValues.indexOf(transparency.toString().toUpperCase()) === -1) {
throw new Error('`transparency` must be one of the following: ' + this._vars.allowedTranspValues.join(', ') + '!');
}

this._data.transparency = transparency.toUpperCase();
return this;
}


/**
* Set/Get the event's creation date
Expand Down Expand Up @@ -1111,6 +1138,11 @@ class ICalEvent {
// SUMMARY
g += 'SUMMARY:' + ICalTools.escape(this._data.summary) + '\r\n';

// TRANSPARENCY
if(this._data.transparency) {
g += 'TRANSP:' + ICalTools.escape(this._data.transparency) + '\r\n';
}

// LOCATION
if (this._data.location) {
g += 'LOCATION:' + ICalTools.escape(this._data.location) + '\r\n';
Expand Down
48 changes: 47 additions & 1 deletion test/test_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,52 @@ describe('ical-generator Event', function () {
});
});

describe('transparency()', function () {
it('getter should return value', function () {
const event = new ICalEvent(null, new ICalCalendar());
assert.strictEqual(event.transparency(), null);

event._data.transparency = 'OPAQUE';
assert.strictEqual(event.transparency(), 'OPAQUE');

event._data.transparency = null;
assert.strictEqual(event.transparency(), null);
});

it('setter should return this', function () {
const e = new ICalEvent(null, new ICalCalendar());
assert.deepStrictEqual(e, e.transparency(null));
assert.deepStrictEqual(e, e.transparency('TRANSPARENT'));
});

it('setter should allow setting null', function () {
const e = new ICalEvent(null, new ICalCalendar());
e._data.transparency = 'OPAQUE';
e.transparency(null);
assert.strictEqual(e._data.transparency, null);
});

it('setter should allow setting valid value', function () {
const e = new ICalEvent(null, new ICalCalendar());
e.transparency('OPAQUE');
assert.strictEqual(e._data.transparency, 'OPAQUE');
});

it('should throw error when method not allowed', function () {
const e = new ICalEvent(null, new ICalCalendar());
assert.throws(function () {
e.transparency('COOKING');
}, /`transparency`/);
assert.throws(function () {
e.transparency(Infinity);
}, /`transparency`/);
assert.throws(function () {
e.transparency(-1);
}, /`transparency`/);
});
});


describe('_generate()', function () {
it('shoult throw an error without start', function () {
const e = new ICalEvent({
Expand All @@ -1666,7 +1712,7 @@ describe('ical-generator Event', function () {
}, /`start`/);
});

it('shoult make use of escaping', function () {
it('should make use of escaping', function () {
const e = new ICalEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
Expand Down

0 comments on commit bd2901d

Please sign in to comment.