matcher.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __values = (this && this.__values) || function(o) {
  14. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  15. if (m) return m.call(o);
  16. if (o && typeof o.length === "number") return {
  17. next: function () {
  18. if (o && i >= o.length) o = void 0;
  19. return { value: o && o[i++], done: !o };
  20. }
  21. };
  22. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  23. };
  24. var __read = (this && this.__read) || function (o, n) {
  25. var m = typeof Symbol === "function" && o[Symbol.iterator];
  26. if (!m) return o;
  27. var i = m.call(o), r, ar = [], e;
  28. try {
  29. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  30. }
  31. catch (error) { e = { error: error }; }
  32. finally {
  33. try {
  34. if (r && !r.done && (m = i["return"])) m.call(i);
  35. }
  36. finally { if (e) throw e.error; }
  37. }
  38. return ar;
  39. };
  40. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  41. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  42. if (ar || !(i in from)) {
  43. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  44. ar[i] = from[i];
  45. }
  46. }
  47. return to.concat(ar || Array.prototype.slice.call(from));
  48. };
  49. exports.__esModule = true;
  50. exports.isMatch = exports.matcher = exports.escapeStringRegexp = void 0;
  51. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  52. var escapeStringRegexp = function (text) {
  53. if (typeof text !== 'string') {
  54. throw new TypeError('Expected a string');
  55. }
  56. // Escape characters with special meaning either inside or outside character sets.
  57. // Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
  58. return text === null || text === void 0 ? void 0 : text.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
  59. };
  60. exports.escapeStringRegexp = escapeStringRegexp;
  61. var regexpCache = new Map();
  62. var sanitizeArray = function (input, inputName) {
  63. if (!Array.isArray(input)) {
  64. switch (typeof input) {
  65. case 'string':
  66. input = [input];
  67. break;
  68. case 'undefined':
  69. input = [];
  70. break;
  71. default:
  72. throw new TypeError("Expected '".concat(inputName, "' to be a string or an array, but got a type of '").concat(typeof input, "'"));
  73. }
  74. }
  75. return input.filter(function (string) {
  76. if (typeof string !== 'string') {
  77. if (typeof string === 'undefined') {
  78. return false;
  79. }
  80. throw new TypeError("Expected '".concat(inputName, "' to be an array of strings, but found a type of '").concat(typeof string, "' in the array"));
  81. }
  82. return true;
  83. });
  84. };
  85. var makeRegexp = function (pattern, options) {
  86. if (options === void 0) { options = {}; }
  87. options = __assign({ caseSensitive: false }, options);
  88. var cacheKey = pattern + JSON.stringify(options);
  89. if (regexpCache.has(cacheKey)) {
  90. return regexpCache.get(cacheKey);
  91. }
  92. var negated = pattern[0] === '!';
  93. if (negated) {
  94. pattern = pattern.slice(1);
  95. }
  96. pattern = (0, exports.escapeStringRegexp)(pattern).replace(/\\\*/g, '[\\s\\S]*');
  97. var regexp = new RegExp("^".concat(pattern, "$"), options.caseSensitive ? '' : 'i');
  98. regexp.negated = negated;
  99. regexpCache.set(cacheKey, regexp);
  100. return regexp;
  101. };
  102. var baseMatcher = function (inputs, patterns, options, firstMatchOnly) {
  103. var e_1, _a, e_2, _b;
  104. if (options === void 0) { options = {}; }
  105. if (firstMatchOnly === void 0) { firstMatchOnly = false; }
  106. inputs = sanitizeArray(inputs, 'inputs');
  107. patterns = sanitizeArray(patterns, 'patterns');
  108. if (patterns.length === 0) {
  109. return [];
  110. }
  111. patterns = patterns.map(function (pattern) { return makeRegexp(pattern, options); });
  112. var allPatterns = (options || {}).allPatterns;
  113. var result = [];
  114. try {
  115. for (var inputs_1 = __values(inputs), inputs_1_1 = inputs_1.next(); !inputs_1_1.done; inputs_1_1 = inputs_1.next()) {
  116. var input = inputs_1_1.value;
  117. // String is included only if it matches at least one non-negated pattern supplied.
  118. // Note: the `allPatterns` option requires every non-negated pattern to be matched once.
  119. // Matching a negated pattern excludes the string.
  120. var matches = void 0;
  121. var didFit = __spreadArray([], __read(patterns), false).fill(false);
  122. try {
  123. for (var _c = (e_2 = void 0, __values(patterns.entries())), _d = _c.next(); !_d.done; _d = _c.next()) {
  124. var _e = __read(_d.value, 2), index = _e[0], pattern = _e[1];
  125. if (pattern.test(input)) {
  126. didFit[index] = true;
  127. matches = !pattern.negated;
  128. if (!matches) {
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  135. finally {
  136. try {
  137. if (_d && !_d.done && (_b = _c["return"])) _b.call(_c);
  138. }
  139. finally { if (e_2) throw e_2.error; }
  140. }
  141. if (!(matches === false ||
  142. (matches === undefined &&
  143. patterns.some(function (pattern) { return !pattern.negated; })) ||
  144. (allPatterns &&
  145. didFit.some(function (yes, index) { return !yes && !patterns[index].negated; })))) {
  146. result.push(input);
  147. if (firstMatchOnly) {
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  154. finally {
  155. try {
  156. if (inputs_1_1 && !inputs_1_1.done && (_a = inputs_1["return"])) _a.call(inputs_1);
  157. }
  158. finally { if (e_1) throw e_1.error; }
  159. }
  160. return result;
  161. };
  162. var matcher = function (inputs, patterns, options) {
  163. if (options === void 0) { options = {}; }
  164. return baseMatcher(inputs, patterns, options, false);
  165. };
  166. exports.matcher = matcher;
  167. var isMatch = function (inputs, patterns, options) {
  168. if (options === void 0) { options = {}; }
  169. return baseMatcher(inputs, patterns, options, true).length > 0;
  170. };
  171. exports.isMatch = isMatch;