简化代码

pull/375/head
王良 2 months ago
parent 8a7c95bb53
commit 9b9c42d3b0

@ -124,12 +124,9 @@ module.exports = (serverConfig) => {
const matchInterceptsOpts = {}
for (const regexp in interceptOpts) { // 遍历拦截配置
// 判断是否匹配拦截器
let matched
if (regexp !== true && regexp !== 'true') {
matched = matchUtil.isMatched(rOptions.path, regexp)
if (matched == null) { // 拦截器匹配失败
continue
}
const matched = matchUtil.isMatched(rOptions.path, regexp)
if (matched == null) { // 拦截器匹配失败
continue
}
// 获取拦截器

@ -1,53 +0,0 @@
// 警告:此文件不再使用,仅用于测试,可在 test/matchUtilTest.js 中比对新逻辑与旧逻辑的效果差异
const lodash = require('lodash')
function isMatched (url, regexp) {
return url.match(regexp)
}
function domainRegexply (target) {
return target.replace(/\./g, '\\.').replace(/\*/g, '.*')
}
function domainMapRegexply (hostMap) {
const regexpMap = {}
if (hostMap == null) {
return regexpMap
}
lodash.each(hostMap, (value, domain) => {
if (domain.indexOf('*') >= 0) {
const regDomain = domainRegexply(domain)
regexpMap[regDomain] = value
} else {
regexpMap[domain] = value
}
})
return regexpMap
}
function matchHostname (hostMap, hostname) {
if (hostMap == null) {
return null
}
const value = hostMap[hostname]
if (value) {
return value
}
if (!value) {
for (const target in hostMap) {
if (target.indexOf('*') < 0) {
continue
}
// 正则表达式匹配
if (hostname.match(target)) {
return hostMap[target]
}
}
}
}
module.exports = {
isMatched,
domainRegexply,
domainMapRegexply,
matchHostname
}
Loading…
Cancel
Save