Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

pattern.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.matchAny = exports.convertPatternsToRe = exports.makeRe = exports.getPatternParts = exports.expandBraceExpansion = exports.expandPatternsWithBraceExpansion = exports.isAffectDepthOfReadingPattern = exports.endsWithSlashGlobStar = exports.hasGlobStar = exports.getBaseDirectory = exports.getPositivePatterns = exports.getNegativePatterns = exports.isPositivePattern = exports.isNegativePattern = exports.convertToNegativePattern = exports.convertToPositivePattern = exports.isDynamicPattern = exports.isStaticPattern = void 0;
  4. const path = require("path");
  5. const globParent = require("glob-parent");
  6. const micromatch = require("micromatch");
  7. const picomatch = require("picomatch");
  8. const GLOBSTAR = '**';
  9. const ESCAPE_SYMBOL = '\\';
  10. const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
  11. const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[.*]/;
  12. const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\(.*\|.*\)/;
  13. const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\(.*\)/;
  14. const BRACE_EXPANSIONS_SYMBOLS_RE = /{.*(?:,|\.\.).*}/;
  15. function isStaticPattern(pattern, options = {}) {
  16. return !isDynamicPattern(pattern, options);
  17. }
  18. exports.isStaticPattern = isStaticPattern;
  19. function isDynamicPattern(pattern, options = {}) {
  20. /**
  21. * A special case with an empty string is necessary for matching patterns that start with a forward slash.
  22. * An empty string cannot be a dynamic pattern.
  23. * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
  24. */
  25. if (pattern === '') {
  26. return false;
  27. }
  28. /**
  29. * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
  30. * filepath directly (without read directory).
  31. */
  32. if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
  33. return true;
  34. }
  35. if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
  36. return true;
  37. }
  38. if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
  39. return true;
  40. }
  41. if (options.braceExpansion !== false && BRACE_EXPANSIONS_SYMBOLS_RE.test(pattern)) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. exports.isDynamicPattern = isDynamicPattern;
  47. function convertToPositivePattern(pattern) {
  48. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  49. }
  50. exports.convertToPositivePattern = convertToPositivePattern;
  51. function convertToNegativePattern(pattern) {
  52. return '!' + pattern;
  53. }
  54. exports.convertToNegativePattern = convertToNegativePattern;
  55. function isNegativePattern(pattern) {
  56. return pattern.startsWith('!') && pattern[1] !== '(';
  57. }
  58. exports.isNegativePattern = isNegativePattern;
  59. function isPositivePattern(pattern) {
  60. return !isNegativePattern(pattern);
  61. }
  62. exports.isPositivePattern = isPositivePattern;
  63. function getNegativePatterns(patterns) {
  64. return patterns.filter(isNegativePattern);
  65. }
  66. exports.getNegativePatterns = getNegativePatterns;
  67. function getPositivePatterns(patterns) {
  68. return patterns.filter(isPositivePattern);
  69. }
  70. exports.getPositivePatterns = getPositivePatterns;
  71. function getBaseDirectory(pattern) {
  72. return globParent(pattern, { flipBackslashes: false });
  73. }
  74. exports.getBaseDirectory = getBaseDirectory;
  75. function hasGlobStar(pattern) {
  76. return pattern.includes(GLOBSTAR);
  77. }
  78. exports.hasGlobStar = hasGlobStar;
  79. function endsWithSlashGlobStar(pattern) {
  80. return pattern.endsWith('/' + GLOBSTAR);
  81. }
  82. exports.endsWithSlashGlobStar = endsWithSlashGlobStar;
  83. function isAffectDepthOfReadingPattern(pattern) {
  84. const basename = path.basename(pattern);
  85. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  86. }
  87. exports.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  88. function expandPatternsWithBraceExpansion(patterns) {
  89. return patterns.reduce((collection, pattern) => {
  90. return collection.concat(expandBraceExpansion(pattern));
  91. }, []);
  92. }
  93. exports.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
  94. function expandBraceExpansion(pattern) {
  95. return micromatch.braces(pattern, {
  96. expand: true,
  97. nodupes: true
  98. });
  99. }
  100. exports.expandBraceExpansion = expandBraceExpansion;
  101. function getPatternParts(pattern, options) {
  102. let { parts } = picomatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
  103. /**
  104. * The scan method returns an empty array in some cases.
  105. * See micromatch/picomatch#58 for more details.
  106. */
  107. if (parts.length === 0) {
  108. parts = [pattern];
  109. }
  110. /**
  111. * The scan method does not return an empty part for the pattern with a forward slash.
  112. * This is another part of micromatch/picomatch#58.
  113. */
  114. if (parts[0].startsWith('/')) {
  115. parts[0] = parts[0].slice(1);
  116. parts.unshift('');
  117. }
  118. return parts;
  119. }
  120. exports.getPatternParts = getPatternParts;
  121. function makeRe(pattern, options) {
  122. return micromatch.makeRe(pattern, options);
  123. }
  124. exports.makeRe = makeRe;
  125. function convertPatternsToRe(patterns, options) {
  126. return patterns.map((pattern) => makeRe(pattern, options));
  127. }
  128. exports.convertPatternsToRe = convertPatternsToRe;
  129. function matchAny(entry, patternsRe) {
  130. return patternsRe.some((patternRe) => patternRe.test(entry));
  131. }
  132. exports.matchAny = matchAny;