feat: add utility function for getting a route path

pull/4377/head
Arman Ozak 5 years ago
parent 6478f57317
commit 41f1f2740e

@ -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<DummyComponent>;
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);
});
});
});

@ -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('/');
}

Loading…
Cancel
Save