/** * Replace placeholders in a template string with values from a data object. * Supports nested JSON data using dot notation (e.g., {{user.name}}). * * @param template - The template string containing placeholders in {{key}} format * @param data - The data object with values to replace placeholders * @returns The processed template with placeholders replaced * * @example * // Simple replacement * replaceTemplatePlaceholders("Hello {{name}}", { name: "World" }); * // Returns: "Hello World" * * @example * // Nested data replacement * replaceTemplatePlaceholders( * "{{user.name}} is {{user.age}} years old", * { user: { name: "Alice", age: 30 } } * ); * // Returns: "Alice is 30 years old" */ export function replaceTemplatePlaceholders( template: string, data: Record ): string { // Replace placeholders with support for nested paths (e.g., {{user.name}}) return template.replace(/\{\{([^}]+)\}\}/g, (match, path) => { // Trim whitespace from the path const trimmedPath = path.trim(); // Split the path by dots for nested access const keys = trimmedPath.split('.'); // Traverse the data object to get the value let value: any = data; for (const key of keys) { if (value && typeof value === 'object' && key in value) { value = value[key]; } else { // If path doesn't exist, return original placeholder return match; } } // Convert value to string, handling various types if (value === null || value === undefined) { return match; // Return original placeholder if value is null/undefined } // Handle arrays and objects by converting to JSON if (typeof value === 'object') { return JSON.stringify(value); } return String(value); }); } /** * Extract all placeholder keys from a template string * * @param template - The template string to analyze * @returns Array of unique placeholder keys found in the template * * @example * extractPlaceholders("Hello {{name}}, you have {{count}} messages"); * // Returns: ["name", "count"] */ export function extractPlaceholders(template: string): string[] { const placeholders = new Set(); const regex = /\{\{([^}]+)\}\}/g; let match; while ((match = regex.exec(template)) !== null) { placeholders.add(match[1].trim()); } return Array.from(placeholders); } /** * Validate that all required placeholders are present in the data object * * @param template - The template string to validate * @param data - The data object to check against * @returns Object with isValid flag and array of missing placeholders * * @example * validatePlaceholders("Hello {{name}}", { name: "World" }); * // Returns: { isValid: true, missing: [] } * * validatePlaceholders("Hello {{name}}", {}); * // Returns: { isValid: false, missing: ["name"] } */ export function validatePlaceholders( template: string, data: Record ): { isValid: boolean; missing: string[] } { const placeholders = extractPlaceholders(template); const missing: string[] = []; for (const placeholder of placeholders) { const keys = placeholder.split('.'); let value: any = data; for (const key of keys) { if (value && typeof value === 'object' && key in value) { value = value[key]; } else { missing.push(placeholder); break; } } // Check if final value is null/undefined if (value === null || value === undefined) { if (!missing.includes(placeholder)) { missing.push(placeholder); } } } return { isValid: missing.length === 0, missing }; }