UNPKG

901 BJavaScriptView Raw
1'use strict'
2
3const { $mappingMethod } = require('./symbols')
4
5const methods = [
6 function (request, url) {
7 return this.match.exec(url)
8 },
9 function ({ method }, url) {
10 return this[$mappingMethod].includes(method) && this.match.exec(url)
11 },
12 function (request, url) {
13 return !this.match.exec(url)
14 },
15 function ({ method }, url) {
16 return !this[$mappingMethod].includes(method) || !this.match.exec(url)
17 }
18]
19
20module.exports = function (mapping) {
21 let index = 0
22 if (mapping[$mappingMethod]) {
23 index += 1
24 }
25 if (mapping['invert-match']) {
26 index += 2
27 }
28 const matchMethod = methods[index]
29 const ifMatch = mapping['if-match']
30 if (ifMatch) {
31 return function (request, url) {
32 const match = matchMethod.call(this, request, url)
33 if (match) {
34 return ifMatch(request, url, match)
35 }
36 return false
37 }
38 }
39 return matchMethod
40}