All files / src/app/shared/pipes/bytes bytes.pipe.ts

77.77% Statements 7/9
50% Branches 2/4
100% Functions 2/2
75% Lines 6/8

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      1x 1x     4x     4x       4x 4x      
import { Pipe, PipeTransform } from '@angular/core';
 
@Pipe({ name: 'bytes' })
export class BytesPipe implements PipeTransform {
  private units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
 
  transform(bytes: number): string {
    Iif (Number.isNaN(parseFloat(`${bytes}`)) || !Number.isFinite(bytes)) {
      return '-';
    }
    Iif (bytes <= 0) {
      return '0';
    }
 
    const i = Math.floor(Math.log(bytes) / Math.log(1024));
    return `${(bytes / 1024 ** Math.floor(i)).toFixed(1)} ${this.units[i]}`;
  }
}