]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(parser): handle optional and repeatable
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 15 Dec 2019 18:49:16 +0000 (19:49 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Dec 2019 09:26:15 +0000 (10:26 +0100)
__tests__/matcher/path-ranking.spec.ts
src/matcher/tokenizer.ts

index 4442036659a52504f2d627a3f4b867df5cc5508e..5b8d3a0fe2a983922cf1c68721d330f0f9dbf39f 100644 (file)
@@ -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'])
   })
index ccc3164fc807fa3654874a5b751896af102fc553..5b03b74a208d6953930173ac378f67edf0af4577 100644 (file)
@@ -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
       }
     }