pattern-to-regex.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = pathToPattern;
  6. function _path() {
  7. const data = require("path");
  8. _path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. const sep = `\\${_path().sep}`;
  14. const endSep = `(?:${sep}|$)`;
  15. const substitution = `[^${sep}]+`;
  16. const starPat = `(?:${substitution}${sep})`;
  17. const starPatLast = `(?:${substitution}${endSep})`;
  18. const starStarPat = `${starPat}*?`;
  19. const starStarPatLast = `${starPat}*?${starPatLast}?`;
  20. function escapeRegExp(string) {
  21. return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
  22. }
  23. function pathToPattern(pattern, dirname) {
  24. const parts = _path().resolve(dirname, pattern).split(_path().sep);
  25. return new RegExp(["^", ...parts.map((part, i) => {
  26. const last = i === parts.length - 1;
  27. if (part === "**") return last ? starStarPatLast : starStarPat;
  28. if (part === "*") return last ? starPatLast : starPat;
  29. if (part.indexOf("*.") === 0) {
  30. return substitution + escapeRegExp(part.slice(1)) + (last ? endSep : sep);
  31. }
  32. return escapeRegExp(part) + (last ? endSep : sep);
  33. })].join(""));
  34. }
  35. 0 && 0;
  36. //# sourceMappingURL=pattern-to-regex.js.map