All files / src/app/core/pipes make-href.pipe.ts

94.73% Statements 18/19
72.72% Branches 8/11
100% Functions 5/5
94.44% Lines 17/18

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  2x 2x 2x   2x 2x     2x 4x   3x       3x     3x   3x 2x 1x   1x 1x       1x     1x            
import { LocationStrategy } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
 
import { omit } from 'ish-core/utils/functions';
import { MultiSiteService } from 'ish-core/utils/multi-site/multi-site.service';
 
@Pipe({ name: 'ishMakeHref', pure: false })
export class MakeHrefPipe implements PipeTransform {
  constructor(private multiSiteService: MultiSiteService) {}
  transform(location: LocationStrategy, urlParams: Record<string, string>): Observable<string> {
    Iif (!location?.path()) {
      return of('undefined');
    }
 
    return of(location.path().split('?')).pipe(
      switchMap(split => {
        // url without query params
        const newUrl = split[0];
 
        if (urlParams) {
          if (urlParams.lang) {
            return this.multiSiteService.getLangUpdatedUrl(urlParams.lang, newUrl).pipe(
              map(modifiedUrl => {
                const modifiedUrlParams = modifiedUrl === newUrl ? urlParams : omit(urlParams, 'lang');
                return this.multiSiteService.appendUrlParams(modifiedUrl, modifiedUrlParams, split?.[1]);
              })
            );
          } else {
            return of(newUrl).pipe(map(url => this.multiSiteService.appendUrlParams(url, urlParams, split?.[1])));
          }
        } else {
          return of(this.multiSiteService.appendUrlParams(newUrl, undefined, split?.[1]));
        }
      })
    );
  }
}