Skip to content

Commit ac9fcf9

Browse files
committed
Handle partial path token matches/compiles (#82)
1 parent 27d8e89 commit ac9fcf9

File tree

4 files changed

+154
-36
lines changed

4 files changed

+154
-36
lines changed

Readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ re.exec('/test/route')
5757

5858
**Please note:** Named parameters must be made up of "word characters" (`[A-Za-z0-9_]`).
5959

60-
Path segments are defined by "prefix" characters (`.` or `/`). If a prefix is used, and the parameter is followed by the same prefix character or end of the path, it is considered a segment and the prefix is part of the match. This behavior is apparent when using optional parameters.
61-
6260
```js
63-
var re = pathToRegexp('/:prefix(apple-)?icon-:res(\\d+).png', keys)
64-
// keys = [{ name: 'prefix', prefix: '', ... }, { name: 'res', prefix: '', ... }]
61+
var re = pathToRegexp('/(apple-)?icon-:res(\\d+).png', keys)
62+
// keys = [{ name: 0, prefix: '/', ... }, { name: 'res', prefix: '', ... }]
6563

6664
re.exec('/icon-76.png')
6765
//=> ['/icon-76.png', undefined, '76']

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ declare namespace pathToRegexp {
4141
prefix: string;
4242
delimiter: string;
4343
optional: boolean;
44+
partial: boolean;
4445
repeat: boolean;
4546
pattern: string;
4647
}
@@ -54,4 +55,4 @@ declare namespace pathToRegexp {
5455
export type PathFunction = (data?: Object, options?: PathFunctionOptions) => string;
5556
}
5657

57-
export = pathToRegexp;
58+
export = pathToRegexp;

index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,13 @@ function parse (str) {
6161
var modifier = res[6]
6262
var asterisk = res[7]
6363

64-
// Only use the prefix when followed by another path segment.
65-
if (prefix != null && next != null && next !== prefix) {
66-
path += prefix
67-
prefix = null
68-
}
69-
7064
// Push the current path onto the tokens.
7165
if (path) {
7266
tokens.push(path)
7367
path = ''
7468
}
7569

70+
var partial = prefix != null && next != null && next !== prefix
7671
var repeat = modifier === '+' || modifier === '*'
7772
var optional = modifier === '?' || modifier === '*'
7873
var delimiter = res[2] || '/'
@@ -84,6 +79,7 @@ function parse (str) {
8479
delimiter: delimiter,
8580
optional: optional,
8681
repeat: repeat,
82+
partial: partial,
8783
pattern: escapeGroup(pattern)
8884
})
8985
}
@@ -157,6 +153,11 @@ function tokensToFunction (tokens) {
157153

158154
if (value == null) {
159155
if (token.optional) {
156+
// Prepend partial segment prefixes.
157+
if (token.partial) {
158+
path += token.prefix
159+
}
160+
160161
continue
161162
} else {
162163
throw new TypeError('Expected "' + token.name + '" to be defined')
@@ -262,6 +263,7 @@ function regexpToRegexp (path, keys) {
262263
prefix: null,
263264
delimiter: null,
264265
optional: false,
266+
partial: false,
265267
repeat: false,
266268
pattern: null
267269
})
@@ -344,10 +346,10 @@ function tokensToRegExp (tokens, options) {
344346
}
345347

346348
if (token.optional) {
347-
if (prefix) {
349+
if (!token.partial) {
348350
capture = '(?:' + prefix + '(' + capture + '))?'
349351
} else {
350-
capture = '(' + capture + ')?'
352+
capture = prefix + '(' + capture + ')?'
351353
}
352354
} else {
353355
capture = prefix + '(' + capture + ')'

0 commit comments

Comments
 (0)