Source

helpers/MatrixGlob.ts

  1. import * as globToRegexp from "glob-to-regexp";
  2. /**
  3. * Represents a common Matrix glob. This is commonly used
  4. * for server ACLs and similar functions.
  5. * @category Utilities
  6. */
  7. export class MatrixGlob {
  8. /**
  9. * The regular expression which represents this glob.
  10. */
  11. public readonly regex: RegExp;
  12. /**
  13. * Creates a new Matrix Glob
  14. * @param {string} glob The glob to convert. Eg: "*.example.org"
  15. */
  16. constructor(glob: string) {
  17. const globRegex = globToRegexp(glob, {
  18. extended: false,
  19. globstar: false,
  20. });
  21. // We need to convert `?` manually because globToRegexp's extended mode
  22. // does more than we want it to.
  23. const replaced = globRegex.toString().replace(/\\\?/g, ".");
  24. this.regex = new RegExp(replaced.substring(1, replaced.length - 1));
  25. }
  26. /**
  27. * Tests the glob against a value, returning true if it matches.
  28. * @param {string} val The value to test.
  29. * @returns {boolean} True if the value matches the glob, false otherwise.
  30. */
  31. public test(val: string): boolean {
  32. return this.regex.test(val);
  33. }
  34. }