templating.ts 390 B

123456789101112131415
  1. import { Environment } from "~/newstore/environments"
  2. export default function parseTemplateString(
  3. str: string,
  4. variables: Environment["variables"]
  5. ) {
  6. if (!variables || !str) {
  7. return str
  8. }
  9. const searchTerm = /<<([^>]*)>>/g // "<<myVariable>>"
  10. return decodeURI(encodeURI(str)).replace(
  11. searchTerm,
  12. (_, p1) => variables.find((x) => x.key === p1)?.value || ""
  13. )
  14. }