From: Eduardo San Martin Morote Date: Sun, 15 Dec 2019 19:01:26 +0000 (+0100) Subject: feat(parser): handle strict and sensitive options X-Git-Tag: v4.0.0-alpha.0~140 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=493d2a135bea52f2fc021faf4b164ce798b7c36b;p=thirdparty%2Fvuejs%2Frouter.git feat(parser): handle strict and sensitive options --- diff --git a/__tests__/matcher/path-ranking.spec.ts b/__tests__/matcher/path-ranking.spec.ts index 5b8d3a0f..2b6cd33b 100644 --- a/__tests__/matcher/path-ranking.spec.ts +++ b/__tests__/matcher/path-ranking.spec.ts @@ -96,6 +96,9 @@ describe('Path ranking', () => { '/a/:b/c', '/a/:b', '/a', + '/:a/:b', + '/:w', + '/:w+', // '/:a/-:b', // '/:a/:b', // '/a-:b', @@ -116,16 +119,42 @@ describe('Path ranking', () => { }) }) + it('sensitive should go before non sensitive', () => { + checkPathOrder([ + ['/Home', { sensitive: true }], + ['/home', {}], + ]) + checkPathOrder([ + ['/:w', { sensitive: true }], + ['/:w', {}], + ]) + }) + + it('strict should go before non strict', () => { + checkPathOrder([ + ['/home', { strict: true }], + ['/home', {}], + ]) + }) + it('orders repeteable and optional', () => { - checkPathOrder(['/:w', '/:w+']) + possibleOptions.forEach(options => { + checkPathOrder(['/:w', ['/:w?', options]]) + checkPathOrder(['/:w?', ['/:w+', options]]) + checkPathOrder(['/:w?', ['/:w*', options]]) + }) }) it('orders static before params', () => { - checkPathOrder(['/a', '/:id']) + possibleOptions.forEach(options => { + checkPathOrder(['/a', ['/:id', options]]) + }) }) it('empty path before slash', () => { - checkPathOrder(['', '/']) + possibleOptions.forEach(options => { + checkPathOrder(['', ['/', options]]) + }) }) it('works with long paths', () => { @@ -149,9 +178,8 @@ describe('Path ranking', () => { '/a/b', ]) - // does this really make sense? - // checkPathOrder([['/a/', { strict: true }], '/a/']) - // checkPathOrder([['/a', { strict: true }], '/a']) + checkPathOrder([['/a/', { strict: true }], '/a/']) + checkPathOrder([['/a', { strict: true }], '/a']) }) it('puts the wildcard at the end', () => { diff --git a/src/matcher/tokenizer.ts b/src/matcher/tokenizer.ts index 5b03b74a..ac5ce4c3 100644 --- a/src/matcher/tokenizer.ts +++ b/src/matcher/tokenizer.ts @@ -297,6 +297,8 @@ export function tokensToParser( : PathScore.Segment : PathScore.Root + if (options.sensitive) segmentScore += PathScore.BonusCaseSensitive + for (let tokenIndex = 0; tokenIndex < segment.length; tokenIndex++) { const token = segment[tokenIndex] if (token.type === TokenType.Static) { @@ -343,6 +345,9 @@ export function tokensToParser( score.push(segmentScore) } + // only apply the strict bonus to the last score + if (options.strict) score[score.length - 1] += PathScore.BonusStrict + // TODO: warn double trailing slash if (!options.strict) pattern += '/?'