ical-generator 8.1.2-develop.9 → 9.0.0-develop.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/attendee.ts CHANGED
@@ -1,79 +1,85 @@
1
1
  'use strict';
2
2
 
3
-
4
- import {addOrGetCustomAttributes, checkEnum, checkNameAndMail, escape} from './tools.ts';
5
- import type ICalEvent from './event.ts';
6
3
  import type ICalAlarm from './alarm.ts';
4
+ import type ICalEvent from './event.ts';
7
5
 
8
-
9
- interface ICalInternalAttendeeData {
10
- name: string | null;
11
- email: string;
12
- mailto: string | null;
13
- sentBy: string | null;
14
- status: ICalAttendeeStatus | null;
15
- role: ICalAttendeeRole;
16
- rsvp: boolean | null;
17
- type: ICalAttendeeType | null;
18
- delegatedTo: ICalAttendee | null;
19
- delegatedFrom: ICalAttendee | null;
20
- x: [string, string][];
21
- }
22
-
23
- export interface ICalAttendeeData {
24
- name?: string | null;
25
- email: string;
26
- mailto?: string | null;
27
- sentBy?: string | null;
28
- status?: ICalAttendeeStatus | null;
29
- role?: ICalAttendeeRole;
30
- rsvp?: boolean | null;
31
- type?: ICalAttendeeType | null;
32
- delegatedTo?: ICalAttendee | ICalAttendeeData | string | null;
33
- delegatedFrom?: ICalAttendee | ICalAttendeeData | string | null;
34
- delegatesTo?: ICalAttendee | ICalAttendeeData | string | null;
35
- delegatesFrom?: ICalAttendee | ICalAttendeeData | string | null;
36
- x?: {key: string, value: string}[] | [string, string][] | Record<string, string>;
37
- }
38
-
39
- export interface ICalAttendeeJSONData {
40
- name: string | null;
41
- email: string;
42
- mailto: string | null;
43
- sentBy: string | null;
44
- status: ICalAttendeeStatus | null;
45
- role: ICalAttendeeRole;
46
- rsvp: boolean | null;
47
- type: ICalAttendeeType | null;
48
- delegatedTo: string | null;
49
- delegatedFrom: string | null;
50
- x: {key: string, value: string}[];
51
- }
6
+ import {
7
+ addOrGetCustomAttributes,
8
+ checkEnum,
9
+ checkNameAndMail,
10
+ escape,
11
+ } from './tools.ts';
52
12
 
53
13
  export enum ICalAttendeeRole {
54
14
  CHAIR = 'CHAIR',
55
- REQ = 'REQ-PARTICIPANT',
15
+ NON = 'NON-PARTICIPANT',
56
16
  OPT = 'OPT-PARTICIPANT',
57
- NON = 'NON-PARTICIPANT'
17
+ REQ = 'REQ-PARTICIPANT',
58
18
  }
59
19
 
60
20
  export enum ICalAttendeeStatus {
61
21
  ACCEPTED = 'ACCEPTED',
62
- TENTATIVE = 'TENTATIVE',
63
22
  DECLINED = 'DECLINED',
64
23
  DELEGATED = 'DELEGATED',
65
- NEEDSACTION = 'NEEDS-ACTION'
24
+ NEEDSACTION = 'NEEDS-ACTION',
25
+ TENTATIVE = 'TENTATIVE',
66
26
  }
67
27
 
68
28
  // ref: https://tools.ietf.org/html/rfc2445#section-4.2.3
69
29
  export enum ICalAttendeeType {
70
- INDIVIDUAL = 'INDIVIDUAL',
71
30
  GROUP = 'GROUP',
31
+ INDIVIDUAL = 'INDIVIDUAL',
72
32
  RESOURCE = 'RESOURCE',
73
33
  ROOM = 'ROOM',
74
- UNKNOWN = 'UNKNOWN'
34
+ UNKNOWN = 'UNKNOWN',
35
+ }
36
+
37
+ export interface ICalAttendeeData {
38
+ delegatedFrom?: ICalAttendee | ICalAttendeeData | null | string;
39
+ delegatedTo?: ICalAttendee | ICalAttendeeData | null | string;
40
+ delegatesFrom?: ICalAttendee | ICalAttendeeData | null | string;
41
+ delegatesTo?: ICalAttendee | ICalAttendeeData | null | string;
42
+ email: string;
43
+ mailto?: null | string;
44
+ name?: null | string;
45
+ role?: ICalAttendeeRole;
46
+ rsvp?: boolean | null;
47
+ sentBy?: null | string;
48
+ status?: ICalAttendeeStatus | null;
49
+ type?: ICalAttendeeType | null;
50
+ x?:
51
+ | [string, string][]
52
+ | Record<string, string>
53
+ | { key: string; value: string }[];
75
54
  }
76
55
 
56
+ export interface ICalAttendeeJSONData {
57
+ delegatedFrom: null | string;
58
+ delegatedTo: null | string;
59
+ email: string;
60
+ mailto: null | string;
61
+ name: null | string;
62
+ role: ICalAttendeeRole;
63
+ rsvp: boolean | null;
64
+ sentBy: null | string;
65
+ status: ICalAttendeeStatus | null;
66
+ type: ICalAttendeeType | null;
67
+ x: { key: string; value: string }[];
68
+ }
69
+
70
+ interface ICalInternalAttendeeData {
71
+ delegatedFrom: ICalAttendee | null;
72
+ delegatedTo: ICalAttendee | null;
73
+ email: string;
74
+ mailto: null | string;
75
+ name: null | string;
76
+ role: ICalAttendeeRole;
77
+ rsvp: boolean | null;
78
+ sentBy: null | string;
79
+ status: ICalAttendeeStatus | null;
80
+ type: ICalAttendeeType | null;
81
+ x: [string, string][];
82
+ }
77
83
 
78
84
  /**
79
85
  * Usually you get an {@link ICalAttendee} object like this:
@@ -95,7 +101,7 @@ export enum ICalAttendeeType {
95
101
  */
96
102
  export default class ICalAttendee {
97
103
  private readonly data: ICalInternalAttendeeData;
98
- private readonly parent: ICalEvent | ICalAlarm;
104
+ private readonly parent: ICalAlarm | ICalEvent;
99
105
 
100
106
  /**
101
107
  * Constructor of {@link ICalAttendee}. The event reference is
@@ -104,19 +110,19 @@ export default class ICalAttendee {
104
110
  * @param data Attendee Data
105
111
  * @param parent Reference to ICalEvent object
106
112
  */
107
- constructor(data: ICalAttendeeData, parent: ICalEvent | ICalAlarm) {
113
+ constructor(data: ICalAttendeeData, parent: ICalAlarm | ICalEvent) {
108
114
  this.data = {
109
- name: null,
115
+ delegatedFrom: null,
116
+ delegatedTo: null,
110
117
  email: '',
111
118
  mailto: null,
112
- sentBy: null,
113
- status: null,
119
+ name: null,
114
120
  role: ICalAttendeeRole.REQ,
115
121
  rsvp: null,
122
+ sentBy: null,
123
+ status: null,
116
124
  type: null,
117
- delegatedTo: null,
118
- delegatedFrom: null,
119
- x: []
125
+ x: [],
120
126
  };
121
127
  this.parent = parent;
122
128
  if (!this.parent) {
@@ -135,46 +141,182 @@ export default class ICalAttendee {
135
141
  if (data.rsvp !== undefined) this.rsvp(data.rsvp);
136
142
  if (data.type !== undefined) this.type(data.type);
137
143
  if (data.delegatedTo !== undefined) this.delegatedTo(data.delegatedTo);
138
- if (data.delegatedFrom !== undefined) this.delegatedFrom(data.delegatedFrom);
144
+ if (data.delegatedFrom !== undefined)
145
+ this.delegatedFrom(data.delegatedFrom);
139
146
  if (data.delegatesTo) this.delegatesTo(data.delegatesTo);
140
147
  if (data.delegatesFrom) this.delegatesFrom(data.delegatesFrom);
141
148
  if (data.x !== undefined) this.x(data.x);
142
149
  }
143
150
 
144
-
145
151
  /**
146
- * Get the attendee's name
152
+ * Get the attendee's delegated-from field
147
153
  * @since 0.2.0
148
154
  */
149
- name(): string | null;
155
+ delegatedFrom(): ICalAttendee | null;
156
+ /**
157
+ * Set the attendee's delegated-from field
158
+ *
159
+ * Creates a new Attendee if the passed object is not already a
160
+ * {@link ICalAttendee} object. Will set the `delegatedTo` and
161
+ * `delegatedFrom` attributes.
162
+ *
163
+ * @param delegatedFrom
164
+ */
165
+ delegatedFrom(
166
+ delegatedFrom: ICalAttendee | ICalAttendeeData | null | string,
167
+ ): this;
168
+ delegatedFrom(
169
+ delegatedFrom?: ICalAttendee | ICalAttendeeData | null | string,
170
+ ): ICalAttendee | null | this {
171
+ if (delegatedFrom === undefined) {
172
+ return this.data.delegatedFrom;
173
+ }
174
+
175
+ if (!delegatedFrom) {
176
+ this.data.delegatedFrom = null;
177
+ } else if (typeof delegatedFrom === 'string') {
178
+ this.data.delegatedFrom = new ICalAttendee(
179
+ {
180
+ email: delegatedFrom,
181
+ ...checkNameAndMail('delegatedFrom', delegatedFrom),
182
+ },
183
+ this.parent,
184
+ );
185
+ } else if (delegatedFrom instanceof ICalAttendee) {
186
+ this.data.delegatedFrom = delegatedFrom;
187
+ } else {
188
+ this.data.delegatedFrom = new ICalAttendee(
189
+ delegatedFrom,
190
+ this.parent,
191
+ );
192
+ }
193
+
194
+ return this;
195
+ }
150
196
 
151
197
  /**
152
- * Set the attendee's name
198
+ * Get the attendee's delegated-to value.
153
199
  * @since 0.2.0
154
200
  */
155
- name(name: string | null): this;
156
- name(name?: string | null): this | string | null {
157
- if (name === undefined) {
158
- return this.data.name;
201
+ delegatedTo(): ICalAttendee | null;
202
+ /**
203
+ * Set the attendee's delegated-to field.
204
+ *
205
+ * Creates a new Attendee if the passed object is not already a
206
+ * {@link ICalAttendee} object. Will set the `delegatedTo` and
207
+ * `delegatedFrom` attributes.
208
+ *
209
+ * Will also set the `status` to `DELEGATED`, if attribute is set.
210
+ *
211
+ * ```javascript
212
+ * const cal = ical();
213
+ * const event = cal.createEvent();
214
+ * const attendee = cal.createAttendee();
215
+ *
216
+ * attendee.delegatesTo({email: 'foo@bar.com', name: 'Foo'});
217
+ ```
218
+ *
219
+ * @since 0.2.0
220
+ */
221
+ delegatedTo(
222
+ delegatedTo: ICalAttendee | ICalAttendeeData | null | string,
223
+ ): this;
224
+ delegatedTo(
225
+ delegatedTo?: ICalAttendee | ICalAttendeeData | null | string,
226
+ ): ICalAttendee | null | this {
227
+ if (delegatedTo === undefined) {
228
+ return this.data.delegatedTo;
229
+ }
230
+ if (!delegatedTo) {
231
+ this.data.delegatedTo = null;
232
+ if (this.data.status === ICalAttendeeStatus.DELEGATED) {
233
+ this.data.status = null;
234
+ }
235
+ return this;
159
236
  }
160
237
 
161
- this.data.name = name || null;
238
+ if (typeof delegatedTo === 'string') {
239
+ this.data.delegatedTo = new ICalAttendee(
240
+ {
241
+ email: delegatedTo,
242
+ ...checkNameAndMail('delegatedTo', delegatedTo),
243
+ },
244
+ this.parent,
245
+ );
246
+ } else if (delegatedTo instanceof ICalAttendee) {
247
+ this.data.delegatedTo = delegatedTo;
248
+ } else {
249
+ this.data.delegatedTo = new ICalAttendee(delegatedTo, this.parent);
250
+ }
251
+
252
+ this.data.status = ICalAttendeeStatus.DELEGATED;
162
253
  return this;
163
254
  }
164
255
 
256
+ /**
257
+ * Create a new attendee this attendee delegates from and returns
258
+ * this new attendee. Creates a new attendee if the passed object
259
+ * is not already an {@link ICalAttendee}.
260
+ *
261
+ * ```javascript
262
+ * const cal = ical();
263
+ * const event = cal.createEvent();
264
+ * const attendee = cal.createAttendee();
265
+ *
266
+ * attendee.delegatesFrom({email: 'foo@bar.com', name: 'Foo'});
267
+ * ```
268
+ *
269
+ * @since 0.2.0
270
+ */
271
+ delegatesFrom(
272
+ options: ICalAttendee | ICalAttendeeData | string,
273
+ ): ICalAttendee {
274
+ const a =
275
+ options instanceof ICalAttendee
276
+ ? options
277
+ : this.parent.createAttendee(options);
278
+ this.delegatedFrom(a);
279
+ a.delegatedTo(this);
280
+ return a;
281
+ }
165
282
 
283
+ /**
284
+ * Create a new attendee this attendee delegates to and returns
285
+ * this new attendee. Creates a new attendee if the passed object
286
+ * is not already an {@link ICalAttendee}.
287
+ *
288
+ * ```javascript
289
+ * const cal = ical();
290
+ * const event = cal.createEvent();
291
+ * const attendee = cal.createAttendee();
292
+ *
293
+ * attendee.delegatesTo({email: 'foo@bar.com', name: 'Foo'});
294
+ * ```
295
+ *
296
+ * @since 0.2.0
297
+ */
298
+ delegatesTo(
299
+ options: ICalAttendee | ICalAttendeeData | string,
300
+ ): ICalAttendee {
301
+ const a =
302
+ options instanceof ICalAttendee
303
+ ? options
304
+ : this.parent.createAttendee(options);
305
+ this.delegatedTo(a);
306
+ a.delegatedFrom(this);
307
+ return a;
308
+ }
166
309
  /**
167
310
  * Get the attendee's email address
168
311
  * @since 0.2.0
169
312
  */
170
313
  email(): string;
171
-
172
314
  /**
173
315
  * Set the attendee's email address
174
316
  * @since 0.2.0
175
317
  */
176
318
  email(email: string): this;
177
- email(email?: string): this | string {
319
+ email(email?: string): string | this {
178
320
  if (!email) {
179
321
  return this.data.email;
180
322
  }
@@ -182,19 +324,17 @@ export default class ICalAttendee {
182
324
  this.data.email = email;
183
325
  return this;
184
326
  }
185
-
186
327
  /**
187
328
  * Get the attendee's email address
188
329
  * @since 1.3.0
189
330
  */
190
- mailto(): string | null;
191
-
331
+ mailto(): null | string;
192
332
  /**
193
333
  * Set the attendee's email address
194
334
  * @since 1.3.0
195
335
  */
196
- mailto(mailto: string | null): this;
197
- mailto(mailto?: string | null): this | string | null {
336
+ mailto(mailto: null | string): this;
337
+ mailto(mailto?: null | string): null | string | this {
198
338
  if (mailto === undefined) {
199
339
  return this.data.mailto;
200
340
  }
@@ -202,35 +342,29 @@ export default class ICalAttendee {
202
342
  this.data.mailto = mailto || null;
203
343
  return this;
204
344
  }
205
-
206
-
207
345
  /**
208
- * Get the acting user's email adress
209
- * @since 3.3.0
346
+ * Get the attendee's name
347
+ * @since 0.2.0
210
348
  */
211
- sentBy(): string | null;
212
-
349
+ name(): null | string;
213
350
  /**
214
- * Set the acting user's email adress
215
- * @since 3.3.0
351
+ * Set the attendee's name
352
+ * @since 0.2.0
216
353
  */
217
- sentBy(email: string | null): this;
218
- sentBy(email?: string | null): this | string | null {
219
- if (!email) {
220
- return this.data.sentBy;
354
+ name(name: null | string): this;
355
+ name(name?: null | string): null | string | this {
356
+ if (name === undefined) {
357
+ return this.data.name;
221
358
  }
222
359
 
223
- this.data.sentBy = email;
360
+ this.data.name = name || null;
224
361
  return this;
225
362
  }
226
-
227
-
228
363
  /**
229
364
  * Get attendee's role
230
365
  * @since 0.2.0
231
366
  */
232
367
  role(): ICalAttendeeRole;
233
-
234
368
  /**
235
369
  * Set the attendee's role, defaults to `REQ` / `REQ-PARTICIPANT`.
236
370
  * Checkout {@link ICalAttendeeRole} for available roles.
@@ -238,7 +372,7 @@ export default class ICalAttendee {
238
372
  * @since 0.2.0
239
373
  */
240
374
  role(role: ICalAttendeeRole): this;
241
- role(role?: ICalAttendeeRole): this | ICalAttendeeRole {
375
+ role(role?: ICalAttendeeRole): ICalAttendeeRole | this {
242
376
  if (role === undefined) {
243
377
  return this.data.role;
244
378
  }
@@ -246,20 +380,17 @@ export default class ICalAttendee {
246
380
  this.data.role = checkEnum(ICalAttendeeRole, role) as ICalAttendeeRole;
247
381
  return this;
248
382
  }
249
-
250
-
251
383
  /**
252
384
  * Get attendee's RSVP expectation
253
385
  * @since 0.2.1
254
386
  */
255
387
  rsvp(): boolean | null;
256
-
257
388
  /**
258
389
  * Set the attendee's RSVP expectation
259
390
  * @since 0.2.1
260
391
  */
261
392
  rsvp(rsvp: boolean | null): this;
262
- rsvp(rsvp?: boolean | null): this | boolean | null {
393
+ rsvp(rsvp?: boolean | null): boolean | null | this {
263
394
  if (rsvp === undefined) {
264
395
  return this.data.rsvp;
265
396
  }
@@ -271,14 +402,29 @@ export default class ICalAttendee {
271
402
  this.data.rsvp = Boolean(rsvp);
272
403
  return this;
273
404
  }
405
+ /**
406
+ * Get the acting user's email adress
407
+ * @since 3.3.0
408
+ */
409
+ sentBy(): null | string;
410
+ /**
411
+ * Set the acting user's email adress
412
+ * @since 3.3.0
413
+ */
414
+ sentBy(email: null | string): this;
415
+ sentBy(email?: null | string): null | string | this {
416
+ if (!email) {
417
+ return this.data.sentBy;
418
+ }
274
419
 
275
-
420
+ this.data.sentBy = email;
421
+ return this;
422
+ }
276
423
  /**
277
424
  * Get attendee's status
278
425
  * @since 0.2.0
279
426
  */
280
427
  status(): ICalAttendeeStatus | null;
281
-
282
428
  /**
283
429
  * Set the attendee's status. See {@link ICalAttendeeStatus}
284
430
  * for available status options.
@@ -286,7 +432,9 @@ export default class ICalAttendee {
286
432
  * @since 0.2.0
287
433
  */
288
434
  status(status: ICalAttendeeStatus | null): this;
289
- status(status?: ICalAttendeeStatus | null): this | ICalAttendeeStatus | null {
435
+ status(
436
+ status?: ICalAttendeeStatus | null,
437
+ ): ICalAttendeeStatus | null | this {
290
438
  if (status === undefined) {
291
439
  return this.data.status;
292
440
  }
@@ -295,178 +443,126 @@ export default class ICalAttendee {
295
443
  return this;
296
444
  }
297
445
 
298
- this.data.status = checkEnum(ICalAttendeeStatus, status) as ICalAttendeeStatus;
446
+ this.data.status = checkEnum(
447
+ ICalAttendeeStatus,
448
+ status,
449
+ ) as ICalAttendeeStatus;
299
450
  return this;
300
451
  }
301
-
302
-
303
452
  /**
304
- * Get attendee's type (a.k.a. CUTYPE)
305
- * @since 0.2.3
306
- */
307
- type(): ICalAttendeeType;
308
-
309
- /**
310
- * Set attendee's type (a.k.a. CUTYPE).
311
- * See {@link ICalAttendeeType} for available status options.
453
+ * Return a shallow copy of the attendee's options for JSON stringification.
454
+ * Can be used for persistence.
312
455
  *
313
- * @since 0.2.3
456
+ * @since 0.2.4
314
457
  */
315
- type(type: ICalAttendeeType | null): this;
316
- type(type?: ICalAttendeeType | null): this | ICalAttendeeType | null {
317
- if (type === undefined) {
318
- return this.data.type;
319
- }
320
- if (!type) {
321
- this.data.type = null;
322
- return this;
323
- }
324
-
325
- this.data.type = checkEnum(ICalAttendeeType, type) as ICalAttendeeType;
326
- return this;
458
+ toJSON(): ICalAttendeeJSONData {
459
+ return Object.assign({}, this.data, {
460
+ delegatedFrom: this.data.delegatedFrom?.email() || null,
461
+ delegatedTo: this.data.delegatedTo?.email() || null,
462
+ x: this.x(),
463
+ });
327
464
  }
328
465
 
329
-
330
- /**
331
- * Get the attendee's delegated-to value.
332
- * @since 0.2.0
333
- */
334
- delegatedTo(): ICalAttendee | null;
335
-
336
466
  /**
337
- * Set the attendee's delegated-to field.
338
- *
339
- * Creates a new Attendee if the passed object is not already a
340
- * {@link ICalAttendee} object. Will set the `delegatedTo` and
341
- * `delegatedFrom` attributes.
342
- *
343
- * Will also set the `status` to `DELEGATED`, if attribute is set.
467
+ * Return generated attendee as a string.
344
468
  *
345
469
  * ```javascript
346
- * const cal = ical();
347
- * const event = cal.createEvent();
348
- * const attendee = cal.createAttendee();
349
- *
350
- * attendee.delegatesTo({email: 'foo@bar.com', name: 'Foo'});
351
- ```
352
- *
353
- * @since 0.2.0
470
+ * console.log(attendee.toString()); // ATTENDEE;ROLE=…
471
+ * ```
354
472
  */
355
- delegatedTo(delegatedTo: ICalAttendee | ICalAttendeeData | string | null): this;
356
- delegatedTo(delegatedTo?: ICalAttendee | ICalAttendeeData | string | null): this | ICalAttendee | null {
357
- if (delegatedTo === undefined) {
358
- return this.data.delegatedTo;
359
- }
360
- if (!delegatedTo) {
361
- this.data.delegatedTo = null;
362
- if (this.data.status === ICalAttendeeStatus.DELEGATED) {
363
- this.data.status = null;
364
- }
365
- return this;
366
- }
473
+ toString(): string {
474
+ let g = 'ATTENDEE';
367
475
 
368
- if(typeof delegatedTo === 'string') {
369
- this.data.delegatedTo = new ICalAttendee(
370
- { email: delegatedTo, ...checkNameAndMail('delegatedTo', delegatedTo) },
371
- this.parent,
372
- );
373
- }
374
- else if(delegatedTo instanceof ICalAttendee) {
375
- this.data.delegatedTo = delegatedTo;
376
- }
377
- else {
378
- this.data.delegatedTo = new ICalAttendee(delegatedTo, this.parent);
476
+ if (!this.data.email) {
477
+ throw new Error('No value for `email` in ICalAttendee given!');
379
478
  }
380
479
 
381
- this.data.status = ICalAttendeeStatus.DELEGATED;
382
- return this;
383
- }
480
+ // ROLE
481
+ g += ';ROLE=' + this.data.role;
384
482
 
483
+ // TYPE
484
+ if (this.data.type) {
485
+ g += ';CUTYPE=' + this.data.type;
486
+ }
385
487
 
386
- /**
387
- * Get the attendee's delegated-from field
388
- * @since 0.2.0
389
- */
390
- delegatedFrom (): ICalAttendee | null;
488
+ // PARTSTAT
489
+ if (this.data.status) {
490
+ g += ';PARTSTAT=' + this.data.status;
491
+ }
391
492
 
392
- /**
393
- * Set the attendee's delegated-from field
394
- *
395
- * Creates a new Attendee if the passed object is not already a
396
- * {@link ICalAttendee} object. Will set the `delegatedTo` and
397
- * `delegatedFrom` attributes.
398
- *
399
- * @param delegatedFrom
400
- */
401
- delegatedFrom (delegatedFrom: ICalAttendee | ICalAttendeeData | string | null): this;
402
- delegatedFrom(delegatedFrom?: ICalAttendee | ICalAttendeeData | string | null): this | ICalAttendee | null {
403
- if (delegatedFrom === undefined) {
404
- return this.data.delegatedFrom;
493
+ // RSVP
494
+ if (this.data.rsvp !== null) {
495
+ g += ';RSVP=' + this.data.rsvp.toString().toUpperCase();
405
496
  }
406
497
 
407
- if (!delegatedFrom) {
408
- this.data.delegatedFrom = null;
498
+ // SENT-BY
499
+ if (this.data.sentBy !== null) {
500
+ g += ';SENT-BY="mailto:' + this.data.sentBy + '"';
409
501
  }
410
- else if(typeof delegatedFrom === 'string') {
411
- this.data.delegatedFrom = new ICalAttendee(
412
- { email: delegatedFrom, ...checkNameAndMail('delegatedFrom', delegatedFrom) },
413
- this.parent,
414
- );
502
+
503
+ // DELEGATED-TO
504
+ if (this.data.delegatedTo) {
505
+ g += ';DELEGATED-TO="' + this.data.delegatedTo.email() + '"';
415
506
  }
416
- else if(delegatedFrom instanceof ICalAttendee) {
417
- this.data.delegatedFrom = delegatedFrom;
507
+
508
+ // DELEGATED-FROM
509
+ if (this.data.delegatedFrom) {
510
+ g += ';DELEGATED-FROM="' + this.data.delegatedFrom.email() + '"';
418
511
  }
419
- else {
420
- this.data.delegatedFrom = new ICalAttendee(delegatedFrom, this.parent);
512
+
513
+ // CN / Name
514
+ if (this.data.name) {
515
+ g += ';CN="' + escape(this.data.name, true) + '"';
421
516
  }
422
517
 
423
- return this;
424
- }
518
+ // EMAIL
519
+ if (this.data.email && this.data.mailto) {
520
+ g += ';EMAIL=' + escape(this.data.email, false);
521
+ }
425
522
 
523
+ // CUSTOM X ATTRIBUTES
524
+ if (this.data.x.length) {
525
+ g +=
526
+ ';' +
527
+ this.data.x
528
+ .map(
529
+ ([key, value]) =>
530
+ key.toUpperCase() + '=' + escape(value, false),
531
+ )
532
+ .join(';');
533
+ }
534
+
535
+ g +=
536
+ ':MAILTO:' +
537
+ escape(this.data.mailto || this.data.email, false) +
538
+ '\r\n';
426
539
 
427
- /**
428
- * Create a new attendee this attendee delegates to and returns
429
- * this new attendee. Creates a new attendee if the passed object
430
- * is not already an {@link ICalAttendee}.
431
- *
432
- * ```javascript
433
- * const cal = ical();
434
- * const event = cal.createEvent();
435
- * const attendee = cal.createAttendee();
436
- *
437
- * attendee.delegatesTo({email: 'foo@bar.com', name: 'Foo'});
438
- * ```
439
- *
440
- * @since 0.2.0
441
- */
442
- delegatesTo (options: ICalAttendee | ICalAttendeeData | string): ICalAttendee {
443
- const a = options instanceof ICalAttendee ? options : this.parent.createAttendee(options);
444
- this.delegatedTo(a);
445
- a.delegatedFrom(this);
446
- return a;
540
+ return g;
447
541
  }
448
542
 
449
-
450
543
  /**
451
- * Create a new attendee this attendee delegates from and returns
452
- * this new attendee. Creates a new attendee if the passed object
453
- * is not already an {@link ICalAttendee}.
454
- *
455
- * ```javascript
456
- * const cal = ical();
457
- * const event = cal.createEvent();
458
- * const attendee = cal.createAttendee();
459
- *
460
- * attendee.delegatesFrom({email: 'foo@bar.com', name: 'Foo'});
461
- * ```
544
+ * Get attendee's type (a.k.a. CUTYPE)
545
+ * @since 0.2.3
546
+ */
547
+ type(): ICalAttendeeType;
548
+ /**
549
+ * Set attendee's type (a.k.a. CUTYPE).
550
+ * See {@link ICalAttendeeType} for available status options.
462
551
  *
463
- * @since 0.2.0
552
+ * @since 0.2.3
464
553
  */
465
- delegatesFrom (options: ICalAttendee | ICalAttendeeData | string): ICalAttendee {
466
- const a = options instanceof ICalAttendee ? options : this.parent.createAttendee(options);
467
- this.delegatedFrom(a);
468
- a.delegatedTo(this);
469
- return a;
554
+ type(type: ICalAttendeeType | null): this;
555
+ type(type?: ICalAttendeeType | null): ICalAttendeeType | null | this {
556
+ if (type === undefined) {
557
+ return this.data.type;
558
+ }
559
+ if (!type) {
560
+ this.data.type = null;
561
+ return this;
562
+ }
563
+
564
+ this.data.type = checkEnum(ICalAttendeeType, type) as ICalAttendeeType;
565
+ return this;
470
566
  }
471
567
 
472
568
  /**
@@ -493,8 +589,12 @@ export default class ICalAttendee {
493
589
  *
494
590
  * @since 1.9.0
495
591
  */
496
- x (keyOrArray: {key: string, value: string}[] | [string, string][] | Record<string, string>): this;
497
-
592
+ x(
593
+ keyOrArray:
594
+ | [string, string][]
595
+ | Record<string, string>
596
+ | { key: string; value: string }[],
597
+ ): this;
498
598
  /**
499
599
  * Set a X-* attribute. Woun't filter double attributes,
500
600
  * which are also added by another method (e.g. status),
@@ -506,113 +606,32 @@ export default class ICalAttendee {
506
606
  *
507
607
  * @since 1.9.0
508
608
  */
509
- x (keyOrArray: string, value: string): this;
510
-
609
+ x(keyOrArray: string, value: string): this;
511
610
  /**
512
611
  * Get all custom X-* attributes.
513
612
  * @since 1.9.0
514
613
  */
515
- x (): {key: string, value: string}[];
516
- x (keyOrArray?: ({key: string, value: string})[] | [string, string][] | Record<string, string> | string, value?: string): this | void | ({key: string, value: string})[] {
517
- if(keyOrArray === undefined) {
518
- return addOrGetCustomAttributes (this.data);
519
- }
520
-
521
- if(typeof keyOrArray === 'string' && typeof value === 'string') {
522
- addOrGetCustomAttributes (this.data, keyOrArray, value);
523
- }
524
- else if(typeof keyOrArray === 'object') {
525
- addOrGetCustomAttributes (this.data, keyOrArray);
526
- }
527
- else {
614
+ x(): { key: string; value: string }[];
615
+ x(
616
+ keyOrArray?:
617
+ | [string, string][]
618
+ | Record<string, string>
619
+ | string
620
+ | { key: string; value: string }[],
621
+ value?: string,
622
+ ): this | void | { key: string; value: string }[] {
623
+ if (keyOrArray === undefined) {
624
+ return addOrGetCustomAttributes(this.data);
625
+ }
626
+
627
+ if (typeof keyOrArray === 'string' && typeof value === 'string') {
628
+ addOrGetCustomAttributes(this.data, keyOrArray, value);
629
+ } else if (typeof keyOrArray === 'object') {
630
+ addOrGetCustomAttributes(this.data, keyOrArray);
631
+ } else {
528
632
  throw new Error('Either key or value is not a string!');
529
633
  }
530
634
 
531
635
  return this;
532
636
  }
533
-
534
-
535
- /**
536
- * Return a shallow copy of the attendee's options for JSON stringification.
537
- * Can be used for persistence.
538
- *
539
- * @since 0.2.4
540
- */
541
- toJSON(): ICalAttendeeJSONData {
542
- return Object.assign({}, this.data, {
543
- delegatedTo: this.data.delegatedTo?.email() || null,
544
- delegatedFrom: this.data.delegatedFrom?.email() || null,
545
- x: this.x()
546
- });
547
- }
548
-
549
-
550
- /**
551
- * Return generated attendee as a string.
552
- *
553
- * ```javascript
554
- * console.log(attendee.toString()); // → ATTENDEE;ROLE=…
555
- * ```
556
- */
557
- toString (): string {
558
- let g = 'ATTENDEE';
559
-
560
- if (!this.data.email) {
561
- throw new Error('No value for `email` in ICalAttendee given!');
562
- }
563
-
564
- // ROLE
565
- g += ';ROLE=' + this.data.role;
566
-
567
- // TYPE
568
- if (this.data.type) {
569
- g += ';CUTYPE=' + this.data.type;
570
- }
571
-
572
- // PARTSTAT
573
- if (this.data.status) {
574
- g += ';PARTSTAT=' + this.data.status;
575
- }
576
-
577
- // RSVP
578
- if (this.data.rsvp !== null) {
579
- g += ';RSVP=' + this.data.rsvp.toString().toUpperCase();
580
- }
581
-
582
- // SENT-BY
583
- if (this.data.sentBy !== null) {
584
- g += ';SENT-BY="mailto:' + this.data.sentBy + '"';
585
- }
586
-
587
- // DELEGATED-TO
588
- if (this.data.delegatedTo) {
589
- g += ';DELEGATED-TO="' + this.data.delegatedTo.email() + '"';
590
- }
591
-
592
- // DELEGATED-FROM
593
- if (this.data.delegatedFrom) {
594
- g += ';DELEGATED-FROM="' + this.data.delegatedFrom.email() + '"';
595
- }
596
-
597
- // CN / Name
598
- if (this.data.name) {
599
- g += ';CN="' + escape(this.data.name, true) + '"';
600
- }
601
-
602
- // EMAIL
603
- if (this.data.email && this.data.mailto) {
604
- g += ';EMAIL=' + escape(this.data.email, false);
605
- }
606
-
607
- // CUSTOM X ATTRIBUTES
608
- if(this.data.x.length) {
609
- g += ';' + this.data.x
610
- .map(([key, value]) => key.toUpperCase() + '=' + escape(value, false))
611
- .join(';');
612
- }
613
-
614
- g += ':MAILTO:' + escape(this.data.mailto || this.data.email, false) + '\r\n';
615
-
616
- return g;
617
- }
618
637
  }