Skip to content

Commit

Permalink
refactor(Calendar): Remove moment.Duration from ttl() method
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `ttl()` will now return a number, not a `moment.Duration`. You can still use `moment.Duration` to set the `ttl` value.
  • Loading branch information
sebbo2002 committed Mar 23, 2021
1 parent 62c1516 commit c6ccd12
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/alarm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

import moment from 'moment-timezone';
import ICalEvent from './event';
import {
addOrGetCustomAttributes,
Expand Down Expand Up @@ -375,7 +374,7 @@ export default class ICalAlarm {
throw new Error('No value for `repeat` in ICalAlarm given, but required for `interval`!');
}
if (this.data.interval) {
g += 'DURATION:' + moment.duration(this.data.interval, 's').toISOString() + '\r\n';
g += 'DURATION:' + toDurationString(this.data.interval) + '\r\n';
}

// ATTACH
Expand Down
34 changes: 19 additions & 15 deletions src/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
'use strict';

import moment from 'moment-timezone';
import {addOrGetCustomAttributes, checkEnum, foldLines, generateCustomAttributes} from './tools';
import type {Duration} from 'moment-timezone';
import {
addOrGetCustomAttributes,
checkEnum,
foldLines,
generateCustomAttributes,
isMomentDuration,
toDurationString
} from './tools';
import ICalEvent, {ICalEventData} from './event';
import {writeFile, writeFileSync} from 'fs';
import {promises as fsPromises} from 'fs';
Expand All @@ -16,7 +23,7 @@ export interface ICalCalendarData {
timezone?: string | null;
url?: string | null;
scale?: string | null;
ttl?: number | moment.Duration | string | null;
ttl?: number | Duration | null;
events?: (ICalEvent | ICalEventData)[];
x?: [string, string][];
}
Expand All @@ -29,7 +36,7 @@ interface ICalCalendarInternalData {
timezone: string | null;
url: string | null;
scale: string | null;
ttl: moment.Duration | null;
ttl: number | null;
events: ICalEvent[];
x: [string, string][];
}
Expand Down Expand Up @@ -254,21 +261,18 @@ export default class ICalCalendar {
* @example cal.ttl(60 * 60 * 24); // 1 day
* @since 0.2.5
*/
ttl(): moment.Duration | null;
ttl(ttl: number | moment.Duration | string | null): this;
ttl(ttl?: number | moment.Duration | string | null): this | moment.Duration | null {
ttl(): number | null;
ttl(ttl: number | Duration | null): this;
ttl(ttl?: number | Duration | null): this | number | null {
if (ttl === undefined) {
return this.data.ttl;
}

if (moment.isDuration(ttl)) {
this.data.ttl = ttl;
}
else if (typeof ttl === 'string') {
this.data.ttl = moment.duration(ttl);
if (isMomentDuration(ttl)) {
this.data.ttl = ttl.asSeconds();
}
else if (ttl && ttl > 0) {
this.data.ttl = moment.duration(ttl, 'seconds');
this.data.ttl = ttl;
}
else {
this.data.ttl = null;
Expand Down Expand Up @@ -489,8 +493,8 @@ export default class ICalCalendar {

// TTL
if (this.data.ttl) {
g += 'REFRESH-INTERVAL;VALUE=DURATION:' + this.data.ttl.toISOString() + '\r\n';
g += 'X-PUBLISHED-TTL:' + this.data.ttl.toISOString() + '\r\n';
g += 'REFRESH-INTERVAL;VALUE=DURATION:' + toDurationString(this.data.ttl) + '\r\n';
g += 'X-PUBLISHED-TTL:' + toDurationString(this.data.ttl) + '\r\n';
}

// Events
Expand Down
4 changes: 3 additions & 1 deletion src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
escape,
formatDate,
formatDateTZ,
generateCustomAttributes, toDate, toJSON
generateCustomAttributes,
toDate,
toJSON
} from './tools';
import ICalAttendee, {ICalAttendeeData} from './attendee';
import ICalAlarm, {ICalAlarmData} from './alarm';
Expand Down
14 changes: 10 additions & 4 deletions src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

import {Moment} from 'moment';
import {Moment as MomentTZ} from 'moment-timezone';
import {Dayjs} from 'dayjs';
import {DateTime as LuxonDateTime} from 'luxon';
import type {Moment, Duration} from 'moment';
import type {Moment as MomentTZ} from 'moment-timezone';
import type {Dayjs} from 'dayjs';
import type {DateTime as LuxonDateTime} from 'luxon';
import {ICalDateTimeValue, ICalOrganizer} from './types';

export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateonly?: boolean, floating?: boolean): string {
Expand Down Expand Up @@ -320,6 +320,12 @@ export function isLuxonDate(value: ICalDateTimeValue): value is LuxonDateTime {
return typeof value === 'object' && value !== null && typeof (value as LuxonDateTime).toJSDate === 'function';
}

export function isMomentDuration(value: unknown): value is Duration {

// @ts-ignore
return value !== null && typeof value === 'object' && typeof value.asSeconds === 'function';
}

export function toJSON(value: ICalDateTimeValue | null | undefined): string | null | undefined {
if(!value) {
return value;
Expand Down
13 changes: 9 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Moment} from 'moment';
import {Moment as MomentTZ} from 'moment-timezone';
import {Dayjs} from 'dayjs';
import {DateTime as LuxonDateTime} from 'luxon';
import type {Moment} from 'moment';
import type {Moment as MomentTZ} from 'moment-timezone';
import type {Dayjs} from 'dayjs';
import type {DateTime as LuxonDateTime} from 'luxon';


export type ICalDateTimeValue = Date | Moment | MomentTZ | Dayjs | LuxonDateTime | string;
Expand Down Expand Up @@ -37,6 +37,11 @@ export interface ICalOrganizer {
mailto?: string;
}

export interface ICalDescription {
plain: string;
html?: string;
}

export enum ICalEventRepeatingFreq {
SECONDLY = 'SECONDLY',
MINUTELY = 'MINUTELY',
Expand Down
4 changes: 2 additions & 2 deletions test/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ describe('ical-generator Calendar', function () {
const cal = new ICalCalendar();
assert.strictEqual(cal.ttl(), null);
cal.ttl(86400);
assert.strictEqual(cal.ttl()?.as('seconds'), 86400);
assert.strictEqual(cal.ttl(), 86400);
});

it('should change something', function () {
const cal = new ICalCalendar().ttl(86400);
assert.strictEqual(cal.ttl()?.as('seconds'), 86400);
assert.strictEqual(cal.ttl(), 86400);
});
});

Expand Down

0 comments on commit c6ccd12

Please sign in to comment.