333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
 
const pathToRegexp = require('path-to-regexp');
 
module.exports = function(options) {
  options = options || {};
  if (options.match && options.ignore) throw new Error('options.match and options.ignore can not both present');
  if (!options.match && !options.ignore) return () => true;
 
  const matchFn = options.match ? toPathMatch(options.match) : toPathMatch(options.ignore);
 
  return function pathMatch(ctx) {
    const matched = matchFn(ctx);
    return options.match ? matched : !matched;
  };
};
 
function toPathMatch(pattern) {
  if (typeof pattern === 'string') {
    const reg = pathToRegexp(pattern, [], { end: false });
    if (reg.global) reg.lastIndex = 0;
    return ctx => reg.test(ctx.path);
  }
  if (pattern instanceof RegExp) {
    return ctx => {
      if (pattern.global) pattern.lastIndex = 0;
      return pattern.test(ctx.path);
    };
  }
  if (typeof pattern === 'function') return pattern;
  if (Array.isArray(pattern)) {
    const matchs = pattern.map(item => toPathMatch(item));
    return ctx => matchs.some(match => match(ctx));
  }
  throw new Error('match/ignore pattern must be RegExp, Array or String, but got ' + pattern);
}