pattern.js 4.4 KB

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