aboutsummaryrefslogtreecommitdiff
path: root/ui/src/app/downloads.pipe.ts
blob: cb3bf922f633302fb2e8bddb2cf909b76223bf12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'eta'
})
export class EtaPipe implements PipeTransform {
  transform(value: number, ...args: any[]): any {
    if (value === null) {
      return null;
    }
    if (value < 60) {
      return `${value}s`;
    }
    if (value < 3600) {
      return `${Math.floor(value/60)}m ${value%60}s`;
    }
    const hours = Math.floor(value/3600)
    const minutes = value % 3600
    return `${hours}h ${Math.floor(minutes/60)}m ${minutes%60}s`;
  }
}

@Pipe({
  name: 'speed'
})
export class SpeedPipe implements PipeTransform {
  transform(value: number, ...args: any[]): any {
    if (value === null) {
      return null;
    }
    const k = 1024;
    const dm = 2;
    const sizes = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s', 'PB/s', 'EB/s', 'ZB/s', 'YB/s'];
    const i = Math.floor(Math.log(value) / Math.log(k));
    return parseFloat((value / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  }
}

@Pipe({
  name: 'encodeURIComponent'
})
export class EncodeURIComponent implements PipeTransform {
  transform(value: string, ...args: any[]): any {
    return encodeURIComponent(value);
  }
}
bgstack15