variable.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { assignModelProperties } from 'app/core/utils/model_utils';
  2. /*
  3. * This regex matches 3 types of variable reference with an optional format specifier
  4. * \$(\w+) $var1
  5. * \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
  6. * \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
  7. */
  8. export const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?::(\w+))?}/g;
  9. // Helper function since lastIndex is not reset
  10. export const variableRegexExec = (variableString: string) => {
  11. variableRegex.lastIndex = 0;
  12. return variableRegex.exec(variableString);
  13. };
  14. export interface Variable {
  15. setValue(option);
  16. updateOptions();
  17. dependsOn(variable);
  18. setValueFromUrl(urlValue);
  19. getValueForUrl();
  20. getSaveModel();
  21. }
  22. export let variableTypes = {};
  23. export { assignModelProperties };
  24. export function containsVariable(...args: any[]) {
  25. const variableName = args[args.length - 1];
  26. const variableString = args.slice(0, -1).join(' ');
  27. const matches = variableString.match(variableRegex);
  28. const isMatchingVariable =
  29. matches !== null
  30. ? matches.find(match => {
  31. const varMatch = variableRegexExec(match);
  32. return varMatch !== null && varMatch.indexOf(variableName) > -1;
  33. })
  34. : false;
  35. return !!isMatchingVariable;
  36. }