ContentLine.ts 2.97 KB
Newer Older
Mark Stenglein's avatar
Mark Stenglein committed
1 2
/*
 * lib-ical
Mark Stenglein's avatar
Mark Stenglein committed
3
 * Copyright (C) 2017 Mark Stenglein
Mark Stenglein's avatar
Mark Stenglein committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18
import { isAlpha } from "./util";
19
import { ICalElement } from "./ICalElement";
Mark Stenglein's avatar
Mark Stenglein committed
20
import Parameter from "./Parameter";
21
const CRLF: string = "\r\n";
Mark Stenglein's avatar
Mark Stenglein committed
22 23 24 25 26 27 28 29 30 31

/**
 * Implementation of a Content Line from RFC 5545
 *
 * Chapter 3.1 defines the content line. This implementation will use it as an
 * abstract class which can be extended later by the specific content types.
 *
 * This class defines all of the general features specified by the chapter,
 * while leaving the specific features required by individual component objects
 * unfilled.
Mark Stenglein's avatar
Mark Stenglein committed
32 33 34
 *
 * @author Mark Stenglein <mark@stengle.in>
 * @since 0.1.0
Mark Stenglein's avatar
Mark Stenglein committed
35
 */
36
export default class ContentLine implements ICalElement {
Mark Stenglein's avatar
Mark Stenglein committed
37
    private _name: string;
38
    private _params: Parameter[];
Mark Stenglein's avatar
Mark Stenglein committed
39 40
    private _value: string;

Mark Stenglein's avatar
Mark Stenglein committed
41
    constructor(inName: string, inParams: Parameter[], inValue: string) {
Mark Stenglein's avatar
Mark Stenglein committed
42
        this.name = inName;
43
        this._params = inParams;
Mark Stenglein's avatar
Mark Stenglein committed
44 45 46 47 48 49 50 51 52
        this.value = inValue;
    }

    /* Getters */

    get name(): string {
        return this._name;
    }

Mark Stenglein's avatar
Mark Stenglein committed
53
    get params(): Parameter[] {
54
        return this._params;
Mark Stenglein's avatar
Mark Stenglein committed
55 56 57 58 59 60 61 62 63
    }

    get value(): string {
        return this._value;
    }

    /* Setters */

    set name(newName: string) {
64
        if (Parameter.isIanaToken(newName)) {
Mark Stenglein's avatar
Mark Stenglein committed
65 66 67
            this._name = newName;
        }
        else {
68
            throw new TypeError("'name' must be alphabetic!");
Mark Stenglein's avatar
Mark Stenglein committed
69 70 71 72 73 74 75
        }
    }

    set value(newValue: string) {
        if (isAlpha(newValue)) {
            this._value = newValue;
        }
76 77 78
        else {
            throw new TypeError("'value' must be alphabetic!");
        }
Mark Stenglein's avatar
Mark Stenglein committed
79
    }
80 81 82

    /**
     * Folds lines into 74 octet sections
Mark Stenglein's avatar
Mark Stenglein committed
83
     *
84
     * @author Sebastian Pekarek <mail@sebbo.net>
Mark Stenglein's avatar
Mark Stenglein committed
85
     *
86 87 88
     * TODO: Make sure that this handles multi-octed UTF-8 segments properly.
     */
    static fold(line: string): string {
Mark Stenglein's avatar
Mark Stenglein committed
89
        return line.match(/(.{1,74})/g).join(CRLF + " ");
90 91 92 93
    }

    /**
     * Generates a folded content line to use to create the final file.
Mark Stenglein's avatar
Mark Stenglein committed
94
     *
95 96 97 98 99
     * @author Mark Stenglein <mark@stengle.in>
     */
    public generate(): string {
        let outputLine = this.name;

100
        this.params.forEach((param) => {
101
            outputLine += ";";
102
            outputLine += param.generate();
103 104
        });

Mark Stenglein's avatar
Mark Stenglein committed
105
        outputLine += ":" + this.value + CRLF;
106 107 108

        return ContentLine.fold(outputLine);
    }
109
}