Skip to content

Commit

Permalink
refactor: make ts-ignore lines obsolet
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Mar 13, 2021
1 parent bf73ef0 commit 21d4ae1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 66 deletions.
8 changes: 0 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@
]
},
"overrides": [
{
"files": [
"src/**/*.ts"
],
"rules": {
"@typescript-eslint/ban-ts-comment": "warn"
}
},
{
"files": [
"test/**/*.ts"
Expand Down
16 changes: 12 additions & 4 deletions src/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import moment from 'moment-timezone';
import ICalEvent from './event';
import {addOrGetCustomAttributes, formatDate, escape, generateCustomAttributes, applyInternalData} from './tools';
import {addOrGetCustomAttributes, formatDate, escape, generateCustomAttributes} from './tools';


export enum ICalAlarmType {
Expand Down Expand Up @@ -62,7 +62,15 @@ export default class ICalAlarm {
throw new Error('`event` option required!');
}

applyInternalData(this, data);
data?.type && this.type(data.type);
data?.trigger && this.trigger(data.trigger);
data?.triggerBefore && this.triggerBefore(data.triggerBefore);
data?.triggerAfter && this.triggerAfter(data.triggerAfter);
data?.repeat && this.repeat(data.repeat);
data?.interval && this.interval(data.interval);
data?.attach && this.attach(data.attach);
data?.description && this.description(data.description);
data?.x && this.x(data.x);
}


Expand Down Expand Up @@ -297,10 +305,10 @@ export default class ICalAlarm {
*
* @since 1.9.0
*/
x (keyOrArray: ({key: string, value: string})[] | Record<string, string>): this;
x (keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): this;
x (keyOrArray: string, value: string): this;
x (): {key: string, value: string}[];
x (keyOrArray?: ({key: string, value: string})[] | Record<string, string> | string, value?: string): this | void | ({key: string, value: string})[] {
x (keyOrArray?: ({key: string, value: string})[] | [string, string][] | Record<string, string> | string, value?: string): this | void | ({key: string, value: string})[] {
if(keyOrArray === undefined) {
return addOrGetCustomAttributes (this.data);
}
Expand Down
18 changes: 11 additions & 7 deletions src/attendee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ export default class ICalAttendee {
throw new Error('`event` option required!');
}

for (const i in data) {
if ([...Object.keys(this.data), 'delegatesTo', 'delegatesFrom'].includes(i)) {

// @ts-ignore
this[i](data[i]);
}
}
data?.name && this.name(data.name);
data?.email && this.email(data.email);
data?.mailto && this.mailto(data.mailto);
data?.status && this.status(data.status);
data?.role && this.role(data.role);
data?.rsvp && this.rsvp(data.rsvp);
data?.type && this.type(data.type);
data?.delegatedTo && this.delegatedTo(data.delegatedTo);
data?.delegatedFrom && this.delegatedFrom(data.delegatedFrom);
data?.delegatesTo && this.delegatesTo(data.delegatesTo);
data?.delegatesFrom && this.delegatesFrom(data.delegatesFrom);
}


Expand Down
25 changes: 14 additions & 11 deletions src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ export default class ICalCalendar {
x: []
};

for (const i in data) {
if (Object.keys(this.data).includes(i)) {

// @ts-ignore
this[i](data[i]);
}
}
data?.prodId && this.prodId(data.prodId);
data?.method && this.method(data.method);
data?.name && this.name(data.name);
data?.description && this.description(data.description);
data?.timezone && this.timezone(data.timezone);
data?.url && this.url(data.url);
data?.scale && this.scale(data.scale);
data?.ttl && this.ttl(data.ttl);
data?.events && this.events(data.events);
data?.x && this.x(data.x);
}


Expand Down Expand Up @@ -294,8 +297,8 @@ export default class ICalCalendar {
* @returns {ICalEvent[]|ICalCalendar}
*/
events(): ICalEvent[];
events(events: ICalEvent[] | ICalEventData[]): this;
events(events?: ICalEvent[] | ICalEventData[]): this | ICalEvent[] {
events(events: (ICalEvent | ICalEventData)[]): this;
events(events?: (ICalEvent | ICalEventData)[]): this | ICalEvent[] {
if (!events) {
return this.data.events;
}
Expand Down Expand Up @@ -383,10 +386,10 @@ export default class ICalCalendar {
*
* @since 1.9.0
*/
x (keyOrArray: ({key: string, value: string})[] | Record<string, string>): this;
x (keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): this;
x (keyOrArray: string, value: string): this;
x (): {key: string, value: string}[];
x (keyOrArray?: ({key: string, value: string})[] | Record<string, string> | string, value?: string): this | void | ({key: string, value: string})[] {
x (keyOrArray?: ({key: string, value: string})[] | [string, string][] | Record<string, string> | string, value?: string): this | void | ({key: string, value: string})[] {
if(keyOrArray === undefined) {
return addOrGetCustomAttributes (this.data);
}
Expand Down
8 changes: 1 addition & 7 deletions src/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ export default class ICalCategory {
name: null
};

for (const i in data) {
if (Object.keys(this.data).includes(i)) {

// @ts-ignore
this[i](data[i]);
}
}
data?.name && this.name(data.name);
}


Expand Down
40 changes: 30 additions & 10 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface ICalEventData {
transparency?: ICalEventTransparency | null,
created?: ICalDateTimeValue | null,
lastModified?: ICalDateTimeValue | null,
x?: ([string, string] | { key: string, value: string })[];
x?: ({key: string, value: string})[] | [string, string][] | Record<string, string>;
}

export interface ICalEventInternalData {
Expand Down Expand Up @@ -163,13 +163,33 @@ export default class ICalEvent {
throw new Error('`calendar` option required!');
}

for (const i in data) {
if (Object.keys(this.data).includes(i)) {

// @ts-ignore
this[i](data[i]);
}
}
data?.id && this.id(data.id);
data?.sequence && this.sequence(data.sequence);
data?.start && this.start(data.start);
data?.end && this.end(data.end);
data?.recurrenceId && this.recurrenceId(data.recurrenceId);
data?.timezone && this.timezone(data.timezone);
data?.stamp && this.stamp(data.stamp);
data?.allDay && this.allDay(data.allDay);
data?.floating && this.floating(data.floating);
data?.repeating && this.repeating(data.repeating);
data?.summary && this.summary(data.summary);
data?.location && this.location(data.location);
data?.appleLocation && this.appleLocation(data.appleLocation);
data?.geo && this.geo(data.geo);
data?.description && this.description(data.description);
data?.htmlDescription && this.htmlDescription(data.htmlDescription);
data?.organizer && this.organizer(data.organizer);
data?.attendees && this.attendees(data.attendees);
data?.alarms && this.alarms(data.alarms);
data?.categories && this.categories(data.categories);
data?.status && this.status(data.status);
data?.busystatus && this.busystatus(data.busystatus);
data?.url && this.url(data.url);
data?.transparency && this.transparency(data.transparency);
data?.created && this.created(data.created);
data?.lastModified && this.lastModified(data.lastModified);
data?.x && this.x(data.x);
}

/**
Expand Down Expand Up @@ -828,10 +848,10 @@ export default class ICalEvent {
* @since 1.9.0
* @returns {ICalEvent|Array<Object<{key: String, value: String}>>}
*/
x(keyOrArray: ({ key: string, value: string })[] | Record<string, string>): this;
x (keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): this;
x(keyOrArray: string, value: string): this;
x(): { key: string, value: string }[];
x(keyOrArray?: ({ key: string, value: string })[] | Record<string, string> | string, value?: string): this | void | ({ key: string, value: string })[] {
x(keyOrArray?: ({ key: string, value: string })[] | [string, string][] | Record<string, string> | string, value?: string): this | void | ({ key: string, value: string })[] {
if (keyOrArray === undefined) {
return addOrGetCustomAttributes(this.data);
}
Expand Down
26 changes: 7 additions & 19 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ export function foldLines (input: string): string {
}).join('\r\n');
}

export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray: ({key: string, value: string})[] | Record<string, string>): void;
export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): void;
export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray: string, value: string): void;
export function addOrGetCustomAttributes (data: {x: [string, string][]}): ({key: string, value: string})[];
export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray?: ({key: string, value: string})[] | Record<string, string> | string | undefined, value?: string | undefined): void | ({key: string, value: string})[] {
export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray?: ({key: string, value: string})[] | [string, string][] | Record<string, string> | string | undefined, value?: string | undefined): void | ({key: string, value: string})[] {
if (Array.isArray(keyOrArray)) {
data.x = keyOrArray.map(o => {
data.x = keyOrArray.map((o: {key: string, value: string} | [string, string]) => {
if(Array.isArray(o)) {
return o;
}
if (typeof o.key !== 'string' || typeof o.value !== 'string') {
throw new Error('Either key or value is not a string!');
}
if (o.key.substr(0, 2) !== 'X-') {
throw new Error('Key has to start with `X-`!');
}

return [o.key, o.value];
return [o.key, o.value] as [string, string];
});
}
else if (typeof keyOrArray === 'object') {
Expand Down Expand Up @@ -209,18 +212,3 @@ export function checkDate(value: ICalDateTimeValue, attribute: string): moment.M

return value;
}

export function applyInternalData<O, D>(object: O, data: D): void {
for (const i in data) {
if (Object.keys(data).includes(i)) {
// @ts-ignore
const m = object[i];

if(typeof m === 'function') {

// @ts-ignore
m.call(object, data[i]);
}
}
}
}

0 comments on commit 21d4ae1

Please sign in to comment.