Skip to content

Instantly share code, notes, and snippets.

@crlspe
Created March 3, 2023 12:55
Show Gist options
  • Save crlspe/e05b3b5321e53e47b51511e91a4e9d63 to your computer and use it in GitHub Desktop.
Save crlspe/e05b3b5321e53e47b51511e91a4e9d63 to your computer and use it in GitHub Desktop.
const variablePattern = /\${([^}]*)}/g;
module.exports = {
format: (normalStringTemplate, values) => {
const replacedStr = normalStringTemplate.replace(variablePattern, (match, p1) => values[p1.trim()]);
const templateStr = `${replacedStr}`;
return templateStr;
},
listVariables: (normalStringTemplate) => {
const pattern = /\${([^}]*)}/g;
const matches = normalStringTemplate.match(pattern);
if (matches) {
return matches.map(match => match.slice(2, -1).trim());
} else {
return [];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment