Skip to content

Instantly share code, notes, and snippets.

@4gray
Forked from Solomko2/fileSize.ts
Created July 31, 2018 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4gray/296f17f31f2aa2e53ece7d81c37cda4f to your computer and use it in GitHub Desktop.
Save 4gray/296f17f31f2aa2e53ece7d81c37cda4f to your computer and use it in GitHub Desktop.
fileSize pipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'fileSize'
})
export class FileSizePipe implements PipeTransform {
private units = [
'bytes',
'KB',
'MB',
'GB',
'TB',
'PB'
];
transform(bytes: number = 0, precision: number = 2 ): string {
if ( isNaN( parseFloat( String(bytes) )) || ! isFinite( bytes ) ) {
return '?'
}
let unit = 0;
while ( bytes >= 1024 ) {
bytes /= 1024;
unit ++;
}
return bytes.toFixed( + precision ) + ' ' + this.units[ unit ];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment