@@ -80,6 +80,7 @@ function parse (str) {
8080 optional : optional ,
8181 repeat : repeat ,
8282 partial : partial ,
83+ asterisk : ! ! asterisk ,
8384 pattern : escapeGroup ( pattern )
8485 } )
8586 }
@@ -108,13 +109,25 @@ function compile (str) {
108109}
109110
110111/**
111- * Encode characters for segment that could cause trouble for parsing .
112+ * Prettier encoding of URI path segments .
112113 *
113114 * @param {string }
114115 * @return {string }
115116 */
116117function encodeURIComponentPretty ( str ) {
117- return encodeURI ( str ) . replace ( / [ / ? # ' " ] / g, function ( c ) {
118+ return encodeURI ( str ) . replace ( / [ \/ ? # ] / g, function ( c ) {
119+ return '%' + c . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( )
120+ } )
121+ }
122+
123+ /**
124+ * Encode the asterisk parameter. Similar to `pretty`, but allows slashes.
125+ *
126+ * @param {string }
127+ * @return {string }
128+ */
129+ function encodeAsterisk ( str ) {
130+ return encodeURI ( str ) . replace ( / [ ? # ] / g, function ( c ) {
118131 return '%' + c . charCodeAt ( 0 ) . toString ( 16 ) . toUpperCase ( )
119132 } )
120133}
@@ -166,7 +179,7 @@ function tokensToFunction (tokens) {
166179
167180 if ( isarray ( value ) ) {
168181 if ( ! token . repeat ) {
169- throw new TypeError ( 'Expected "' + token . name + '" to not repeat, but received " ' + value + '" ' )
182+ throw new TypeError ( 'Expected "' + token . name + '" to not repeat, but received ` ' + JSON . stringify ( value ) + '` ' )
170183 }
171184
172185 if ( value . length === 0 ) {
@@ -181,7 +194,7 @@ function tokensToFunction (tokens) {
181194 segment = encode ( value [ j ] )
182195
183196 if ( ! matches [ i ] . test ( segment ) ) {
184- throw new TypeError ( 'Expected all "' + token . name + '" to match "' + token . pattern + '", but received " ' + segment + '" ' )
197+ throw new TypeError ( 'Expected all "' + token . name + '" to match "' + token . pattern + '", but received ` ' + JSON . stringify ( segment ) + '` ' )
185198 }
186199
187200 path += ( j === 0 ? token . prefix : token . delimiter ) + segment
@@ -190,7 +203,7 @@ function tokensToFunction (tokens) {
190203 continue
191204 }
192205
193- segment = encode ( value )
206+ segment = token . asterisk ? encodeAsterisk ( value ) : encode ( value )
194207
195208 if ( ! matches [ i ] . test ( segment ) ) {
196209 throw new TypeError ( 'Expected "' + token . name + '" to match "' + token . pattern + '", but received "' + segment + '"' )
@@ -263,8 +276,9 @@ function regexpToRegexp (path, keys) {
263276 prefix : null ,
264277 delimiter : null ,
265278 optional : false ,
266- partial : false ,
267279 repeat : false ,
280+ partial : false ,
281+ asterisk : false ,
268282 pattern : null
269283 } )
270284 }
0 commit comments