Skip to content

Commit

Permalink
feat(Event): Add priority() method
Browse files Browse the repository at this point in the history
Closes #163
  • Loading branch information
sebbo2002 committed Mar 23, 2021
1 parent ce842b5 commit 247039f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface ICalEventData {
categories?: ICalCategory[] | ICalCategoryData[],
status?: ICalEventStatus | null,
busystatus?: ICalEventBusyStatus | null,
priority?: number | null,
url?: string | null,
transparency?: ICalEventTransparency | null,
created?: ICalDateTimeValue | null,
Expand Down Expand Up @@ -94,6 +95,7 @@ export interface ICalEventInternalData {
categories: ICalCategory[],
status: ICalEventStatus | null,
busystatus: ICalEventBusyStatus | null,
priority: number | null,
url: string | null,
transparency: ICalEventTransparency | null,
created: ICalDateTimeValue | null,
Expand Down Expand Up @@ -121,6 +123,7 @@ export interface ICalEventJSONData {
categories: ICalCategory[],
status: ICalEventStatus | null,
busystatus: ICalEventBusyStatus | null,
priority?: number | null,
url: string | null,
transparency: ICalEventTransparency | null,
created: string | null,
Expand Down Expand Up @@ -171,6 +174,7 @@ export default class ICalEvent {
categories: [],
status: null,
busystatus: null,
priority: null,
url: null,
transparency: null,
created: null,
Expand Down Expand Up @@ -202,6 +206,7 @@ export default class ICalEvent {
data.categories && this.categories(data.categories);
data.status && this.status(data.status);
data.busystatus && this.busystatus(data.busystatus);
data.priority && this.priority(data.priority);
data.url && this.url(data.url);
data.transparency && this.transparency(data.transparency);
data.created && this.created(data.created);
Expand Down Expand Up @@ -737,6 +742,33 @@ export default class ICalEvent {
}


/**
* Set/Get the event's priority. A value of 1 represents
* the highest priority, 9 the lowest. 0 specifies an undefined
* priority.
*
* @see https://www.kanzaki.com/docs/ical/priority.html
*/
priority(): number | null;
priority(priority: number | null): this;
priority(priority?: number | null): this | number | null {
if (priority === undefined) {
return this.data.priority;
}
if (priority === null) {
this.data.priority = null;
return this;
}

if(priority < 0 || priority > 9) {
throw new Error('`priority` is invalid, musst be 0 ≤ priority ≤ 9.');
}

this.data.priority = Math.round(priority);
return this;
}


/**
* Set/Get the event's URL
* @since 0.2.0
Expand Down Expand Up @@ -1063,6 +1095,11 @@ export default class ICalEvent {
g += 'X-MICROSOFT-CDO-BUSYSTATUS:' + this.data.busystatus.toUpperCase() + '\r\n';
}

// PRIORITY
if (this.data.priority !== null) {
g += 'PRIORITY:' + this.data.priority + '\r\n';
}

// CUSTOM X ATTRIBUTES
g += generateCustomAttributes(this.data);

Expand Down
27 changes: 27 additions & 0 deletions test/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,33 @@ describe('ical-generator Event', function () {
});
});

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

e.priority(5);
assert.strictEqual(e.priority(), 5);
});

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

it('should update value', function () {
const event = new ICalEvent({
start: moment(),
summary: 'Example Event'
}, new ICalCalendar());

event.priority(5);
assert.strictEqual(event.priority(), 5);
assert.ok(event.toString().includes('PRIORITY:5'));
});
});

describe('url()', function () {
it('getter should return value', function () {
const e = new ICalEvent({}, new ICalCalendar());
Expand Down

0 comments on commit 247039f

Please sign in to comment.