UNPKG

@itsmworkbench/utils

Version:

The usual utility functions

32 lines (31 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractVariableNames = exports.findUsedVariables = void 0; const reverse_template_1 = require("./reverse.template"); function findUsedVariables(inputString, variables) { const usedVariables = []; for (const varName in variables) { const varValue = variables[varName]; // Escape potential regex special characters in the variable value const escapedVarValue = (0, reverse_template_1.escapeRegExp)(varValue); // Create a regex to find the variable value in the input string const regex = new RegExp(escapedVarValue); // If the variable value is found in the input string, add the variable name to the usedVariables array if (regex.test(inputString)) { usedVariables.push(varName); } } return usedVariables; } exports.findUsedVariables = findUsedVariables; function extractVariableNames(template) { // Using a stricter regex that matches complete and correct braces around the names const pattern = /\{([^\{\}]+)\}/g; // Only matches well-formed brace pairs let match; const variableNames = []; while ((match = pattern.exec(template)) !== null) { variableNames.push(match[1].trim()); } return variableNames; } exports.extractVariableNames = extractVariableNames;