Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 386x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 3x 3x 36x 36x 28x 28x 28x 36x 36x 1x 1x 36x 36x 1x 1x 36x 1x 1x 36x 36x 36x 36x 36x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 6x 6x 29x 10x 10x 10x 29x 29x 13x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 4x 4x 21x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 4x 4x 21x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 239x 205x 205x 239x 239x 1x 1x 33x 12x 12x 21x 15x 15x 6x 6x 6x 239x 239x 239x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 2x 2x 16x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 5x 5x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 4x 4x 19x 19x 12x 12x 3x 3x 3x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 3x 3x 20x 20x 2x 2x 15x 3x 3x 12x 12x 12x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 57x 57x 57x 57x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 5x 5x 32x 32x 27x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 54x 24x 24x 54x 54x 6x 6x 24x 22x 22x 2x 2x 2x 54x 54x 54x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 1x 1x 48x 48x 48x 1x 1x 48x 48x 48x 48x 48x 48x 7x 7x 48x 48x 48x 2x 2x 2x 48x 48x 48x 1x 1x 48x 48x 48x 6x 6x 6x 6x 6x 6x 8x 8x 8x 8x 1x 1x 8x 8x 7x 7x 6x 6x 48x 14x 14x 14x 48x 48x 48x 2x 2x 2x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 1x | 'use strict'; import { addOrGetCustomAttributes, checkEnum, foldLines, generateCustomAttributes, isMomentDuration, toDurationString } from './tools.ts'; import ICalEvent, { type ICalEventData, type ICalEventJSONData } from './event.ts'; import { type ICalMomentDurationStub, type ICalTimezone } from './types.ts'; export interface ICalCalendarData { prodId?: ICalCalendarProdIdData | string; method?: ICalCalendarMethod | null; name?: string | null; description?: string | null; timezone?: ICalTimezone | string | null; source?: string | null; url?: string | null; scale?: string | null; ttl?: number | ICalMomentDurationStub | null; events?: (ICalEvent | ICalEventData)[]; x?: {key: string, value: string}[] | [string, string][] | Record<string, string>; } interface ICalCalendarInternalData { prodId: string; method: ICalCalendarMethod | null; name: string | null; description: string | null; timezone: ICalTimezone | null; source: string | null; url: string | null; scale: string | null; ttl: number | null; events: ICalEvent[]; x: [string, string][]; } export interface ICalCalendarJSONData { prodId: string; method: ICalCalendarMethod | null; name: string | null; description: string | null; timezone: string | null; source: string | null; url: string | null; scale: string | null; ttl: number | null; events: ICalEventJSONData[]; x: {key: string, value: string}[]; } export interface ICalCalendarProdIdData { company: string; product: string; language?: string; } export enum ICalCalendarMethod { PUBLISH = 'PUBLISH', REQUEST = 'REQUEST', REPLY = 'REPLY', ADD = 'ADD', CANCEL = 'CANCEL', REFRESH = 'REFRESH', COUNTER = 'COUNTER', DECLINECOUNTER = 'DECLINECOUNTER' } /** * Usually you get an {@link ICalCalendar} object like this: * ```javascript * import ical from 'ical-generator'; * const calendar = ical(); * ``` * * But you can also use the constructor directly like this: * ```javascript * import {ICalCalendar} from 'ical-generator'; * const calendar = new ICalCalendar(); * ``` */ export default class ICalCalendar { private readonly data: ICalCalendarInternalData; /** * You can pass options to set up your calendar or use setters to do this. * * ```javascript * * import ical from 'ical-generator'; * * // or use require: * // const { default: ical } = require('ical-generator'); * * * const cal = ical({name: 'my first iCal'}); * * // is the same as * * const cal = ical().name('my first iCal'); * * // is the same as * * const cal = ical(); * cal.name('sebbo.net'); * ``` * * `cal.toString()` would then produce the following string: * ```text * BEGIN:VCALENDAR * VERSION:2.0 * PRODID:-//sebbo.net//ical-generator//EN * NAME:sebbo.net * X-WR-CALNAME:sebbo.net * END:VCALENDAR * ``` * * @param data Calendar data */ constructor(data: ICalCalendarData = {}) { this.data = { prodId: '//sebbo.net//ical-generator//EN', method: null, name: null, description: null, timezone: null, source: null, url: null, scale: null, ttl: null, events: [], x: [] }; if (data.prodId !== undefined) this.prodId(data.prodId); if (data.method !== undefined) this.method(data.method); if (data.name !== undefined) this.name(data.name); if (data.description !== undefined) this.description(data.description); if (data.timezone !== undefined) this.timezone(data.timezone); if (data.source !== undefined) this.source(data.source); if (data.url !== undefined) this.url(data.url); if (data.scale !== undefined) this.scale(data.scale); if (data.ttl !== undefined) this.ttl(data.ttl); if (data.events !== undefined) this.events(data.events); if (data.x !== undefined) this.x(data.x); } /** * Get your feed's prodid. Will always return a string. * @since 0.2.0 */ prodId(): string; /** * Set your feed's prodid. `prodid` can be either a * string like `//sebbo.net//ical-generator//EN` or a * valid {@link ICalCalendarProdIdData} object. `language` * is optional and defaults to `EN`. * * ```javascript * cal.prodId({ * company: 'My Company', * product: 'My Product', * language: 'EN' // optional, defaults to EN * }); * ``` * * `cal.toString()` would then produce the following string: * ```text * PRODID:-//My Company//My Product//EN * ``` * * @since 0.2.0 */ prodId(prodId: ICalCalendarProdIdData | string): this; prodId(prodId?: ICalCalendarProdIdData | string): this | string { if (!prodId) { return this.data.prodId; } if (typeof prodId === 'string') { this.data.prodId = prodId; return this; } if (typeof prodId !== 'object') { throw new Error('`prodid` needs to be a string or an object!'); } if (!prodId.company) { throw new Error('`prodid.company` is a mandatory item!'); } if (!prodId.product) { throw new Error('`prodid.product` is a mandatory item!'); } const language = (prodId.language || 'EN').toUpperCase(); this.data.prodId = '//' + prodId.company + '//' + prodId.product + '//' + language; return this; } /** * Get the feed method attribute. * See {@link ICalCalendarMethod} for possible results. * * @since 0.2.8 */ method(): ICalCalendarMethod | null; /** * Set the feed method attribute. * See {@link ICalCalendarMethod} for available options. * * #### Typescript Example * ```typescript * import {ICalCalendarMethod} from 'ical-generator'; * * // METHOD:PUBLISH * calendar.method(ICalCalendarMethod.PUBLISH); * ``` * * @since 0.2.8 */ method(method: ICalCalendarMethod | null): this; method(method?: ICalCalendarMethod | null): this | ICalCalendarMethod | null { if (method === undefined) { return this.data.method; } if (!method) { this.data.method = null; return this; } this.data.method = checkEnum(ICalCalendarMethod, method) as ICalCalendarMethod; return this; } /** * Get your feed's name * @since 0.2.0 */ name(): string | null; /** * Set your feed's name. Is used to fill `NAME` * and `X-WR-CALNAME` in your iCal file. * * ```typescript * import ical from 'ical-generator'; * * const cal = ical(); * cal.name('Next Arrivals'); * * cal.toString(); * ``` * * ```text * BEGIN:VCALENDAR * VERSION:2.0 * PRODID:-//sebbo.net//ical-generator//EN * NAME:Next Arrivals * X-WR-CALNAME:Next Arrivals * END:VCALENDAR * ``` * * @since 0.2.0 */ name(name: string | null): this; name(name?: string | null): this | string | null { if (name === undefined) { return this.data.name; } this.data.name = name ? String(name) : null; return this; } /** * Get your feed's description * @since 0.2.7 */ description(): string | null; /** * Set your feed's description * @since 0.2.7 */ description(description: string | null): this; description(description?: string | null): this | string | null { if (description === undefined) { return this.data.description; } this.data.description = description ? String(description) : null; return this; } /** * Get the current calendar timezone * @since 0.2.0 */ timezone(): string | null; /** * Use this method to set your feed's timezone. Is used * to fill `TIMEZONE-ID` and `X-WR-TIMEZONE` in your iCal export. * Please not that all date values are treaded differently, if * a timezone was set. See {@link formatDate} for details. If no * time zone is specified, all information is output as UTC. * * ```javascript * cal.timezone('America/New_York'); * ``` * * @see https://github.com/sebbo2002/ical-generator#-date-time--timezones * @since 0.2.0 */ timezone(timezone: string | null): this; /** * Sets the time zone to be used in this calendar file for all times of all * events. Please note that if the time zone is set, ical-generator assumes * that all times are already in the correct time zone. Alternatively, a * `moment-timezone` or a Luxon object can be passed with `setZone`, * ical-generator will then set the time zone itself. * * For the best support of time zones, a VTimezone entry in the calendar is * recommended, which informs the client about the corresponding time zones * (daylight saving time, deviation from UTC, etc.). `ical-generator` itself * does not have a time zone database, so an external generator is needed here. * * A VTimezone generator is a function that takes a time zone as a string and * returns a VTimezone component according to the ical standard. For example, * ical-timezones can be used for this: * * ```typescript * import ical from 'ical-generator'; * import {getVtimezoneComponent} from '@touch4it/ical-timezones'; * * const cal = ical(); * cal.timezone({ * name: 'FOO', * generator: getVtimezoneComponent * }); * cal.createEvent({ * start: new Date(), * timezone: 'Europe/London' * }); * ``` * * @see https://github.com/sebbo2002/ical-generator#-date-time--timezones * @since 2.0.0 */ timezone(timezone: ICalTimezone | string | null): this; timezone(timezone?: ICalTimezone | string | null): this | string | null { if (timezone === undefined) { return this.data.timezone?.name || null; } if(timezone === 'UTC') { this.data.timezone = null; } else if(typeof timezone === 'string') { this.data.timezone = {name: timezone}; } else if(timezone === null) { this.data.timezone = null; } else { this.data.timezone = timezone; } return this; } /** * Get current value of the `SOURCE` attribute. * @since 2.2.0-develop.1 */ source(): string | null; /** * Use this method to set your feed's `SOURCE` attribute. * This tells the client where to refresh your feed. * * ```javascript * cal.source('http://example.com/my/original_source.ical'); * ``` * * ```text * SOURCE;VALUE=URI:http://example.com/my/original_source.ical * ``` * * @since 2.2.0-develop.1 */ source(source: string | null): this; source(source?: string | null): this | string | null { if (source === undefined) { return this.data.source; } this.data.source = source || null; return this; } /** * Get your feed's URL * @since 0.2.5 */ url(): string | null; /** * Set your feed's URL * * ```javascript * calendar.url('http://example.com/my/feed.ical'); * ``` * * @since 0.2.5 */ url(url: string | null): this; url(url?: string | null): this | string | null { if (url === undefined) { return this.data.url; } this.data.url = url || null; return this; } /** * Get current value of the `CALSCALE` attribute. It will * return `null` if no value was set. The iCal standard * specifies this as `GREGORIAN` if no value is present. * * @since 1.8.0 */ scale(): string | null; /** * Use this method to set your feed's `CALSCALE` attribute. There is no * default value for this property and it will not appear in your iCal * file unless set. The iCal standard specifies this as `GREGORIAN` if * no value is present. * * ```javascript * cal.scale('gregorian'); * ``` * * @since 1.8.0 */ scale(scale: string | null): this; scale(scale?: string | null): this | string | null { if (scale === undefined) { return this.data.scale; } if (scale === null) { this.data.scale = null; } else { this.data.scale = scale.toUpperCase(); } return this; } /** * Get the current ttl duration in seconds * @since 0.2.5 */ ttl(): number | null; /** * Use this method to set your feed's time to live * (in seconds). Is used to fill `REFRESH-INTERVAL` and * `X-PUBLISHED-TTL` in your iCal. * * ```javascript * const cal = ical().ttl(60 * 60 * 24); // 1 day * ``` * * You can also pass a moment.js duration object. Zero, null * or negative numbers will reset the `ttl` attribute. * * @since 0.2.5 */ ttl(ttl: number | ICalMomentDurationStub | null): this; ttl(ttl?: number | ICalMomentDurationStub | null): this | number | null { if (ttl === undefined) { return this.data.ttl; } if (isMomentDuration(ttl)) { this.data.ttl = ttl.asSeconds(); } else if (ttl && ttl > 0) { this.data.ttl = ttl; } else { this.data.ttl = null; } return this; } /** * Creates a new {@link ICalEvent} and returns it. Use options to prefill the event's attributes. * Calling this method without options will create an empty event. * * ```javascript * import ical from 'ical-generator'; * * // or use require: * // const { default: ical } = require('ical-generator'); * * const cal = ical(); * const event = cal.createEvent({summary: 'My Event'}); * * // overwrite event summary * event.summary('Your Event'); * ``` * * @since 0.2.0 */ createEvent(data: ICalEvent | ICalEventData): ICalEvent { const event = data instanceof ICalEvent ? data : new ICalEvent(data, this); this.data.events.push(event); return event; } /** * Returns all events of this calendar. * * ```javascript * const cal = ical(); * * cal.events([ * { * start: new Date(), * end: new Date(new Date().getTime() + 3600000), * summary: 'Example Event', * description: 'It works ;)', * url: 'http://sebbo.net/' * } * ]); * * cal.events(); // --> [ICalEvent] * ``` * * @since 0.2.0 */ events(): ICalEvent[]; /** * Add multiple events to your calendar. * * ```javascript * const cal = ical(); * * cal.events([ * { * start: new Date(), * end: new Date(new Date().getTime() + 3600000), * summary: 'Example Event', * description: 'It works ;)', * url: 'http://sebbo.net/' * } * ]); * * cal.events(); // --> [ICalEvent] * ``` * * @since 0.2.0 */ events(events: (ICalEvent | ICalEventData)[]): this; events(events?: (ICalEvent | ICalEventData)[]): this | ICalEvent[] { if (!events) { return this.data.events; } events.forEach((e: ICalEvent | ICalEventData) => this.createEvent(e)); return this; } /** * Remove all events from the calendar without * touching any other data like name or prodId. * * @since 2.0.0-develop.1 */ clear(): this { this.data.events = []; return this; } /** * Set X-* attributes. Woun't filter double attributes, * which are also added by another method (e.g. busystatus), * so these attributes may be inserted twice. * * ```javascript * calendar.x([ * { * key: "X-MY-CUSTOM-ATTR", * value: "1337!" * } * ]); * * calendar.x([ * ["X-MY-CUSTOM-ATTR", "1337!"] * ]); * * calendar.x({ * "X-MY-CUSTOM-ATTR": "1337!" * }); * ``` * * ```text * BEGIN:VCALENDAR * VERSION:2.0 * PRODID:-//sebbo.net//ical-generator//EN * X-MY-CUSTOM-ATTR:1337! * END:VCALENDAR * ``` * * @since 1.9.0 */ x (keyOrArray: {key: string, value: string}[] | [string, string][] | Record<string, string>): this; /** * Set a X-* attribute. Woun't filter double attributes, * which are also added by another method (e.g. busystatus), * so these attributes may be inserted twice. * * ```javascript * calendar.x("X-MY-CUSTOM-ATTR", "1337!"); * ``` * * ```text * BEGIN:VCALENDAR * VERSION:2.0 * PRODID:-//sebbo.net//ical-generator//EN * X-MY-CUSTOM-ATTR:1337! * END:VCALENDAR * ``` * * @since 1.9.0 */ x (keyOrArray: string, value: string): this; /** * Get all custom X-* attributes. * @since 1.9.0 */ x (): {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); } if(typeof keyOrArray === 'string' && typeof value === 'string') { addOrGetCustomAttributes (this.data, keyOrArray, value); } else if(typeof keyOrArray === 'object') { addOrGetCustomAttributes (this.data, keyOrArray); } else { throw new Error('Either key or value is not a string!'); } return this; } /** * Return a shallow copy of the calendar's options for JSON stringification. * Third party objects like moment.js values or RRule objects are stringified * as well. Can be used for persistence. * * ```javascript * const cal = ical(); * const json = JSON.stringify(cal); * * // later: restore calendar data * cal = ical(JSON.parse(json)); * ``` * * @since 0.2.4 */ toJSON(): ICalCalendarJSONData { return Object.assign({}, this.data, { timezone: this.timezone(), events: this.data.events.map(event => event.toJSON()), x: this.x() }); } /** * Get the number of events added to your calendar */ length(): number { return this.data.events.length; } /** * Return generated calendar as a string. * * ```javascript * const cal = ical(); * console.log(cal.toString()); // → BEGIN:VCALENDAR… * ``` */ toString(): string { let g = ''; // VCALENDAR and VERSION g = 'BEGIN:VCALENDAR\r\nVERSION:2.0\r\n'; // PRODID g += 'PRODID:-' + this.data.prodId + '\r\n'; // URL if (this.data.url) { g += 'URL:' + this.data.url + '\r\n'; } // SOURCE if (this.data.source) { g += 'SOURCE;VALUE=URI:' + this.data.source + '\r\n'; } // CALSCALE if (this.data.scale) { g += 'CALSCALE:' + this.data.scale + '\r\n'; } // METHOD if (this.data.method) { g += 'METHOD:' + this.data.method + '\r\n'; } // NAME if (this.data.name) { g += 'NAME:' + this.data.name + '\r\n'; g += 'X-WR-CALNAME:' + this.data.name + '\r\n'; } // Description if (this.data.description) { g += 'X-WR-CALDESC:' + this.data.description + '\r\n'; } // Timezone if(this.data.timezone?.generator) { const timezones = [...new Set([ this.timezone(), ...this.data.events.map(event => event.timezone()) ])].filter(tz => tz !== null && !tz.startsWith('/')) as string[]; timezones.forEach(tz => { if(!this.data.timezone?.generator) { return; } const s = this.data.timezone.generator(tz); if(!s) { return; } g += s.replace(/\r\n/g, '\n') .replace(/\n/g, '\r\n') .trim() + '\r\n'; }); } if (this.data.timezone?.name) { g += 'TIMEZONE-ID:' + this.data.timezone.name + '\r\n'; g += 'X-WR-TIMEZONE:' + this.data.timezone.name + '\r\n'; } // TTL if (this.data.ttl) { g += 'REFRESH-INTERVAL;VALUE=DURATION:' + toDurationString(this.data.ttl) + '\r\n'; g += 'X-PUBLISHED-TTL:' + toDurationString(this.data.ttl) + '\r\n'; } // Events this.data.events.forEach(event => g += event.toString()); // CUSTOM X ATTRIBUTES g += generateCustomAttributes(this.data); g += 'END:VCALENDAR'; return foldLines(g); } } |