summaryrefslogtreecommitdiffstats
path: root/src/templatedString.ts
blob: c397ad9290ee399dbca6a8b1dde57b7498675fa4 (plain)
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
import { getEnvVars } from "./util";

/**
 * Represent a templated variable in string
 */
export interface ITemplate {
    toReplace: string;  // The template to be replaced in string
    with: string;       // The value to replace with
}

export function getIndentedTemplate(replace: string): string {
    if (replace === "") {
        return "";
    }
    const snippets = replace.split(/({indent:\d+})/);

    let indentedString: string = "";
    let indentWidth: number = 0;

    // tslint:disable-next-line:prefer-for-of
    snippets.forEach((element) => {
        if (element.match(/{indent:\d+}/)) {
            const indents = parseInt(element.match(/{indent:(\d+)}/)[1], 10);
            indentWidth = indents;
            const numSpaces = Math.max(indentWidth - indentedString.length, 0);
            indentedString += " ".repeat(numSpaces);
        } else {
            // just some text
            indentedString += element;
        }
    });

    return indentedString;
}

/**
 * Expand variable template in the string
 * @param original the original string
 * @param template variable template to be expanded
 * @returns new string with expanded template
 */
export function getTemplatedString(original: string, template: ITemplate): string {
    const replacedTemplate = original.replace(template.toReplace, template.with);
    const replacedWithEnv = getEnvVars(replacedTemplate);
    return getIndentedTemplate(replacedWithEnv);
}

/**
 * Generate lines of doxygen comments from template
 * @param lines Arrays to store lines of comments
 * @param replace Variable template to be expanded
 * @param template Original string that contains variable templates
 * @param templateWith Arrays of values to replace in the original template string
 */
export function generateFromTemplate(
    lines: string[],
    replace: string,
    template: string,
    templateWith: string[],
) {
    templateWith.forEach((element: string) => {
        // Ignore null values
        if (element !== null) {
            lines.push(...getTemplatedString(template, { toReplace: replace, with: element }).split("\n"));
        }
    });
}

/**
 * Expand multiple variable templates in the string
 * @param original string containing multiple variables to be expanded
 * @param templates variable templates to be expanded
 * @returns new string with expanded templates
 */
export function getMultiTemplatedString(
    original: string,
    templates: ITemplate[],
): string {
    // For each replace entry, attempt to replace it with the corresponding param in the template
    for (const template of templates) {
        original = original.replace(template.toReplace, template.with);
    }
    return getEnvVars(original);
}