jatg
Version:
Just Another Template Generator
17 lines • 816 B
JavaScript
import { processVariableOperations } from './processVariableOperations.js';
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export function replaceVariables(source, variables, values, context) {
for (const variable of variables) {
const regex = new RegExp('%\\s?' + escapeRegExp(variable.variable) + '(\\.[a-zA-Z.]+)?\\s?%', 'g');
const value = values.get(variable.variable) ?? '';
const globalOperations = variable.preprocessing || [];
source = source.replace(regex, (_, ops) => {
const operations = ops ? ops.substring(1).split('.') : [];
return processVariableOperations(value, [...globalOperations, ...operations], context);
});
}
return source;
}
//# sourceMappingURL=replaceVariables.js.map