]>
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, | |
28 | targetedUnits: string, | |
29 | factor: number, | |
30 | labels: string[] | |
31 | ): any { | |
32 | return this.formatter.formatNumberFromTo(value, units, targetedUnits, factor, labels); | |
33 | } | |
34 | ||
35 | formatBytesFromTo(value: any, units: string, targetedUnits: string): any { | |
36 | return this.formatFromTo(value, units, targetedUnits, 1024, this.bytesLabels); | |
37 | } | |
38 | ||
39 | formatBytesPerSecondFromTo(value: any, units: string, targetedUnits: string): any { | |
40 | return this.formatFromTo(value, units, targetedUnits, 1024, this.bytesPerSecondLabels); | |
41 | } | |
42 | ||
43 | formatSecondsFromTo(value: any, units: string, targetedUnits: string): any { | |
44 | return this.formatFromTo(value, units, targetedUnits, 1000, this.secondsLabels); | |
45 | } | |
46 | ||
47 | formatUnitlessFromTo(value: any, units: string, targetedUnits: string): any { | |
48 | return this.formatFromTo(value, units, targetedUnits, 1000, this.unitlessLabels); | |
49 | } | |
50 | } |