Skip to content

Commit ac0841f

Browse files
committed
Handle asterisk matches in the compile function (#83)
1 parent ac9fcf9 commit ac0841f

File tree

4 files changed

+187
-83
lines changed

4 files changed

+187
-83
lines changed

Readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ Path-To-RegExp exposes the two functions used internally that accept an array of
207207
* `pathToRegexp.tokensToRegExp(tokens, options)` Transform an array of tokens into a matching regular expression.
208208
* `pathToRegexp.tokensToFunction(tokens)` Transform an array of tokens into a path generator function.
209209

210+
#### Token Information
211+
212+
* `name` The name of the token (`string` for named or `number` for index)
213+
* `prefix` The prefix character for the segment (`/` or `.`)
214+
* `delimiter` The delimiter for the segment (same as prefix or `/`)
215+
* `optional` Indicates the token is optional (`boolean`)
216+
* `repeat` Indicates the token is repeated (`boolean`)
217+
* `partial` Indicates this token is a partial path segment (`boolean`)
218+
* `pattern` The RegExp used to match this token (`string`)
219+
* `asterisk` Indicates the token is an `*` match (`boolean`)
220+
210221
## Compatibility with Express <= 4.x
211222

212223
Path-To-RegExp breaks compatibility with Express <= `4.x`:

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ declare namespace pathToRegexp {
4141
prefix: string;
4242
delimiter: string;
4343
optional: boolean;
44-
partial: boolean;
4544
repeat: boolean;
4645
pattern: string;
46+
partial: boolean;
47+
asterisk: boolean;
4748
}
4849

4950
interface PathFunctionOptions {

index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
116117
function 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

Comments
 (0)