All files / src/app/core/models/image image.mapper.ts

94.73% Statements 18/19
83.33% Branches 10/12
100% Functions 7/7
94.11% Lines 16/17

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 10031x 31x   31x                         31x       92x                   93x 92x   2x                   2x                         4x     4x 1x   3x                   13x 12x   1x                                                
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
 
import { getICMBaseURL } from 'ish-core/store/core/configuration';
 
import { Image } from './image.model';
 
/**
 * ImageMappers maps and adjusts meta-data of Images or its properties, i.e.
 * the absolute path of Image property effectiveUrl.
 *
 * @example
 * ImageMapper.fromImages(images)
 * ImageMapper.fromImage(image)
 */
@Injectable({ providedIn: 'root' })
export class ImageMapper {
  private icmBaseURL: string;
 
  constructor(store: Store) {
    store.pipe(select(getICMBaseURL)).subscribe(url => (this.icmBaseURL = url));
  }
 
  /**
   * Maps Images to Images.
   *
   * @param images The source images.
   * @returns The images.
   */
  fromImages(images: Image[]): Image[] {
    if (!images || images.length === 0) {
      return;
    }
    return images.map(image => this.fromImage(image));
  }
 
  /**
   * Maps Image to Image.
   *
   * @param image The source image.
   * @returns The image.
   */
  private fromImage(image: Image): Image {
    return {
      ...image,
      effectiveUrl: this.fromEffectiveUrl(image.effectiveUrl),
    };
  }
 
  /**
   * Builds absolute URL from relative URL and icmBaseURL or returns absolute URL.
   *
   * @param url The relative or absolute image URL.
   * @returns The URL.
   */
  fromEffectiveUrl(url: string): string {
    Iif (!url) {
      return;
    }
    if (url.match('^(https?|file):') || !url.startsWith('/')) {
      return url;
    }
    return `${this.icmBaseURL}${url}`;
  }
 
  /**
   * Maps a single product image URL to a minimum product images array.
   *
   * @param url The image URL.
   * @returns The minimum images (M and S image).
   */
  fromImageUrl(url: string): Image[] {
    if (!url) {
      return;
    }
    return [
      {
        effectiveUrl: this.fromEffectiveUrl(url),
        name: 'front M',
        primaryImage: true,
        type: 'Image',
        typeID: 'M',
        viewID: 'front',
        imageActualHeight: 270,
        imageActualWidth: 270,
      },
      {
        effectiveUrl: this.fromEffectiveUrl(url),
        name: 'front S',
        primaryImage: true,
        type: 'Image',
        typeID: 'S',
        viewID: 'front',
        imageActualHeight: 110,
        imageActualWidth: 110,
      },
    ];
  }
}