]>
Commit | Line | Data |
---|---|---|
05a536ef TL |
1 | import { Injectable } from '@angular/core'; |
2 | import { FormatterService } from './formatter.service'; | |
3 | ||
4 | @Injectable({ | |
5 | providedIn: 'root' | |
6 | }) | |
7 | export class NumberFormatterService { | |
8 | readonly bytesLabels = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; | |
9 | readonly bytesPerSecondLabels = [ | |
10 | 'B/s', | |
11 | 'KiB/s', | |
12 | 'MiB/s', | |
13 | 'GiB/s', | |
14 | 'TiB/s', | |
15 | 'PiB/s', | |
16 | 'EiB/s', | |
17 | 'ZiB/s', | |
18 | 'YiB/s' | |
19 | ]; | |
20 | readonly secondsLabels = ['ns', 'μs', 'ms', 's', 'ks', 'Ms']; | |
21 | readonly unitlessLabels = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']; | |
22 | ||
23 | constructor(private formatter: FormatterService) {} | |
24 | ||
25 | formatFromTo( | |
26 | value: any, | |
27 | units: string, | |
aee94f69 | 28 | targetedUnits: string = '', |
05a536ef | 29 | factor: number, |
aee94f69 TL |
30 | labels: string[], |
31 | decimals: number = 1 | |
05a536ef | 32 | ): any { |
aee94f69 | 33 | return this.formatter.formatNumberFromTo(value, units, targetedUnits, factor, labels, decimals); |
05a536ef TL |
34 | } |
35 | ||
aee94f69 TL |
36 | formatBytesFromTo(value: any, units: string, targetedUnits: string, decimals: number = 1): any { |
37 | return this.formatFromTo(value, units, targetedUnits, 1024, this.bytesLabels, decimals); | |
05a536ef TL |
38 | } |
39 | ||
aee94f69 TL |
40 | formatBytesPerSecondFromTo( |
41 | value: any, | |
42 | units: string, | |
43 | targetedUnits: string, | |
44 | decimals: number = 1 | |
45 | ): any { | |
46 | return this.formatFromTo( | |
47 | value, | |
48 | units, | |
49 | targetedUnits, | |
50 | 1024, | |
51 | this.bytesPerSecondLabels, | |
52 | decimals | |
53 | ); | |
05a536ef TL |
54 | } |
55 | ||
aee94f69 TL |
56 | formatSecondsFromTo(value: any, units: string, targetedUnits: string, decimals: number = 1): any { |
57 | return this.formatFromTo(value, units, targetedUnits, 1000, this.secondsLabels, decimals); | |
05a536ef TL |
58 | } |
59 | ||
aee94f69 TL |
60 | formatUnitlessFromTo( |
61 | value: any, | |
62 | units: string, | |
63 | targetedUnits: string = '', | |
64 | decimals: number = 1 | |
65 | ): any { | |
66 | return this.formatFromTo(value, units, targetedUnits, 1000, this.unitlessLabels, decimals); | |
05a536ef TL |
67 | } |
68 | } |