import { get, has } from 'lodash-es'; /** * Match simple variables, like `$foo` */ const simpleVariableRegex = /(?!\\\$)\$(\w+)/g; /** * Match wrapped variables, like `${foo.bar}` */ const wrappedVariableRegex = /(?!\\\$)\${([^}]+)}/g; const processVariable = ( data: Record, _matchedText: string, varName: string, ): string => { if (has(data, varName)) { return `${get(data, varName)}`; } throw new Error( `Invalid template variable: ${JSON.stringify( varName, )}. Please use \\$ if this is not a variable.`, ); }; const processTemplate = (template: string | null, data: Record): string => { if (!template) { return ''; } if (!template.includes('$')) { return template; } // Replace any vars (excluding `\$`) with their data const replacerFn = processVariable.bind(null, data); return template .replace(simpleVariableRegex, replacerFn) .replace(wrappedVariableRegex, replacerFn); }; export { processTemplate };