Skip to content

Commit 2f18c22

Browse files
fix(utils): add formatBytesCustom function
1 parent 256faa9 commit 2f18c22

File tree

8 files changed

+95
-3
lines changed

8 files changed

+95
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {GIGABYTE, KILOBYTE, MEGABYTE} from '../constants';
2+
import {isNumeric} from '../utils';
3+
4+
import i18n from './i18n';
5+
6+
const sizes = {
7+
b: {
8+
value: 1,
9+
label: i18n('b'),
10+
},
11+
kb: {
12+
value: KILOBYTE,
13+
label: i18n('kb'),
14+
},
15+
mb: {
16+
value: MEGABYTE,
17+
label: i18n('mb'),
18+
},
19+
gb: {
20+
value: GIGABYTE,
21+
label: i18n('gb'),
22+
},
23+
};
24+
25+
export type IBytesSizes = keyof typeof sizes;
26+
27+
interface FormatBytesArgs {
28+
value: number | string | undefined;
29+
size?: IBytesSizes;
30+
precision?: number;
31+
withLabel?: boolean;
32+
isSpeed?: boolean;
33+
}
34+
35+
export const formatBytesCustom = ({
36+
value,
37+
size = 'mb',
38+
precision = 0,
39+
withLabel = true,
40+
isSpeed = false,
41+
}: FormatBytesArgs) => {
42+
if (!isNumeric(value)) {
43+
return '';
44+
}
45+
46+
let result = (Number(value) / sizes[size].value).toFixed(precision);
47+
48+
if (withLabel) {
49+
result += ` ${sizes[size].label}`;
50+
51+
if (isSpeed) {
52+
result += i18n('perSecond');
53+
}
54+
}
55+
56+
return result;
57+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"b": "B",
3+
"kb": "KB",
4+
"mb": "MB",
5+
"gb": "GB",
6+
"perSecond": "/s"
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {i18n, Lang} from '../../i18n';
2+
3+
import en from './en.json';
4+
import ru from './ru.json';
5+
6+
const COMPONENT = 'ydb-bytes-parsers';
7+
8+
i18n.registerKeyset(Lang.En, COMPONENT, en);
9+
i18n.registerKeyset(Lang.Ru, COMPONENT, ru);
10+
11+
export default i18n.keyset(COMPONENT);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"b": "Б",
3+
"kb": "КБ",
4+
"mb": "МБ",
5+
"gb": "ГБ",
6+
"perSecond": ""
7+
}

src/utils/bytesParsers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './formatBytesCustom';

src/utils/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const AUTO_RELOAD_INTERVAL = 10 * SECOND;
99
// by agreement, display all byte values in decimal scale
1010
// values in data are always in bytes, never in higher units,
1111
// therefore there is no issue arbitrary converting them in UI
12+
export const KILOBYTE = 1_000;
1213
export const MEGABYTE = 1_000_000;
1314
export const GIGABYTE = 1_000_000_000;
1415
export const TERABYTE = 1_000_000_000_000;
@@ -118,3 +119,5 @@ export const DEFAULT_TABLE_SETTINGS = {
118119

119120
export const TENANT_INITIAL_TAB_KEY = 'saved_tenant_initial_tab';
120121
export const QUERY_INITIAL_RUN_ACTION_KEY = 'query_initial_run_action';
122+
123+
export const PARTITIONS_SELECTED_COLUMNS_KEY = 'partitionsSelectedColumns';

src/utils/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {isNumeric} from './utils';
99

1010
numeral.locale(i18n.lang);
1111

12+
// Here you can't control displayed size and precision
13+
// If you need more custom format, use formatBytesCustom instead
1214
export const formatBytes = (bytes) => {
1315
if (!isNumeric(bytes)) {
1416
return '';
@@ -42,6 +44,10 @@ export const formatUptime = (seconds) => {
4244
return uptime;
4345
};
4446

47+
export const formatMsToUptime = (ms) => {
48+
return formatUptime(ms / 1000);
49+
};
50+
4551
export const formatIOPS = (value, capacity) => {
4652
return [Math.floor(value), Math.floor(capacity) + ' IOPS'];
4753
};

src/utils/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ export function bytesToGB(bytes, shouldRound) {
7676

7777
export function pad9(val) {
7878
const len = String(val).length;
79-
let result = val
79+
let result = val;
8080
for (let i = len; i < 9; i++) {
81-
result = "0" + result;
81+
result = '0' + result;
8282
}
8383
return result;
8484
}
@@ -88,4 +88,4 @@ export function isNumeric(value) {
8888
// - isNaN treats true/false/''/etc. as numbers, parseFloat fixes this
8989
// - parseFloat treats '123qwe' as number, isNaN fixes this
9090
return !isNaN(value) && !isNaN(parseFloat(value));
91-
};
91+
}

0 commit comments

Comments
 (0)