forked from pillarjs/path-match
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (30 loc) · 855 Bytes
/
index.js
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
37
38
var pathToRegexp = require('path-to-regexp')
var createError = require('http-errors')
module.exports = function (options) {
options = options || {}
return function (path) {
var keys = []
var re = pathToRegexp(path, keys, options)
return function (pathname, params) {
var m = re.exec(pathname)
if (!m) return false
params = params || {}
var key, param
for (var i = 0; i < keys.length; i++) {
key = keys[i]
param = m[i + 1]
if (!param) continue
params[key.name] = decodeParam(param)
if (key.repeat) params[key.name] = params[key.name].split(key.delimiter)
}
return params
}
}
}
function decodeParam (param) {
try {
return decodeURIComponent(param)
} catch (_) {
throw createError(400, 'failed to decode param "' + param + '"')
}
}