All files / src/app/core/utils link-parser.ts

100% Statements 13/13
100% Branches 13/13
100% Functions 1/1
100% Lines 13/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 3742x 42x       31x 27x   27x   27x     4x     2x     6x     9x         5x     1x     5x      
export class LinkParser {
  private static linkRegexp = /^(unsafe:)?([a-z]+):\/\/(.*?)(@.*)?$/;
 
  // eslint-disable-next-line complexity
  static parseLink(link: string, baseHref?: string): string {
    if (LinkParser.linkRegexp.test(link)) {
      const [, , protocol, value, unitName] = LinkParser.linkRegexp.exec(link);
 
      const prefix = !baseHref || baseHref === '/' ? '' : baseHref;
 
      switch (protocol) {
        case 'product':
          // product page links
          return `${prefix}/product/${value}`;
        case 'category':
          // category page links
          return `${prefix}/categoryref/${value}${unitName}`;
        case 'page':
          // content page links
          return `${prefix}/page/${value}`;
        case 'route':
          // direct router links for the PWA
          return `${prefix}/${value}`;
        case 'http':
        case 'https':
        case 'javascript':
          // external links are not changed
          return link;
        default:
          // eslint-disable-next-line no-console
          console.log('Unknown link type:', link);
      }
    }
    return link;
  }
}