All files / src/app/core/models/attribute attribute.pipe.ts

93.1% Statements 27/29
87.09% Branches 27/31
100% Functions 7/7
92.59% Lines 25/27

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 713x 3x 3x     3x 3x         3x 40x     6x       26x       2x         40x       40x   7x         8x   2x   2x         16x     5x   4x   4x 4x   2x 2x   2x         2x        
import { formatNumber } from '@angular/common';
import { Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
 
import { Price } from 'ish-core/models/price/price.model';
import { formatPrice } from 'ish-core/models/price/price.pipe';
import { formatISHDate } from 'ish-core/pipes/date.pipe';
 
import { Attribute } from './attribute.model';
 
@Pipe({ name: 'ishAttribute', pure: false })
export class AttributeToStringPipe implements PipeTransform {
  constructor(private translateService: TranslateService) {}
 
  private toDate(val: string | number | Date): string {
    return formatISHDate(val, 'shortDate', this.translateService.currentLang);
  }
 
  private toDecimal(val: number): string {
    return formatNumber(val, this.translateService.currentLang);
  }
 
  private toCurrency(price: Price): string {
    return formatPrice(price, this.translateService.currentLang);
  }
 
  // eslint-disable-next-line complexity
  transform(data: Attribute, valuesSeparator: string = ', '): string {
    Iif (!this.translateService.currentLang) {
      return 'undefined';
    }
 
    switch (data.type) {
      case 'String':
        return data.value as string;
      case 'Integer':
      case 'Double':
      case 'Long':
      case 'BigDecimal':
        return this.toDecimal(data.value as number);
      case 'Boolean':
        return data.value.toString();
      case 'Date':
        return this.toDate(data.value as string | number | Date);
      case 'MultipleInteger':
      case 'MultipleDouble':
      case 'MultipleLong':
      case 'MultipleBigDecimal':
        return Array.isArray(data.value) && data.value.map(v => this.toDecimal(v)).join(valuesSeparator);
      case 'MultipleString':
      case 'MultipleBoolean':
        return Array.isArray(data.value) && data.value.join(valuesSeparator);
      case 'MultipleDate':
        return Array.isArray(data.value) && data.value.map(v => this.toDate(v)).join(valuesSeparator);
      case 'ResourceAttribute':
        const resourceAttribute = data as Attribute<{ type: string }>;
        switch (resourceAttribute.value.type) {
          case 'Quantity':
            const quantityAttribute = data as Attribute<{ value: unknown; unit: string }>;
            return `${this.toDecimal(quantityAttribute.value.value as number)}\xA0${quantityAttribute.value.unit}`;
          case 'Money':
            return this.toCurrency(data.value as Price);
          default:
            return data.value.toString();
        }
      default:
        return data.value.toString();
    }
  }
}