Files
iTunes/dashboard/src/app/pipes/convert-size-to-string.pipe.ts
Maxence G. de Montauzan 6300e9cfc7 Refactoring
Move Top Played part.
Move pipes and dashboard
Rename object folder to model
Doc pipes
2018-03-20 23:32:21 +01:00

27 lines
585 B
TypeScript

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'convertSizeToString'
})
export class ConvertSizeToStringPipe implements PipeTransform {
/**
* Convert size to human readable size
* @param size size in byte
*/
transform(size: number): any {
const units = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB'];
if (size === 0) {
return '0 Byte';
}
const i = Math.floor(Math.log(size) / Math.log(1024));
let calcSize = size / Math.pow(1024, i);
calcSize = Math.round(calcSize * 100) / 100;
return calcSize + ' ' + units[i];
}
}