3
3
*/
4
4
module . exports = pathtoRegexp ;
5
5
6
+ /**
7
+ * The main path matching regexp utility.
8
+ *
9
+ * @type {RegExp }
10
+ */
6
11
var PATH_REGEXP = new RegExp ( [
7
12
// Match already escaped characters that would otherwise incorrectly appear
8
13
// in future matches. This allows the user to escape special characters that
@@ -28,6 +33,19 @@ function escapeGroup (group) {
28
33
return group . replace ( / ( [ = ! : $ \/ ( ) ] ) / g, '\\$1' ) ;
29
34
}
30
35
36
+ /**
37
+ * Attach the keys as a property of the regexp.
38
+ *
39
+ * @param {RegExp } re
40
+ * @param {Array } keys
41
+ * @return {RegExp }
42
+ */
43
+ var attachKeys = function ( re , keys ) {
44
+ re . keys = keys ;
45
+
46
+ return re ;
47
+ } ;
48
+
31
49
/**
32
50
* Normalize the given path string, returning a regular expression.
33
51
*
@@ -63,7 +81,7 @@ function pathtoRegexp (path, keys, options) {
63
81
} ) ) ;
64
82
65
83
// Return the source back to the user.
66
- return path ;
84
+ return attachKeys ( path , keys ) ;
67
85
}
68
86
69
87
if ( Array . isArray ( path ) ) {
@@ -75,7 +93,7 @@ function pathtoRegexp (path, keys, options) {
75
93
} ) ;
76
94
77
95
// Generate a new regexp instance by joining all the parts together.
78
- return new RegExp ( '(?:' + path . join ( '|' ) + ')' , flags ) ;
96
+ return attachKeys ( new RegExp ( '(?:' + path . join ( '|' ) + ')' , flags ) , keys ) ;
79
97
}
80
98
81
99
// Alter the path string into a usable regexp.
@@ -140,5 +158,5 @@ function pathtoRegexp (path, keys, options) {
140
158
path += strict && endsWithSlash ? '' : '(?=\\/|$)' ;
141
159
}
142
160
143
- return new RegExp ( '^' + path + ( end ? '$' : '' ) , flags ) ;
161
+ return attachKeys ( new RegExp ( '^' + path + ( end ? '$' : '' ) , flags ) , keys ) ;
144
162
} ;
0 commit comments