All files / src/app/core/routing/category category.route.ts

100% Statements 33/33
100% Branches 11/11
100% Functions 9/9
100% Lines 32/32

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91186x   186x         186x 186x               186x 146x       186x               186x   18x 2x       28x     16x   12x 12x 12x 12x   12x         4x                 186x 65x 2x       63x 96x 96x       63x 61x     63x   63x     186x 137x 137x     741x        
import { UrlMatchResult, UrlSegment } from '@angular/router';
import { MonoTypeOperatorFunction } from 'rxjs';
import { filter } from 'rxjs/operators';
 
import { CategoryView } from 'ish-core/models/category-view/category-view.model';
import { Category } from 'ish-core/models/category/category.model';
import { CoreState } from 'ish-core/store/core/core-store';
import { selectRouteParam, selectRouteParamAorB } from 'ish-core/store/core/router';
import { sanitizeSlugData } from 'ish-core/utils/routing';
 
/**
 * generate a localized category slug
 *
 * @param category category element for slug
 * @returns localized, formatted category slug
 */
export function generateLocalizedCategorySlug(category: Category) {
  return sanitizeSlugData(category?.name);
}
 
// matcher to check if a given url is a category route
const categoryRouteFormat = /^\/(?!category|categoryref\/.*$)(.*?)-?ctg(.*)$/;
 
/**
 * check if given url is a category route
 *
 * @param segments current url segments
 * @returns match result if given url is a category route or not
 */
export function matchCategoryRoute(segments: UrlSegment[]): UrlMatchResult {
  // compatibility to old routes
  if (segments && segments.length === 2 && (segments[0].path === 'category' || segments[0].path === 'categoryref')) {
    return { consumed: [] };
  }
 
  // generate complete url path
  const url = `/${segments.map(s => s.path).join('/')}`;
 
  // check that complete url path is a category route
  if (categoryRouteFormat.test(url)) {
    // select categoryUniqueId to render a category component
    const match = categoryRouteFormat.exec(url);
    const posParams: { [id: string]: UrlSegment } = {};
    if (match[2]) {
      posParams.categoryUniqueId = new UrlSegment(match[2], {});
    }
    return {
      consumed: [],
      posParams,
    };
  }
  return;
}
 
/**
 * generate a localized category url from a category view
 *
 * @param category category view
 * @returns localized category url
 */
export function generateCategoryUrl(category: CategoryView): string {
  if (!category) {
    return '/';
  }
 
  // generate for each path element from the given category view a category slug and join them together to a complete route
  let route = `/${category.pathElements
    ?.filter(x => !!x)
    .map(el => generateLocalizedCategorySlug(el))
    .join('/')}`;
 
  // add to category route the category identifier
  if (route !== '/') {
    route += '-';
  }
 
  route += `ctg${category.uniqueId}`;
 
  return route;
}
 
export function ofCategoryUrl(): MonoTypeOperatorFunction<{}> {
  return source$ =>
    source$.pipe(
      filter(
        (state: CoreState) =>
          !selectRouteParam('sku')(state) && !!selectRouteParamAorB('categoryUniqueId', 'categoryRefId')(state)
      )
    );
}