pattern.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var path = require("path");
  4. var globParent = require("glob-parent");
  5. var isGlob = require("is-glob");
  6. var micromatch = require("micromatch");
  7. var GLOBSTAR = '**';
  8. /**
  9. * Return true for static pattern.
  10. */
  11. function isStaticPattern(pattern) {
  12. return !isDynamicPattern(pattern);
  13. }
  14. exports.isStaticPattern = isStaticPattern;
  15. /**
  16. * Return true for pattern that looks like glob.
  17. */
  18. function isDynamicPattern(pattern) {
  19. return isGlob(pattern, { strict: false });
  20. }
  21. exports.isDynamicPattern = isDynamicPattern;
  22. /**
  23. * Convert a windows «path» to a unix-style «path».
  24. */
  25. function unixifyPattern(pattern) {
  26. return pattern.replace(/\\/g, '/');
  27. }
  28. exports.unixifyPattern = unixifyPattern;
  29. /**
  30. * Returns negative pattern as positive pattern.
  31. */
  32. function convertToPositivePattern(pattern) {
  33. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  34. }
  35. exports.convertToPositivePattern = convertToPositivePattern;
  36. /**
  37. * Returns positive pattern as negative pattern.
  38. */
  39. function convertToNegativePattern(pattern) {
  40. return '!' + pattern;
  41. }
  42. exports.convertToNegativePattern = convertToNegativePattern;
  43. /**
  44. * Return true if provided pattern is negative pattern.
  45. */
  46. function isNegativePattern(pattern) {
  47. return pattern.startsWith('!') && pattern[1] !== '(';
  48. }
  49. exports.isNegativePattern = isNegativePattern;
  50. /**
  51. * Return true if provided pattern is positive pattern.
  52. */
  53. function isPositivePattern(pattern) {
  54. return !isNegativePattern(pattern);
  55. }
  56. exports.isPositivePattern = isPositivePattern;
  57. /**
  58. * Extracts negative patterns from array of patterns.
  59. */
  60. function getNegativePatterns(patterns) {
  61. return patterns.filter(isNegativePattern);
  62. }
  63. exports.getNegativePatterns = getNegativePatterns;
  64. /**
  65. * Extracts positive patterns from array of patterns.
  66. */
  67. function getPositivePatterns(patterns) {
  68. return patterns.filter(isPositivePattern);
  69. }
  70. exports.getPositivePatterns = getPositivePatterns;
  71. /**
  72. * Extract base directory from provided pattern.
  73. */
  74. function getBaseDirectory(pattern) {
  75. return globParent(pattern);
  76. }
  77. exports.getBaseDirectory = getBaseDirectory;
  78. /**
  79. * Return true if provided pattern has globstar.
  80. */
  81. function hasGlobStar(pattern) {
  82. return pattern.indexOf(GLOBSTAR) !== -1;
  83. }
  84. exports.hasGlobStar = hasGlobStar;
  85. /**
  86. * Return true if provided pattern ends with slash and globstar.
  87. */
  88. function endsWithSlashGlobStar(pattern) {
  89. return pattern.endsWith('/' + GLOBSTAR);
  90. }
  91. exports.endsWithSlashGlobStar = endsWithSlashGlobStar;
  92. /**
  93. * Returns «true» when pattern ends with a slash and globstar or the last partial of the pattern is static pattern.
  94. */
  95. function isAffectDepthOfReadingPattern(pattern) {
  96. var basename = path.basename(pattern);
  97. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  98. }
  99. exports.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  100. /**
  101. * Return naive depth of provided pattern without depth of the base directory.
  102. */
  103. function getNaiveDepth(pattern) {
  104. var base = getBaseDirectory(pattern);
  105. var patternDepth = pattern.split('/').length;
  106. var patternBaseDepth = base.split('/').length;
  107. /**
  108. * This is a hack for pattern that has no base directory.
  109. *
  110. * This is related to the `*\something\*` pattern.
  111. */
  112. if (base === '.') {
  113. return patternDepth - patternBaseDepth;
  114. }
  115. return patternDepth - patternBaseDepth - 1;
  116. }
  117. exports.getNaiveDepth = getNaiveDepth;
  118. /**
  119. * Return max naive depth of provided patterns without depth of the base directory.
  120. */
  121. function getMaxNaivePatternsDepth(patterns) {
  122. return patterns.reduce(function (max, pattern) {
  123. var depth = getNaiveDepth(pattern);
  124. return depth > max ? depth : max;
  125. }, 0);
  126. }
  127. exports.getMaxNaivePatternsDepth = getMaxNaivePatternsDepth;
  128. /**
  129. * Make RegExp for provided pattern.
  130. */
  131. function makeRe(pattern, options) {
  132. return micromatch.makeRe(pattern, options);
  133. }
  134. exports.makeRe = makeRe;
  135. /**
  136. * Convert patterns to regexps.
  137. */
  138. function convertPatternsToRe(patterns, options) {
  139. return patterns.map(function (pattern) { return makeRe(pattern, options); });
  140. }
  141. exports.convertPatternsToRe = convertPatternsToRe;
  142. /**
  143. * Returns true if the entry match any of the given RegExp's.
  144. */
  145. function matchAny(entry, patternsRe) {
  146. return patternsRe.some(function (patternRe) { return patternRe.test(entry); });
  147. }
  148. exports.matchAny = matchAny;