diff --git a/index.js b/index.js index d6c45f9..1f3d982 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,7 @@ function tokensToFunction (tokens) { if (token.optional) { // Prepend partial segment prefixes. - if (token.partial) path += token.prefix + if (i === 0 && token.partial) path += token.prefix continue } @@ -323,7 +323,7 @@ function tokensToRegExp (tokens, keys, options) { if (keys) keys.push(token) if (token.optional) { - if (token.partial) { + if (i === 0 && token.partial) { route += escapeString(token.prefix) + '(' + capture + ')?' } else { route += '(?:' + escapeString(token.prefix) + '(' + capture + '))?' diff --git a/test.ts b/test.ts index 6cee055..4c8aab7 100644 --- a/test.ts +++ b/test.ts @@ -857,6 +857,40 @@ var TESTS: Test[] = [ [{ test: 'foo' }, '/foo-bar'] ] ], + [ + '/:required/:optional?-ext', + null, + [ + { + name: 'required', + prefix: '/', + delimiter: '/', + optional: false, + repeat: false, + partial: false, + pattern: '[^\\/]+?' + }, + { + name: 'optional', + prefix: '/', + delimiter: '/', + optional: true, + repeat: false, + partial: true, + pattern: '[^\\/]+?' + }, + '-ext' + ], + [ + ['/foo-ext', ['/foo-ext', 'foo', undefined]], + ['/foo/bar-ext', ['/foo/bar-ext', 'foo', 'bar']], + ['/foo/-ext', null] + ], + [ + [{ required: 'foo' }, '/foo-ext'], + [{ required: 'foo', optional: 'baz' }, '/foo/baz-ext'] + ] + ], [ '/:test*-bar', null,