@@ -2,21 +2,23 @@ const path = require('path')
2
2
const fs = require ( 'fs' )
3
3
const qs = require ( 'querystring' )
4
4
5
- const paramPattern = / : ( [ ^ / ] + ) /
5
+
6
+ const paramPattern = / (?: : | % ) ( [ ^ \/ ] + ) /
7
+
6
8
// takes routes and decorates them with a 'match' method that will return { params, query } if a path matches
7
9
function addMatch ( route ) {
8
10
let routePath = route . path
9
11
let paramNames = [ ]
10
12
let matched
11
- // find any paths prefixed with a `: `, and treat them as capture groups
13
+ // find any paths prefixed with `:` or `% `, and treat them as capture groups
12
14
while ( ( matched = routePath . match ( paramPattern ) ) !== null ) {
13
15
routePath = routePath . replace ( paramPattern , '([^?/]+)' )
14
16
paramNames . push ( matched [ 1 ] )
15
17
}
16
18
// if a route ends with `index`, allow matching that route without matching the `index` part
17
19
if ( path . basename ( routePath ) === 'index' ) {
18
20
route . isIndex = true
19
- routePath = routePath . replace ( / \/ i n d e x $ / , '/?(: ?index)?' )
21
+ routePath = routePath . replace ( / \/ i n d e x $ / , '/?([:%] ?index)?' )
20
22
}
21
23
// create a regex with our path
22
24
let pattern = new RegExp ( `^${ routePath } (\\?(.*)|$)` , 'i' )
@@ -45,7 +47,9 @@ function findRoutes (dir) {
45
47
}
46
48
47
49
const val = v => ( typeof v === 'undefined' ? 0 : v )
50
+
48
51
module . exports = function router ( routesDir , config ) {
52
+
49
53
const routes = findRoutes ( routesDir )
50
54
// if filter function is set, filter routes
51
55
. filter ( config && config . filter || function ( ) { return true } )
@@ -55,9 +59,11 @@ module.exports = function router (routesDir, config) {
55
59
let route = require ( routeFile )
56
60
let extPattern = new RegExp ( path . extname ( routeFile ) + '$' )
57
61
if ( ! route . path ) {
62
+
58
63
route . path = '/' + path . relative ( routesDir , routeFile ) . replace ( extPattern , '' )
59
64
//Fix issue with windows paths
60
65
route . path = route . path . replace ( / \\ / , '/' )
66
+
61
67
}
62
68
return route
63
69
} )
0 commit comments