640a253c1a24c85a4edad64e58b3dad97df5866f91215e1697b7e9b7b366615972c204658b29b74cd66406f256633464b0a0c8ad1999ee5142ce0e42d17345 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var pathUtils = require("../../utils/path");
  4. var patternUtils = require("../../utils/pattern");
  5. var EntryFilter = /** @class */ (function () {
  6. function EntryFilter(options, micromatchOptions) {
  7. this.options = options;
  8. this.micromatchOptions = micromatchOptions;
  9. this.index = new Map();
  10. }
  11. /**
  12. * Returns filter for directories.
  13. */
  14. EntryFilter.prototype.getFilter = function (positive, negative) {
  15. var _this = this;
  16. var positiveRe = patternUtils.convertPatternsToRe(positive, this.micromatchOptions);
  17. var negativeRe = patternUtils.convertPatternsToRe(negative, this.micromatchOptions);
  18. return function (entry) { return _this.filter(entry, positiveRe, negativeRe); };
  19. };
  20. /**
  21. * Returns true if entry must be added to result.
  22. */
  23. EntryFilter.prototype.filter = function (entry, positiveRe, negativeRe) {
  24. // Exclude duplicate results
  25. if (this.options.unique) {
  26. if (this.isDuplicateEntry(entry)) {
  27. return false;
  28. }
  29. this.createIndexRecord(entry);
  30. }
  31. // Filter files and directories by options
  32. if (this.onlyFileFilter(entry) || this.onlyDirectoryFilter(entry)) {
  33. return false;
  34. }
  35. if (this.isSkippedByAbsoluteNegativePatterns(entry, negativeRe)) {
  36. return false;
  37. }
  38. return this.isMatchToPatterns(entry.path, positiveRe) && !this.isMatchToPatterns(entry.path, negativeRe);
  39. };
  40. /**
  41. * Return true if the entry already has in the cross reader index.
  42. */
  43. EntryFilter.prototype.isDuplicateEntry = function (entry) {
  44. return this.index.has(entry.path);
  45. };
  46. /**
  47. * Create record in the cross reader index.
  48. */
  49. EntryFilter.prototype.createIndexRecord = function (entry) {
  50. this.index.set(entry.path, undefined);
  51. };
  52. /**
  53. * Returns true for non-files if the «onlyFiles» option is enabled.
  54. */
  55. EntryFilter.prototype.onlyFileFilter = function (entry) {
  56. return this.options.onlyFiles && !entry.isFile();
  57. };
  58. /**
  59. * Returns true for non-directories if the «onlyDirectories» option is enabled.
  60. */
  61. EntryFilter.prototype.onlyDirectoryFilter = function (entry) {
  62. return this.options.onlyDirectories && !entry.isDirectory();
  63. };
  64. /**
  65. * Return true when `absolute` option is enabled and matched to the negative patterns.
  66. */
  67. EntryFilter.prototype.isSkippedByAbsoluteNegativePatterns = function (entry, negativeRe) {
  68. if (!this.options.absolute) {
  69. return false;
  70. }
  71. var fullpath = pathUtils.makeAbsolute(this.options.cwd, entry.path);
  72. return this.isMatchToPatterns(fullpath, negativeRe);
  73. };
  74. /**
  75. * Return true when entry match to provided patterns.
  76. *
  77. * First, just trying to apply patterns to the path.
  78. * Second, trying to apply patterns to the path with final slash (need to micromatch to support «directory/**» patterns).
  79. */
  80. EntryFilter.prototype.isMatchToPatterns = function (filepath, patternsRe) {
  81. return patternUtils.matchAny(filepath, patternsRe) || patternUtils.matchAny(filepath + '/', patternsRe);
  82. };
  83. return EntryFilter;
  84. }());
  85. exports.default = EntryFilter;