diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-14 17:28:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-08-14 17:28:54 +0200 |
commit | b4c3c51dc874711febf43b719ca878436b31084d (patch) | |
tree | 19cc50cc8e28c9de33f001595a4c6d4ced9690cf /client/src/app/shared/shared-main/angular | |
parent | 93e903ac165eed918986e415127d6ea9730e572c (diff) | |
download | PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.tar.gz PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.tar.zst PeerTube-b4c3c51dc874711febf43b719ca878436b31084d.zip |
Fix circular dependencies
Diffstat (limited to 'client/src/app/shared/shared-main/angular')
-rw-r--r-- | client/src/app/shared/shared-main/angular/bytes.pipe.ts | 26 |
1 files changed, 2 insertions, 24 deletions
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 @@ | |||
1 | import { Pipe, PipeTransform } from '@angular/core' | 1 | import { Pipe, PipeTransform } from '@angular/core' |
2 | import { getBytes } from '@root-helpers/bytes' | ||
2 | 3 | ||
3 | // Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts | 4 | // Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts |
4 | 5 | ||
5 | @Pipe({ name: 'bytes' }) | 6 | @Pipe({ name: 'bytes' }) |
6 | export class BytesPipe implements PipeTransform { | 7 | export class BytesPipe implements PipeTransform { |
7 | private dictionary: Array<{ max: number; type: string }> = [ | ||
8 | { max: 1024, type: 'B' }, | ||
9 | { max: 1048576, type: 'KB' }, | ||
10 | { max: 1073741824, type: 'MB' }, | ||
11 | { max: 1.0995116e12, type: 'GB' } | ||
12 | ] | ||
13 | 8 | ||
14 | transform (value: number, precision?: number | undefined): string | number { | 9 | transform (value: number, precision?: number | undefined): string | number { |
15 | const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1] | 10 | return getBytes(value, precision) |
16 | const calc = value / (format.max / 1024) | ||
17 | |||
18 | const num = precision === undefined | ||
19 | ? calc | ||
20 | : applyPrecision(calc, precision) | ||
21 | |||
22 | return `${num} ${format.type}` | ||
23 | } | 11 | } |
24 | } | 12 | } |
25 | |||
26 | function applyPrecision (num: number, precision: number) { | ||
27 | if (precision <= 0) { | ||
28 | return Math.round(num) | ||
29 | } | ||
30 | |||
31 | const tho = 10 ** precision | ||
32 | |||
33 | return Math.round(num * tho) / tho | ||
34 | } | ||