All files / src/app/core/utils routing.ts

30.76% Statements 4/13
22.22% Branches 2/9
33.33% Functions 1/3
30.76% Lines 4/13

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                  194x                                                     194x         194x 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') || ''
  );
}