diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-11 16:50:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-08-11 16:50:00 +0200 |
commit | 94676e631c5045144da598fefbefaa3cfcaaeb0d (patch) | |
tree | ad722f1316ff81a026072938b010ca6549b40e52 /client/src/app/shared/shared-main/angular | |
parent | f309a156a6201797b4eca19d090f37f2f0da403b (diff) | |
download | PeerTube-94676e631c5045144da598fefbefaa3cfcaaeb0d.tar.gz PeerTube-94676e631c5045144da598fefbefaa3cfcaaeb0d.tar.zst PeerTube-94676e631c5045144da598fefbefaa3cfcaaeb0d.zip |
Remove angular pipes module
Diffstat (limited to 'client/src/app/shared/shared-main/angular')
3 files changed, 36 insertions, 1 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 new file mode 100644 index 000000000..f4f473568 --- /dev/null +++ b/client/src/app/shared/shared-main/angular/bytes.pipe.ts | |||
@@ -0,0 +1,34 @@ | |||
1 | import { Pipe, PipeTransform } from '@angular/core' | ||
2 | |||
3 | // Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts | ||
4 | |||
5 | @Pipe({ name: 'bytes' }) | ||
6 | 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 | |||
14 | transform (value: number, precision?: number | undefined): string | number { | ||
15 | const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1] | ||
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 | } | ||
24 | } | ||
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 | } | ||
diff --git a/client/src/app/shared/shared-main/angular/index.ts b/client/src/app/shared/shared-main/angular/index.ts index 3b072fb84..9ba815136 100644 --- a/client/src/app/shared/shared-main/angular/index.ts +++ b/client/src/app/shared/shared-main/angular/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './bytes.pipe' | ||
1 | export * from './from-now.pipe' | 2 | export * from './from-now.pipe' |
2 | export * from './infinite-scroller.directive' | 3 | export * from './infinite-scroller.directive' |
3 | export * from './number-formatter.pipe' | 4 | export * from './number-formatter.pipe' |
diff --git a/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts b/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts index 8a0756a36..e2eba3a60 100644 --- a/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts +++ b/client/src/app/shared/shared-main/angular/number-formatter.pipe.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import { Pipe, PipeTransform } from '@angular/core' | 1 | import { Pipe, PipeTransform } from '@angular/core' |
2 | 2 | ||
3 | // Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts | 3 | // Thanks: https://github.com/danrevah/ngx-pipes/blob/master/src/ng-pipes/pipes/math/bytes.ts |
4 | 4 | ||
5 | @Pipe({ name: 'myNumberFormatter' }) | 5 | @Pipe({ name: 'myNumberFormatter' }) |
6 | export class NumberFormatterPipe implements PipeTransform { | 6 | export class NumberFormatterPipe implements PipeTransform { |