-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom pathToRegexpModule (#11)
- Loading branch information
Showing
4 changed files
with
52 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,54 @@ | ||
'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; | ||
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); | ||
const pathToRegexpModule = options.pathToRegexpModule || require('path-to-regexp'); | ||
const pathToRegexp = pathToRegexpModule.pathToRegexp || pathToRegexpModule; | ||
const matchFn = options.match ? | ||
toPathMatch(options.match, pathToRegexp) : toPathMatch(options.ignore, pathToRegexp); | ||
|
||
return function pathMatch(ctx) { | ||
const matched = matchFn(ctx); | ||
return options.match ? matched : !matched; | ||
}; | ||
}; | ||
|
||
function toPathMatch(pattern) { | ||
function toPathMatch(pattern, pathToRegexp) { | ||
if (typeof pattern === 'string') { | ||
const reg = pathToRegexp(pattern, [], { end: false }); | ||
if (reg.global) reg.lastIndex = 0; | ||
let reg = pathToRegexp(pattern, [], { end: false }); | ||
if (reg.regexp) { | ||
// support path-to-regexp@8 | ||
// => const { regexp, keys } = pathToRegexp("/foo/:bar"); | ||
reg = reg.regexp; | ||
} | ||
if (reg.global) { | ||
reg.lastIndex = 0; | ||
} | ||
return ctx => reg.test(ctx.path); | ||
} | ||
|
||
if (pattern instanceof RegExp) { | ||
return ctx => { | ||
if (pattern.global) pattern.lastIndex = 0; | ||
if (pattern.global) { | ||
pattern.lastIndex = 0; | ||
} | ||
return pattern.test(ctx.path); | ||
}; | ||
} | ||
if (typeof pattern === 'function') return pattern; | ||
|
||
if (typeof pattern === 'function') { | ||
return pattern; | ||
} | ||
|
||
if (Array.isArray(pattern)) { | ||
const matchs = pattern.map(item => toPathMatch(item)); | ||
const matchs = pattern.map(item => toPathMatch(item, pathToRegexp)); | ||
return ctx => matchs.some(match => match(ctx)); | ||
} | ||
|
||
throw new Error('match/ignore pattern must be RegExp, Array or String, but got ' + pattern); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters