]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(parser): handle strict and sensitive options
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 15 Dec 2019 19:01:26 +0000 (20:01 +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 5b8d3a0fe2a983922cf1c68721d330f0f9dbf39f..2b6cd33b1ae401a78b9b579bc2ece9d95cce8079 100644 (file)
@@ -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', () => {
index 5b03b74a208d6953930173ac378f67edf0af4577..ac5ce4c354481699dd9ffe3e0e716a60f6931938 100644 (file)
@@ -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 += '/?'