From 41f1f2740e074ff840679431d7f58653a97d4aee Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 17 Jun 2020 18:43:30 +0300 Subject: [PATCH] feat: add utility function for getting a route path --- .../core/src/lib/tests/route-utils.spec.ts | 60 +++++++++++++ .../core/src/lib/utils/route-utils.ts | 87 ++----------------- 2 files changed, 65 insertions(+), 82 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/route-utils.spec.ts diff --git a/npm/ng-packs/packages/core/src/lib/tests/route-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/route-utils.spec.ts new file mode 100644 index 0000000000..9c5f81ac2a --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/route-utils.spec.ts @@ -0,0 +1,60 @@ +import { Component } from '@angular/core'; +import { RouterModule } from '@angular/router'; +import { createRoutingFactory, SpectatorRouting } from '@ngneat/spectator/jest'; +import { RouterOutletComponent } from '../components'; +import { getRoutePath } from '../utils/route-utils'; + +// tslint:disable-next-line +@Component({ template: '' }) +class DummyComponent {} + +describe('Route Utils', () => { + describe('#getRoutePath', () => { + let spectator: SpectatorRouting; + const createRouting = createRoutingFactory({ + component: RouterOutletComponent, + stubsEnabled: false, + declarations: [DummyComponent], + imports: [RouterModule], + routes: [ + { + path: '', + children: [ + { + path: 'foo', + children: [ + { + path: 'bar', + children: [ + { + path: 'baz', + component: DummyComponent, + }, + ], + }, + ], + }, + ], + }, + ], + }); + + beforeEach(async () => { + spectator = createRouting(); + }); + + test.each` + url | expected + ${''} | ${'/'} + ${'/'} | ${'/'} + ${'/foo'} | ${'/foo'} + ${'/foo/bar'} | ${'/foo/bar'} + ${'/foo/bar/baz'} | ${'/foo/bar/baz'} + ${'/foo?bar=baz'} | ${'/foo'} + ${'/foo#bar'} | ${'/foo'} + `('should return $expected when url is $url', async ({ url, expected }) => { + await spectator.router.navigateByUrl(url); + expect(getRoutePath(spectator.router)).toBe(expected); + }); + }); +}); diff --git a/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts index 51abe4218b..7ef062986a 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts @@ -1,85 +1,8 @@ -import { ABP } from '../models/common'; +import { PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router'; -export function organizeRoutes( - routes: ABP.FullRoute[], - wrappers: ABP.FullRoute[] = [], - parentNameArr = [] as ABP.FullRoute[], - parentName: string = null, -): ABP.FullRoute[] { - const filter = route => { - if (route.children && route.children.length) { - route.children = organizeRoutes(route.children, wrappers, parentNameArr, route.name); - } +export function getRoutePath(router: Router) { + const emptyGroup = { segments: [] } as UrlSegmentGroup; + const primaryGroup = router.parseUrl(router.url).root.children[PRIMARY_OUTLET]; - if (route.parentName && route.parentName !== parentName) { - parentNameArr.push(route); - return false; - } - - return true; - }; - - if (parentName) { - // recursive block - return routes.filter(filter); - } - - const filteredRoutes = routes.filter(filter); - - if (parentNameArr.length) { - return sortRoutes(setChildRoute([...filteredRoutes, ...wrappers], parentNameArr)); - } - - return filteredRoutes; -} - -export function setChildRoute( - routes: ABP.FullRoute[], - parentNameArr: ABP.FullRoute[], -): ABP.FullRoute[] { - return routes.map(route => { - if (route.children && route.children.length) { - route.children = setChildRoute(route.children, parentNameArr); - } - - const foundedChildren = parentNameArr.filter(parent => parent.parentName === route.name); - if (foundedChildren && foundedChildren.length) { - route.children = [...(route.children || []), ...foundedChildren]; - } - - return route; - }); -} - -export function sortRoutes(routes: ABP.FullRoute[] = []): ABP.FullRoute[] { - if (!routes.length) return []; - return routes - .map((route, index) => { - return { - ...route, - order: typeof route.order === 'undefined' ? index + 1 : route.order, - }; - }) - .sort((a, b) => a.order - b.order) - .map(route => { - if (route.children && route.children.length) { - route.children = sortRoutes(route.children); - } - - return route; - }); -} - -const ABP_ROUTES = [] as ABP.FullRoute[]; - -export function addAbpRoutes(routes: ABP.FullRoute | ABP.FullRoute[]): void { - if (!Array.isArray(routes)) { - routes = [routes]; - } - - ABP_ROUTES.push(...routes); -} - -export function getAbpRoutes(): ABP.FullRoute[] { - return ABP_ROUTES; + return '/' + (primaryGroup || emptyGroup).segments.map(({ path }) => path).join('/'); }