Selaa lähdekoodia

Fix the `is` and `not` checking when route is not yet defined

Shaun 2 vuotta sitten
vanhempi
commit
f5f1d0b932
4 muutettua tiedostoa jossa 33 lisäystä ja 21 poistoa
  1. 1 1
      package.json
  2. 3 3
      src/index.js
  3. 16 4
      src/router.js
  4. 13 13
      tests/router.test.js

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "@shaun/alpinejs-router",
-  "version": "1.2.3",
+  "version": "1.2.4",
   "description": "Easy to use and flexible router for Alpine.js",
   "type": "module",
   "main": "dist/module.cjs.js",

+ 3 - 3
src/index.js

@@ -44,10 +44,10 @@ export default function (Alpine) {
     },
 
     is (...paths) {
-      return router.is(paths)
+      return router.is(getTargetURL(state.href), ...paths)
     },
     not (...paths) {
-      return router.not(paths)
+      return router.not(getTargetURL(state.href), ...paths)
     },
     get notfound () {
       return router.notfound(getTargetURL(state.href))
@@ -172,7 +172,7 @@ export default function (Alpine) {
     Alpine.nextTick(() => {
       effect(() => {
         const target = getTargetURL(state.href)
-        const found = modifiers.includes('notfound') ? router.notfound(target) : router.is(expression)
+        const found = modifiers.includes('notfound') ? router.notfound(target) : router.is(target, expression)
         found ? show() : hide()
       })
     })

+ 16 - 4
src/router.js

@@ -24,12 +24,24 @@ export class Router {
     return false
   }
 
-  is (...routes) {
-    return routes.indexOf(this.#current) > -1
+  is (target, ...routes) {
+    for (const route of routes) {
+      const pattern = this.#patterns[route] ?? URLPattern.build(route)
+      if (URLPattern.is(target.path, pattern)) {
+        return true
+      }
+    }
+    return false
   }
 
-  not (...routes) {
-    return routes.indexOf(this.#current) === -1
+  not (target, ...routes) {
+    for (const route of routes) {
+      const pattern = this.#patterns[route] ?? URLPattern.build(route)
+      if (URLPattern.is(target.path, pattern)) {
+        return false
+      }
+    }
+    return true
   }
 
   notfound (target) {

+ 13 - 13
tests/router.test.js

@@ -18,19 +18,19 @@ describe('router', () => {
     r.add('/hello')
     r.add('/users/add')
     r.add('/users/:id')
-    r.match(new RouterURL('http:/localhost/hello'))
-    expect(r.is('/hello')).toBe(true)
-    expect(r.is('/xyz')).toBe(false)
-    expect(r.is('/xyz', '/hello')).toBe(true)
-    expect(r.not('/hello')).toBe(false)
-    expect(r.not('/xyz')).toBe(true)
-    expect(r.not('/xyz', '/hello')).toBe(false)
-    r.match(new RouterURL('http:/localhost/users/add'))
-    expect(r.is('/users/add')).toBe(true)
-    expect(r.not('/users/:id')).toBe(true)
-    r.match(new RouterURL('http:/localhost/users/123'))
-    expect(r.not('/users/add')).toBe(true)
-    expect(r.is('/users/:id')).toBe(true)
+    let url = new RouterURL('http:/localhost/hello')
+    expect(r.is(url, '/hello')).toBe(true)
+    expect(r.is(url, '/xyz')).toBe(false)
+    expect(r.is(url, '/xyz', '/hello')).toBe(true)
+    expect(r.not(url, '/hello')).toBe(false)
+    expect(r.not(url, '/xyz')).toBe(true)
+    expect(r.not(url, '/xyz', '/hello')).toBe(false)
+    url = new RouterURL('http:/localhost/users/add')
+    expect(r.is(url, '/users/add')).toBe(true)
+    expect(r.not(url, '/users/:id(\\d+)')).toBe(true)
+    url = new RouterURL('http:/localhost/users/123')
+    expect(r.not(url, '/users/add')).toBe(true)
+    expect(r.is(url, '/users/:id(\\d+)')).toBe(true)
     expect(r.notfound(new RouterURL('http:/localhost/hello/world'))).toBe(true)
     expect(r.notfound(new RouterURL('http:/localhost/hello'))).toBe(false)
   })