All files category.ts

100% Statements 101/101
100% Branches 11/11
100% Functions 7/7
100% Lines 101/101

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 1021x 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 12x 12x 12x 12x 12x 1x 1x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 4x 4x 17x 17x 13x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x  
'use strict';
 
 
import {escape} from './tools.ts';
 
 
export interface ICalCategoryData {
    name: string;
}
 
 
export interface ICalCategoryJSONData {
    name: string;
}
 
export type ICalCategoryInternalData = ICalCategoryJSONData;
 
 
/**
 * Usually you get an {@link ICalCategory} object like this:
 *
 * ```javascript
 * import ical from 'ical-generator';
 * const calendar = ical();
 * const event = calendar.createEvent();
 * const category = event.createCategory();
 * ```
 *
 * You can also use the {@link ICalCategory} object directly:
 *
 * ```javascript
 * import ical, {ICalCategory} from 'ical-generator';
 * const category = new ICalCategory();
 * event.categories([category]);
 * ```
 */
export default class ICalCategory {
    private readonly data: ICalCategoryInternalData;
 
    /**
     * Constructor of {@link ICalCategory}.
     * @param data Category Data
     */
    constructor(data: ICalCategoryData) {
        this.data = {
            name: ''
        };
 
        if(!data.name) {
            throw new Error('No value for `name` in ICalCategory given!');
        }
 
        this.name(data.name);
    }
 
 
    /**
     * Get the category name
     * @since 0.3.0
     */
    name(): string;
 
    /**
     * Set the category name
     * @since 0.3.0
     */
    name(name: string): this;
    name(name?: string): this | string {
        if (name === undefined) {
            return this.data.name;
        }
 
        this.data.name = name;
        return this;
    }
 
 
    /**
     * Return a shallow copy of the category's options for JSON stringification.
     * Can be used for persistence.
     *
     * @since 0.2.4
     */
    toJSON(): ICalCategoryInternalData {
        return Object.assign({}, this.data);
    }
 
 
    /**
     * Return generated category name as a string.
     *
     * ```javascript
     * console.log(category.toString());
     * ```
     */
    toString(): string {
 
        // CN / Name
        return escape(this.data.name, false);
    }
}