Skip to content

Commit

Permalink
feat: Support moment.js, Day.js and Luxon (#91, BREAKING CHANGE)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Mar 16, 2021
1 parent bb70156 commit 6db24ee
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 259 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"overrides": [
{
"files": [
"test/**/*.ts"
"test/**/*.ts",
"src/tools.ts"
],
"rules": {
"@typescript-eslint/ban-ts-ignore": "off",
Expand Down
7 changes: 0 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@
"typedoc": "^0.20.28",
"typescript": "^4.2.2"
},
"peerDependencies": {
"@types/node": ">= 10.0.0",
"dayjs": "^1.9.6",
"luxon": "^1.25.0",
"moment": "^2.29.1",
"moment-timezone": "^0.5.32"
},
"scripts": {
"all": "npm run lint && npm run build",
"build": "./build.sh",
Expand Down
78 changes: 37 additions & 41 deletions src/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

import moment from 'moment-timezone';
import ICalEvent from './event';
import {addOrGetCustomAttributes, formatDate, escape, generateCustomAttributes} from './tools';
import {
addOrGetCustomAttributes,
formatDate,
escape,
generateCustomAttributes,
checkDate,
toDurationString
} from './tools';
import {ICalDateTimeValue} from './types';


export enum ICalAlarmType {
Expand All @@ -19,9 +27,9 @@ interface ICalAttachment {

export interface ICalAlarmData {
type?: ICalAlarmType | null;
trigger?: number | moment.Moment | Date | null;
triggerBefore?: number | moment.Moment | Date | null;
triggerAfter?: number | moment.Moment | Date | null;
trigger?: number | ICalDateTimeValue | null;
triggerBefore?: number | ICalDateTimeValue | null;
triggerAfter?: number | ICalDateTimeValue | null;
repeat?: number | null;
interval?: number | null;
attach?: string | ICalAttachment | null;
Expand All @@ -31,7 +39,7 @@ export interface ICalAlarmData {

interface ICalInternalAlarmData {
type: ICalAlarmType | null;
trigger: moment.Moment | number | null;
trigger: ICalDateTimeValue | number | null;
repeat: number | null;
interval: number | null;
attach: ICalAttachment | null;
Expand Down Expand Up @@ -108,61 +116,49 @@ export default class ICalAlarm {
*
* @since 0.2.1
*/
trigger (trigger: number | moment.Moment | moment.Duration | Date | null): this;
trigger (): number | moment.Moment | null;
trigger (trigger?: number | moment.Moment | moment.Duration | Date | null): this | number | moment.Moment | null {
trigger (trigger: number | ICalDateTimeValue | Date | null): this;
trigger (): number | ICalDateTimeValue | null;
trigger (trigger?: number | ICalDateTimeValue | Date | null): this | number | ICalDateTimeValue | null {

// Getter
if (trigger === undefined && moment.isMoment(this.data.trigger)) {
return this.data.trigger;
}
if (trigger === undefined && typeof this.data.trigger === 'number') {
return -1 * this.data.trigger;
}
if (trigger === undefined && this.data.trigger) {
return this.data.trigger;
}
if (trigger === undefined) {
return null;
}

// Setter
if (!trigger) {
this.data.trigger = null;
return this;
}
if (trigger instanceof Date) {
this.data.trigger = moment(trigger);
return this;
}
if (moment.isMoment(trigger)) {
this.data.trigger = trigger;
return this;
else if (typeof trigger === 'number' && isFinite(trigger)) {
this.data.trigger = -1 * trigger;
}
if (moment.isDuration(trigger)) {
this.data.trigger = -1 * trigger.as('seconds');
return this;
else if(typeof trigger === 'number') {
throw new Error('`trigger` is not correct, must be a finite number or a supported date!');
}
if (typeof trigger === 'number' && isFinite(trigger)) {
this.data.trigger = -1 * trigger;
return this;
else {
this.data.trigger = checkDate(trigger, 'trigger');
}

throw new Error('`trigger` is not correct, must be a `Number`, `Date` or `moment` or a `moment.duration`!');
return this;
}


/**
* Set/Get seconds after event to trigger alarm
* @since 0.2.1
*/
triggerAfter (trigger: number | moment.Moment | moment.Duration | Date | null): this;
triggerAfter (): number | moment.Moment | null;
triggerAfter (trigger?: number | moment.Moment | moment.Duration | Date | null): this | number | moment.Moment | null {
triggerAfter (trigger: number | ICalDateTimeValue | null): this;
triggerAfter (): number | ICalDateTimeValue | null;
triggerAfter (trigger?: number | ICalDateTimeValue | null): this | number | ICalDateTimeValue | null {
if (trigger === undefined) {
return this.data.trigger;
}
if (moment.isDuration(trigger)) {
this.data.trigger = -1 * trigger.as('seconds');
return this;
}

return this.trigger(typeof trigger === 'number' ? -1 * trigger : trigger);
}
Expand All @@ -172,9 +168,9 @@ export default class ICalAlarm {
* Set/Get seconds before event to trigger alarm
* @since 0.2.1
*/
triggerBefore (trigger: number | moment.Moment | moment.Duration | Date | null): this;
triggerBefore (): number | moment.Moment | null;
triggerBefore (trigger?: number | moment.Moment | moment.Duration | Date | null): this | number | moment.Moment | null {
triggerBefore (trigger: number | ICalDateTimeValue | null): this;
triggerBefore (): number | ICalDateTimeValue | null;
triggerBefore (trigger?: number | ICalDateTimeValue | null): this | number | ICalDateTimeValue | null {
if(trigger === undefined) {
return this.trigger();
}
Expand Down Expand Up @@ -356,14 +352,14 @@ export default class ICalAlarm {
// ACTION
g += 'ACTION:' + this.data.type.toUpperCase() + '\r\n';

if (moment.isMoment(this.data.trigger)) {
g += 'TRIGGER;VALUE=DATE-TIME:' + formatDate(this.event.timezone(), this.data.trigger) + '\r\n';
if (typeof this.data.trigger === 'number' && this.data.trigger > 0) {
g += 'TRIGGER;RELATED=END:' + toDurationString(this.data.trigger) + '\r\n';
}
else if (this.data.trigger > 0) {
g += 'TRIGGER;RELATED=END:' + moment.duration(this.data.trigger, 's').toISOString() + '\r\n';
else if (typeof this.data.trigger === 'number') {
g += 'TRIGGER:' + toDurationString(this.data.trigger) + '\r\n';
}
else {
g += 'TRIGGER:' + moment.duration(this.data.trigger, 's').toISOString() + '\r\n';
g += 'TRIGGER;VALUE=DATE-TIME:' + formatDate(this.event.timezone(), this.data.trigger) + '\r\n';
}

// REPEAT
Expand Down
66 changes: 33 additions & 33 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
escape,
formatDate,
formatDateTZ,
generateCustomAttributes
generateCustomAttributes, toDate, toJSON
} from './tools';
import ICalAttendee, {ICalAttendeeData} from './attendee';
import ICalAlarm, {ICalAlarmData} from './alarm';
Expand Down Expand Up @@ -78,11 +78,11 @@ export interface ICalEventData {
export interface ICalEventInternalData {
id: string,
sequence: number,
start: moment.Moment | null,
end: moment.Moment | null,
recurrenceId: moment.Moment | null,
start: ICalDateTimeValue | null,
end: ICalDateTimeValue | null,
recurrenceId: ICalDateTimeValue | null,
timezone: string | null,
stamp: moment.Moment,
stamp: ICalDateTimeValue,
allDay: boolean,
floating: boolean,
repeating: ICalEventInternalRepeatingData | null,
Expand All @@ -100,21 +100,21 @@ export interface ICalEventInternalData {
busystatus: ICalEventBusyStatus | null,
url: string | null,
transparency: ICalEventTransparency | null,
created: moment.Moment | null,
lastModified: moment.Moment | null,
created: ICalDateTimeValue | null,
lastModified: ICalDateTimeValue | null,
x: [string, string][];
}

export interface ICalEventInternalRepeatingData {
freq: ICalEventRepeatingFreq;
count?: number;
interval?: number;
until?: moment.Moment;
until?: ICalDateTimeValue;
byDay?: ICalWeekday[];
byMonth?: number[];
byMonthDay?: number[];
bySetPos?: number;
exclude?: moment.Moment[];
exclude?: ICalDateTimeValue[];
startOfWeek?: ICalWeekday;
}

Expand Down Expand Up @@ -247,15 +247,15 @@ export default class ICalEvent {
*
* @since 0.2.0
*/
start(): moment.Moment | null;
start(): ICalDateTimeValue | null;
start(start: ICalDateTimeValue): this;
start(start?: ICalDateTimeValue): this | moment.Moment | null {
start(start?: ICalDateTimeValue): this | ICalDateTimeValue | null {
if (start === undefined) {
return this.data.start;
}

this.data.start = checkDate(start, 'start');
if (this.data.start && this.data.end && this.data.start.isAfter(this.data.end)) {
if (this.data.start && this.data.end && toDate(this.data.start).getTime() > toDate(this.data.end).getTime()) {
const t = this.data.start;
this.data.start = this.data.end;
this.data.end = t;
Expand All @@ -269,9 +269,9 @@ export default class ICalEvent {
*
* @since 0.2.0
*/
end(): moment.Moment | null;
end(): ICalDateTimeValue | null;
end(end: ICalDateTimeValue | null): this;
end(end?: ICalDateTimeValue | null): this | moment.Moment | null {
end(end?: ICalDateTimeValue | null): this | ICalDateTimeValue | null {
if (end === undefined) {
return this.data.end;
}
Expand All @@ -281,7 +281,7 @@ export default class ICalEvent {
}

this.data.end = checkDate(end, 'end');
if (this.data.start && this.data.end && this.data.start.isAfter(this.data.end)) {
if (this.data.start && this.data.end && toDate(this.data.start).getTime() > toDate(this.data.end).getTime()) {
const t = this.data.start;
this.data.start = this.data.end;
this.data.end = t;
Expand All @@ -294,9 +294,9 @@ export default class ICalEvent {
* Set/Get the event's recurrence id
* @since 0.2.0
*/
recurrenceId(): moment.Moment | null;
recurrenceId(): ICalDateTimeValue | null;
recurrenceId(recurrenceId: ICalDateTimeValue | null): this;
recurrenceId(recurrenceId?: ICalDateTimeValue | null): this | moment.Moment | null {
recurrenceId(recurrenceId?: ICalDateTimeValue | null): this | ICalDateTimeValue | null {
if (recurrenceId === undefined) {
return this.data.recurrenceId;
}
Expand Down Expand Up @@ -338,9 +338,9 @@ export default class ICalEvent {
* Set/Get the event's timestamp
* @since 0.2.0
*/
stamp(): moment.Moment;
stamp(): ICalDateTimeValue;
stamp(stamp: ICalDateTimeValue): this;
stamp(stamp?: ICalDateTimeValue): this | moment.Moment {
stamp(stamp?: ICalDateTimeValue): this | ICalDateTimeValue {
if (stamp === undefined) {
return this.data.stamp;
}
Expand All @@ -355,9 +355,9 @@ export default class ICalEvent {
* @since 0.2.0
* @alias stamp
*/
timestamp(): moment.Moment;
timestamp(): ICalDateTimeValue;
timestamp(stamp: ICalDateTimeValue): this;
timestamp(stamp?: ICalDateTimeValue): this | moment.Moment {
timestamp(stamp?: ICalDateTimeValue): this | ICalDateTimeValue {
if (stamp === undefined) {
return this.stamp();
}
Expand Down Expand Up @@ -802,9 +802,9 @@ export default class ICalEvent {
* Set/Get the event's creation date
* @since 0.3.0
*/
created(): moment.Moment | null;
created(): ICalDateTimeValue | null;
created(created: ICalDateTimeValue | null): this;
created(created?: ICalDateTimeValue | null): this | moment.Moment | null {
created(created?: ICalDateTimeValue | null): this | ICalDateTimeValue | null {
if (created === undefined) {
return this.data.created;
}
Expand All @@ -822,9 +822,9 @@ export default class ICalEvent {
* Set/Get the event's last modification date
* @since 0.3.0
*/
lastModified(): moment.Moment | null;
lastModified(): ICalDateTimeValue | null;
lastModified(lastModified: ICalDateTimeValue | null): this;
lastModified(lastModified?: ICalDateTimeValue | null): this | moment.Moment | null {
lastModified(lastModified?: ICalDateTimeValue | null): this | ICalDateTimeValue | null {
if (lastModified === undefined) {
return this.data.lastModified;
}
Expand Down Expand Up @@ -873,15 +873,15 @@ export default class ICalEvent {
*/
toJSON(): ICalEventInternalData {
return Object.assign({}, this.data, {
start: this.data.start?.toJSON() || null,
end: this.data.end?.toJSON() || null,
recurrenceId: this.data.recurrenceId?.toJSON() || null,
stamp: this.data.stamp?.toJSON() || null,
created: this.data.created?.toJSON() || null,
lastModified: this.data.lastModified?.toJSON() || null,
start: toJSON(this.data.start) || null,
end: toJSON(this.data.end) || null,
recurrenceId: toJSON(this.data.recurrenceId) || null,
stamp: toJSON(this.data.stamp) || null,
created: toJSON(this.data.created) || null,
lastModified: toJSON(this.data.lastModified) || null,
repeating: this.data.repeating ? Object.assign({}, this.data.repeating, {
until: this.data.repeating.until?.toJSON(),
exclude: this.data.repeating.exclude?.map(d => d.toJSON()),
until: toJSON(this.data.repeating.until),
exclude: this.data.repeating.exclude?.map(d => toJSON(d)),
}) : null,
x: this.x()
});
Expand Down
Loading

0 comments on commit 6db24ee

Please sign in to comment.