import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'dataSize'
})
export class DataSizePipe implements PipeTransform {

  transform(num: number): any {
    return this.niceBytes(num);
  }

  niceBytes(x) {
    const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    let l = 0,
      n = parseInt(x, 10) || 0;
    while (n >= 1024 && ++l) {
      n = n / 1024;
    }
    return (n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
  }
}
