]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: add a clearRoutes method
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 19 Jun 2024 13:54:03 +0000 (15:54 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 19 Jun 2024 13:56:05 +0000 (15:56 +0200)
packages/router/__tests__/matcher/addingRemoving.spec.ts
packages/router/src/matcher/index.ts
packages/router/src/router.ts

index 5c48b867644881627345af3eb66a449ec6fa48bc..85ef37888bb93600c9b84bd7f222ee7dccc69480 100644 (file)
@@ -16,6 +16,21 @@ describe('Matcher: adding and removing records', () => {
     })
   })
 
+  it('can remove all records', () => {
+    const matcher = createRouterMatcher([], {})
+    matcher.addRoute({ path: '/', component })
+    matcher.addRoute({ path: '/about', component, name: 'about' })
+    matcher.addRoute({
+      path: '/with-children',
+      component,
+      children: [{ path: 'child', component }],
+    })
+    expect(matcher.getRoutes()).not.toHaveLength(0)
+    matcher.clearRoutes()
+    expect(matcher.getRoutes()).toHaveLength(0)
+    expect(matcher.getRecordMatcher('about')).toBeFalsy()
+  })
+
   it('throws when adding *', () => {
     const matcher = createRouterMatcher([], {})
     expect(() => {
index fc88dacadef0d0ac4590ce821cbb7ff6015c8e81..0d67061d391086d5632edd5c4cb0887b102b3a9e 100644 (file)
@@ -27,10 +27,9 @@ import type { RouteRecordNameGeneric, _RouteRecordProps } from '../typed-routes'
  */
 export interface RouterMatcher {
   addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void
-
   removeRoute(matcher: RouteRecordMatcher): void
   removeRoute(name: NonNullable<RouteRecordNameGeneric>): void
-
+  clearRoutes: () => void
   getRoutes: () => RouteRecordMatcher[]
   getRecordMatcher: (
     name: NonNullable<RouteRecordNameGeneric>
@@ -345,7 +344,19 @@ export function createRouterMatcher(
   // add initial routes
   routes.forEach(route => addRoute(route))
 
-  return { addRoute, resolve, removeRoute, getRoutes, getRecordMatcher }
+  function clearRoutes() {
+    matchers.length = 0
+    matcherMap.clear()
+  }
+
+  return {
+    addRoute,
+    resolve,
+    removeRoute,
+    clearRoutes,
+    getRoutes,
+    getRecordMatcher,
+  }
 }
 
 function paramsFromLocation(
index 20df891ab882005a43d3d832c3e7b27eeb450420..da33e976d70b5192cf2bf82afc988d226bd0cbe3 100644 (file)
@@ -239,6 +239,11 @@ export interface Router {
    */
   getRoutes(): RouteRecord[]
 
+  /**
+   * Delete all routes from the router matcher.
+   */
+  clearRoutes(): void
+
   /**
    * Returns the {@link RouteLocation | normalized version} of a
    * {@link RouteLocationRaw | route location}. Also includes an `href` property
@@ -1228,6 +1233,7 @@ export function createRouter(options: RouterOptions): Router {
 
     addRoute,
     removeRoute,
+    clearRoutes: matcher.clearRoutes,
     hasRoute,
     getRoutes,
     resolve,