From 94676e631c5045144da598fefbefaa3cfcaaeb0d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 11 Aug 2020 16:50:00 +0200 Subject: Remove angular pipes module --- .../app/shared/shared-main/angular/bytes.pipe.ts | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 client/src/app/shared/shared-main/angular/bytes.pipe.ts (limited to 'client/src/app/shared/shared-main/angular/bytes.pipe.ts') diff --git a/client/src/app/shared/shared-main/angular/bytes.pipe.ts b/client/src/app/shared/shared-main/angular/bytes.pipe.ts new file mode 100644 index 000000000..f4f473568 --- /dev/null +++ b/client/src/app/shared/shared-main/angular/bytes.pipe.ts @@ -0,0 +1,34 @@ +import { Pipe, PipeTransform } from '@angular/core' + +// 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}` + } +} + +function applyPrecision (num: number, precision: number) { + if (precision <= 0) { + return Math.round(num) + } + + const tho = 10 ** precision + + return Math.round(num * tho) / tho +} -- cgit v1.2.3