Created
March 3, 2023 12:55
-
-
Save crlspe/e05b3b5321e53e47b51511e91a4e9d63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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