Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 796c0cf

Browse files
rawagnersuomiy
authored andcommitted
Align byte units to tectonic ones. (#338)
1 parent 4418191 commit 796c0cf

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/components/ClusterOverview/Capacity/tests/__snapshots__/Capacity.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports[`<Capacity /> renders correctly 1`] = `
4545
Memory
4646
</h3>
4747
<h6>
48-
1.0 MiB available out of 1 MiB
48+
1.0 Mi available out of 1 Mi
4949
</h6>
5050
<div>
5151
<div

src/components/ClusterOverview/Utilization/tests/__snapshots__/Utilization.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ exports[`<Utilization /> renders correctly 1`] = `
6262
<div
6363
class="kubevirt-utilization__item-actual col-lg-6 col-md-6 col-sm-6 col-xs-6"
6464
>
65-
30 KiB
65+
30 Ki
6666
</div>
6767
</div>
6868
<div

src/utils/tests/utils.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import { formatBytes, formatCores, formatNetTraffic } from '../utils';
33
describe('unit format functions', () => {
44
it('formats bytes', () => {
55
expect(formatBytes(2)).toEqual({ value: 2, unit: 'B' });
6-
expect(formatBytes(3 * 1024)).toEqual({ value: 3, unit: 'KiB' });
7-
expect(formatBytes(3 * 1024 ** 2)).toEqual({ value: 3, unit: 'MiB' });
8-
expect(formatBytes(3 * 1024 ** 3)).toEqual({ value: 3, unit: 'GiB' });
9-
expect(formatBytes(3 * 1024 ** 4)).toEqual({ value: 3, unit: 'TiB' });
10-
expect(formatBytes(3 * 1024 ** 5)).toEqual({ value: 3, unit: 'PiB' });
11-
expect(formatBytes(3 * 1024 ** 6)).toEqual({ value: 3 * 1024, unit: 'PiB' });
12-
expect(formatBytes(3 * 1024 ** 7)).toEqual({ value: 3 * 1024 ** 2, unit: 'PiB' });
6+
expect(formatBytes(3 * 1024)).toEqual({ value: 3, unit: 'Ki' });
7+
expect(formatBytes(3 * 1024 ** 2)).toEqual({ value: 3, unit: 'Mi' });
8+
expect(formatBytes(3 * 1024 ** 3)).toEqual({ value: 3, unit: 'Gi' });
9+
expect(formatBytes(3 * 1024 ** 4)).toEqual({ value: 3, unit: 'Ti' });
10+
expect(formatBytes(3 * 1024 ** 5)).toEqual({ value: 3, unit: 'Pi' });
11+
expect(formatBytes(3 * 1024 ** 6)).toEqual({ value: 3 * 1024, unit: 'Pi' });
12+
expect(formatBytes(3 * 1024 ** 7)).toEqual({ value: 3 * 1024 ** 2, unit: 'Pi' });
1313
});
1414
it('formats bytes in preferred units', () => {
15-
expect(formatBytes(3 * 1024 * 1024, 'KiB')).toEqual({ value: 3 * 1024, unit: 'KiB' });
15+
expect(formatBytes(3 * 1024 * 1024, 'Ki')).toEqual({ value: 3 * 1024, unit: 'Ki' });
1616
});
1717
it('formats cores', () => {
1818
expect(formatCores(2)).toEqual({ value: 2, unit: 'cores' });
1919
});
2020
it('formats net traffic', () => {
21-
expect(formatNetTraffic(3 * 1024 * 1024)).toEqual({ value: 3, unit: 'MiBps' });
21+
expect(formatNetTraffic(3 * 1000 * 1000)).toEqual({ value: 3, unit: 'MBps' });
2222
});
2323
});

src/utils/utils.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,42 @@ export const getResource = (
8282
return res;
8383
};
8484

85-
const BYTE_UNITS = {
86-
B: 0,
87-
KiB: 1,
88-
MiB: 2,
89-
GiB: 3,
90-
TiB: 4,
91-
PiB: 5,
85+
const BYTE_UNITS_CONFIG = {
86+
conversion: 1024,
87+
units: {
88+
B: 0,
89+
Ki: 1,
90+
Mi: 2,
91+
Gi: 3,
92+
Ti: 4,
93+
Pi: 5,
94+
},
9295
};
9396

94-
export const formatBytes = (bytes, unit, fixed = 2) => {
95-
unit = unit || Object.keys(BYTE_UNITS).find(key => bytes < 1024 ** (BYTE_UNITS[key] + 1)) || 'PiB';
96-
return { value: Number((bytes / 1024 ** BYTE_UNITS[unit]).toFixed(fixed)), unit };
97+
const NETWORK_BYTE_UNITS_CONFIG = {
98+
conversion: 1000,
99+
units: {
100+
Bps: 0,
101+
KBps: 1,
102+
MBps: 2,
103+
GBps: 3,
104+
TBps: 4,
105+
PBps: 5,
106+
},
107+
};
108+
109+
export const formatBytes = (bytes, unit, fixed = 2, conversionConfig = BYTE_UNITS_CONFIG) => {
110+
const { units, conversion } = conversionConfig;
111+
const unitKeys = Object.keys(units);
112+
unit = unit || unitKeys.find(key => bytes < conversion ** (units[key] + 1)) || unitKeys[unitKeys.length - 1];
113+
return { value: Number((bytes / conversion ** units[unit]).toFixed(fixed)), unit };
97114
};
98115

99116
export const formatCores = cores => ({ value: cores, unit: 'cores' });
100117
export const formatPercents = percents => ({ value: percents, unit: '%' });
101118

102-
export const formatNetTraffic = (bytesPerSecond, preferredUnit, fixed = 2) => {
103-
preferredUnit =
104-
preferredUnit && preferredUnit.endsWith('ps') ? preferredUnit.slice(0, preferredUnit.length - 2) : preferredUnit;
105-
const formatted = formatBytes(bytesPerSecond, preferredUnit, fixed);
106-
formatted.unit = `${formatted.unit}ps`;
107-
return formatted;
108-
};
119+
export const formatNetTraffic = (bytesPerSecond, preferredUnit, fixed = 2) =>
120+
formatBytes(bytesPerSecond, preferredUnit, fixed, NETWORK_BYTE_UNITS_CONFIG);
109121

110122
export const getNetworkBindings = networkType => {
111123
switch (networkType) {

0 commit comments

Comments
 (0)