All files / src/variables/types base.ts

94.73% Statements 18/19
100% Branches 14/14
80% Functions 4/5
94.73% Lines 18/19

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  1x   1x   1x           40x 40x               4x                               20x       100x 33x 67x 11x 11x 11x 1x 1x 1x   1x         66x      
import { DateAndTimeUtils } from "@templates/utils/dateAndTime";
import { encode } from "html-entities";
 
export class InvalidDefinitionError extends Error {}
 
export class CustomVariable {
    static definitionName: string;
    protected label: string;
    protected name: string;
 
    constructor(name: string, label: string) {
        this.name = name;
        this.label = label;
    }
 
    protected inputHTML(): string {
        return "";
    }
 
    public toHTML(): string {
        return (
            `
            <div class="variableInput">
                <div class="variableName">
                    ${encode(this.label)}
                </div>
                <div>
                    ${this.inputHTML()}
                </div>
            </div>
            `
        );
    }
 
    // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
    public processInput(input: string, dateAndTimeUtils: DateAndTimeUtils): any {
        return input;
    }
 
    static createFromDefinition(name: string, definition: unknown): CustomVariable {
        if (typeof definition === "string" && definition.trim() === this.definitionName) {
            return new this(name, name);
        } else if (typeof definition === "object" && definition !== null) {
            if ("type" in definition) {
                const variableType = definition["type"];
                if (typeof variableType === "string" && variableType.trim() === this.definitionName) {
                    let label = name;
                    if ("label" in definition && typeof definition["label"] === "string") {
                        label = definition["label"].trim();
                    }
                    return new this(name, label);
                }
            }
        }
 
        throw new InvalidDefinitionError();
    }
}