All files / src/app/core/pipes frequency.pipe.ts

86.66% Statements 13/15
75% Branches 9/12
100% Functions 2/2
86.66% Lines 13/15

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 3613x 13x             13x 9x     8x 8x     8x 4x 4x     8x       4x   2x   2x            
import { Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
 
/**
 * The frequency pipe converts a string of special interval format to the corresponding display string
 * example: interval 'P21D' returns '3 Week(s)'
 */
@Pipe({ name: 'ishFrequency' })
export class FrequencyPipe implements PipeTransform {
  constructor(private translate: TranslateService) {}
 
  transform(interval: string): string {
    let period = interval.slice(-1).toUpperCase();
    let duration = parseInt(interval.slice(1, -1), 10);
 
    // convert days to weeks if possible since the API only returns daily, monthly or yearly intervals
    if (period === 'D' && duration % 7 === 0) {
      period = 'W';
      duration = duration / 7;
    }
 
    switch (period) {
      case 'D':
        return this.translate.instant('order.recurrence.period.days', { 0: duration });
      case 'W':
        return this.translate.instant('order.recurrence.period.weeks', { 0: duration });
      case 'M':
        return this.translate.instant('order.recurrence.period.months', { 0: duration });
      case 'Y':
        return this.translate.instant('order.recurrence.period.years', { 0: duration });
      default:
        return interval;
    }
  }
}