X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-main%2Fangular%2Fbytes.pipe.ts;h=f3f57b8255e965a314aae3ab0452c63832abc046;hb=34ba86a8b481e0071c5253fcd562911a48688352;hp=f4f473568b2f8526a303f182ccfb8cc915b4f086;hpb=94676e631c5045144da598fefbefaa3cfcaaeb0d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-main/angular/bytes.pipe.ts b/client/src/app/shared/shared-main/angular/bytes.pipe.ts index f4f473568..f3f57b825 100644 --- a/client/src/app/shared/shared-main/angular/bytes.pipe.ts +++ b/client/src/app/shared/shared-main/angular/bytes.pipe.ts @@ -1,34 +1,12 @@ import { Pipe, PipeTransform } from '@angular/core' +import { getBytes } from '@root-helpers/bytes' // Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts @Pipe({ name: 'bytes' }) export class BytesPipe implements PipeTransform { - private dictionary: Array<{ max: number; type: string }> = [ - { max: 1024, type: 'B' }, - { max: 1048576, type: 'KB' }, - { max: 1073741824, type: 'MB' }, - { max: 1.0995116e12, type: 'GB' } - ] transform (value: number, precision?: number | undefined): string | number { - const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1] - const calc = value / (format.max / 1024) - - const num = precision === undefined - ? calc - : applyPrecision(calc, precision) - - return `${num} ${format.type}` + return getBytes(value, precision) } } - -function applyPrecision (num: number, precision: number) { - if (precision <= 0) { - return Math.round(num) - } - - const tho = 10 ** precision - - return Math.round(num * tho) / tho -}