All files / node-sdk/models/bxml Root.ts

100% Statements 20/20
87.5% Branches 7/8
100% Functions 6/6
100% Lines 18/18

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 6013x       13x               13x                   10x 10x 4x 2x   2x     6x             13x 10x 12x 10x             13x 4x             13x 10x   13x  
import { create } from 'xmlbuilder2';
import { XMLBuilder, XMLWriterOptions } from 'xmlbuilder2/lib/interfaces';
import { Verb } from './Verb';
 
export const SSML_REGEX = /<([a-zA-Z\/\/].*?)>/g;
 
/**
 * @export
 * @class Root
 * @extends {Verb}
 * Root class for BXML and Response Tags
 */
export class Root {
    name: string;
    nestedVerbs?: Verb[];
 
    /**
     * Creates an instance of Root
     * @param name [string] Name of the XML element
     * @param nestedVerbs [Verb | Array<Verb>] Nested Verb or Array of Nested Verbs
     */
    constructor(name: string, nestedVerbs?: Verb | Verb[]) {
        this.name = name;
        if (nestedVerbs) {
            if (nestedVerbs instanceof Array) {
                this.nestedVerbs = nestedVerbs;
            } else {
                this.nestedVerbs = [nestedVerbs];
            }
        } else {
            this.nestedVerbs = [];
        }
    }
 
    /**
     * Generate an XML element for the verb
     */
    generateXml(): XMLBuilder {
        const xml = create({ version: '1.0', encoding: 'UTF-8' }).ele(this.name);
        this.nestedVerbs?.forEach((verb) => { xml.import(verb.generateXml()); });
        return xml;
    }
 
    /**
     * Add a verb or verbs to the root element
     * @param {Verb | Verb[]} verbs The verb or verbs to add
     */
    addVerbs(verbs: Verb | Verb[]): void {
        this.nestedVerbs = this.nestedVerbs.concat(verbs);
    }
 
    /**
     * Return BXML representation of this element
     * @param options XML Serialization options
     */
    toBxml(options?: XMLWriterOptions): string {
        return this.generateXml().end(options).replace(SSML_REGEX, '<$1>');
    }
}