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

98.07% Statements 51/52
86.48% Branches 32/37
100% Functions 11/11
97.91% Lines 47/48

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118105x   105x       105x 105x   105x 105x                 20x       20x   20x 1x 1x 4x 4x 4x       20x       105x               105x   31x 31x 31x 4x       50x     27x   20x 20x 20x 15x   20x 20x   20x         7x                   105x 61x 3x     58x     58x     58x 47x 23x       58x 20x       58x   58x 23x     58x     105x 1240x    
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 { ProductView } from 'ish-core/models/product-view/product-view.model';
import { ProductHelper } from 'ish-core/models/product/product.model';
import { generateLocalizedCategorySlug } from 'ish-core/routing/category/category.route';
import { CoreState } from 'ish-core/store/core/core-store';
import { selectRouteParam } from 'ish-core/store/core/router';
import { sanitizeSlugData } from 'ish-core/utils/routing';
 
/**
 * generate a localized product slug
 *
 * @param product product element for slug
 * @returns localized, formatted product slug
 */
function generateLocalizedProductSlug(product: ProductView) {
  Iif (!product) {
    return;
  }
 
  let slug = product.name || '';
 
  if (ProductHelper.isVariationProduct(product) && product.variableVariationAttributes?.length > 0) {
    slug += '-';
    slug += product.variableVariationAttributes
      .map(att => att.value)
      .filter(val => typeof val === 'string' || typeof val === 'boolean' || typeof val === 'number')
      .map(val => val.toString())
      .join('-');
  }
 
  return sanitizeSlugData(slug);
}
 
// matcher to check if a given url is a product url
const productRouteFormat = /\/(?!ctg)(?!.*-ctg.*-prd)(.*?)-?prd(.*?)(-ctg(.*))?$/;
 
/**
 * check if a given url is a product url
 *
 * @param segments current url segments
 * @returns match result if given url is a product route or not
 */
export function matchProductRoute(segments: UrlSegment[]): UrlMatchResult {
  // compatibility to old routes
  const isSimpleProduct = segments && segments.length > 0 && segments[0].path === 'product';
  const isContextProduct = segments?.length > 2 && segments[0].path === 'category' && segments[2].path === 'product';
  if (isSimpleProduct || isContextProduct) {
    return { consumed: [] };
  }
 
  // generate complete url path
  const url = `/${segments.map(s => s.path).join('/')}`;
 
  // check that complete url path is a product route
  if (productRouteFormat.test(url)) {
    // select product sku and categoryUniqueId to render a product detail page
    const match = productRouteFormat.exec(url);
    const posParams: Record<string, UrlSegment> = {};
    if (match[4]) {
      posParams.categoryUniqueId = new UrlSegment(match[4], {});
    }
    Eif (match[2]) {
      posParams.sku = new UrlSegment(match[2], {});
    }
    return {
      consumed: [],
      posParams,
    };
  }
  return;
}
 
/**
 * generate a localized product url from a product view
 *
 * @param product product view
 * @param category optional category view
 * @returns localized product url
 */
export function generateProductUrl(product: ProductView, category?: CategoryView): string {
  if (!product?.sku) {
    return '/';
  }
 
  let route = '/';
 
  // get right category context for given product
  const contextCategory = category || product?.defaultCategory;
 
  // generate for each path element from the category context a category slug and join them together to a complete route
  if (contextCategory?.pathElements) {
    route += contextCategory?.pathElements.map(el => generateLocalizedCategorySlug(el)).join('/');
    route += '/';
  }
 
  // add product slug with product name to the route
  if (product?.name) {
    route += `${generateLocalizedProductSlug(product)}-`;
  }
 
  // sku and category identifier are added to the route
  route += `prd${product.sku}`;
 
  if (contextCategory) {
    route += `-ctg${contextCategory.uniqueId}`;
  }
 
  return route;
}
 
export function ofProductUrl(): MonoTypeOperatorFunction<{}> {
  return source$ => source$.pipe(filter((state: CoreState) => !!selectRouteParam('sku')(state)));
}