From: Eduardo San Martin Morote Date: Sun, 15 Dec 2019 18:49:16 +0000 (+0100) Subject: feat(parser): handle optional and repeatable X-Git-Tag: v4.0.0-alpha.0~141 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=309237a6e6a7c3e5e8c49f4764e4b9fd7df3ad41;p=thirdparty%2Fvuejs%2Frouter.git feat(parser): handle optional and repeatable --- diff --git a/__tests__/matcher/path-ranking.spec.ts b/__tests__/matcher/path-ranking.spec.ts index 44420366..5b8d3a0f 100644 --- a/__tests__/matcher/path-ranking.spec.ts +++ b/__tests__/matcher/path-ranking.spec.ts @@ -89,6 +89,37 @@ describe('Path ranking', () => { } } + it('works', () => { + checkPathOrder([ + '/a/b/c', + '/a/b', + '/a/:b/c', + '/a/:b', + '/a', + // '/:a/-:b', + // '/:a/:b', + // '/a-:b', + // '/a-:w(.*)', + // '/:a-b', + // '/:a-:b-:c', + // '/:a-:b', + // '/:a-:b(.*)', + ]) + }) + + it('puts the slash before optional paramateres', () => { + possibleOptions.forEach(options => { + checkPathOrder(['/', ['/:a?', options]]) + checkPathOrder(['/', ['/:a*', options]]) + checkPathOrder(['/', ['/:a(\\d+)?', options]]) + checkPathOrder(['/', ['/:a(\\d+)*', options]]) + }) + }) + + it('orders repeteable and optional', () => { + checkPathOrder(['/:w', '/:w+']) + }) + it('orders static before params', () => { checkPathOrder(['/a', '/:id']) }) diff --git a/src/matcher/tokenizer.ts b/src/matcher/tokenizer.ts index ccc3164f..5b03b74a 100644 --- a/src/matcher/tokenizer.ts +++ b/src/matcher/tokenizer.ts @@ -258,9 +258,9 @@ const enum PathScore { Static = 3 * _multiplier, // /static Dynamic = 2 * _multiplier, // /:someId BonusCustomRegExp = 1 * _multiplier, // /:someId(\\d+) - BonusWildcard = -3 * _multiplier, // /:namedWildcard(.*) - BonusRepeatable = -0.5 * _multiplier, // /:w+ or /:w* - BonusOptional = -4 * _multiplier, // /:w? or /:w* + BonusWildcard = -4 * _multiplier - BonusCustomRegExp, // /:namedWildcard(.*) we remove the bonus added by the custom regexp + BonusRepeatable = -2 * _multiplier, // /:w+ or /:w* + BonusOptional = -1 * _multiplier, // /:w? or /:w* // these two have to be under 0.1 so a strict /:page is still lower than /:a-:b BonusStrict = 0.07 * _multiplier, // when options strict: true is passed, as the regex omits \/? BonusCaseSensitive = 0.025 * _multiplier, // when options strict: true is passed, as the regex omits \/? @@ -334,6 +334,8 @@ export function tokensToParser( pattern += subPattern segmentScore += PathScore.Dynamic + if (token.optional) segmentScore += PathScore.BonusOptional + if (token.repeatable) segmentScore += PathScore.BonusRepeatable if (re === '.*') segmentScore += PathScore.BonusWildcard } }