Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 192x 192x 192x 239x | import { CanActivateChildFn, CanActivateFn, Router } from '@angular/router';
/**
* function to add a functional guard to all routes
*
* @param router The router
* @param guard The functional guard
* @param config An optional configuration to control whether the guard should be added for 'canActivate' or 'canActivateChild' routes (default is for both)
*/
export function addGlobalGuard(
router: Router,
guard: CanActivateFn | CanActivateChildFn,
config: { canActivate: boolean; canActivateChild: boolean } = { canActivate: true, canActivateChild: true }
) {
router.config.forEach(route => {
Iif (config.canActivate) {
if (route.canActivate) {
route.canActivate.push(guard);
} else {
route.canActivate = [guard];
}
}
Iif (config.canActivateChild) {
if (route.canActivateChild) {
route.canActivateChild.push(guard);
} else {
route.canActivateChild = [guard];
}
}
});
}
/**
* RegEx that finds reserved characters that should not be contained in non functional parts of routes/URLs (e.g product slugs for SEO)
*/
// not-dead-code
export const reservedCharactersRegEx = /[ &\(\)=%]/g;
/**
* Sanitize slug data (remove reserved characters, clean up obsolete '-', lower case, capitalize identifiers)
*/
export function sanitizeSlugData(slugData: string) {
return (
slugData
?.replace(reservedCharactersRegEx, '-')
.replace(/-+/g, '-')
.replace(/-+$/, '')
.toLowerCase()
.replaceAll('pg', 'Pg')
.replaceAll('prd', 'Prd')
.replaceAll('ctg', 'Ctg') || ''
);
}
|