All files / src/utils template.ts

100% Statements 11/11
100% Branches 2/2
100% Functions 1/1
100% Lines 11/11

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                                      1x 211x 211x 211x 211x     211x   1605x 1605x 1605x   211x 211x  
/**
 * Template variable replacement utilities
 */
 
/**
 * Replace template variables in content using mustache-style syntax {{variableName}}
 *
 * @param content - The content string containing template variables
 * @param variables - Object containing variable names and their replacement values
 * @returns Content with all template variables replaced
 *
 * @example
 * ```typescript
 * const content = "Hello {{name}}, version {{version}}!";
 * const variables = { name: "World", version: "1.0.0" };
 * const result = replaceTemplateVariables(content, variables);
 * // Result: "Hello World, version 1.0.0!"
 * ```
 */
export function replaceTemplateVariables(
    content: string,
    variables: Record<string, string>
): string {
    let result = content;
 
    // Iterate through all variables and replace them
    for (const [key, value] of Object.entries(variables)) {
        // Create regex pattern for {{variableName}} with global flag
        const pattern = new RegExp(`\\{\\{${key}\\}\\}`, 'g');
        result = result.replace(pattern, value);
    }
 
    return result;
}