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

91.3% Statements 21/23
68.75% Branches 11/16
100% Functions 7/7
90.47% Lines 19/21

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 7124x 24x   24x   24x         24x       4x       2x 1x     1x 1x 1x                         2x       2x 2x 2x                         6x   2x   2x   2x                
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
 
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Image } from 'ish-core/models/image/image.model';
import { getStaticEndpoint } from 'ish-core/store/core/configuration';
 
import { SparqueImage } from './sparque-image.interface';
 
@Injectable({ providedIn: 'root' })
export class SparqueImageMapper {
  private staticURL: string;
 
  constructor(store: Store) {
    store.pipe(select(getStaticEndpoint)).subscribe(staticURL => (this.staticURL = staticURL));
  }
 
  fromImageUrl(imageUrl: string): Image {
    if (!imageUrl) {
      return;
    }
    const typeID =
      imageUrl.split('/').length > 1 ? (imageUrl.split('/')[0].length > 1 ? 'S' : imageUrl.split('/')[0]) : 'S';
    const viewID = 'front';
    return {
      effectiveUrl: `${this.staticURL}/${imageUrl}`,
      viewID,
      typeID,
      type: 'Image',
      name: `${viewID} ${typeID}`,
      imageActualHeight: this.mapImageSize(typeID),
      imageActualWidth: this.mapImageSize(typeID),
      primaryImage: true,
    };
  }
 
  fromImages(images: SparqueImage[]): Image[] {
    return images?.length ? images.map(image => this.fromImage(image)) : [];
  }
 
  private fromImage(image: SparqueImage): Image {
    const viewID = AttributeHelper.getAttributeValueByAttributeName<string>(image.attributes, 'image-view');
    const typeID = AttributeHelper.getAttributeValueByAttributeName<string>(image.attributes, 'image-type');
    return {
      effectiveUrl: `${this.staticURL}/${image.id}`,
      typeID,
      viewID,
      type: 'Image',
      name: `${viewID} ${typeID}`,
      imageActualHeight: this.mapImageSize(typeID),
      imageActualWidth: this.mapImageSize(typeID),
      primaryImage: viewID === 'front',
    };
  }
 
  private mapImageSize(typeID: string): number {
    switch (typeID) {
      case 'S':
        return 110;
      case 'M':
        return 270;
      case 'L':
        return 500;
      case 'ZOOM':
        return 1500;
      default:
        return 270;
    }
  }
}