Skip to content

Commit 63582de

Browse files
authored
adding int value functions (#852)
1 parent d8840cc commit 63582de

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

portal-ui/src/common/utils.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ export const k8sUnits = ["Ki", "Mi", "Gi", "Ti", "Pi", "Ei"];
3636
export const k8sCalcUnits = ["B", ...k8sUnits];
3737

3838
export const niceBytes = (x: string, showK8sUnits: boolean = false) => {
39-
let l = 0,
40-
n = parseInt(x, 10) || 0;
39+
let n = parseInt(x, 10) || 0;
40+
41+
return niceBytesInt(n, showK8sUnits)
42+
};
43+
44+
export const niceBytesInt = (n: number, showK8sUnits: boolean = false) => {
45+
let l = 0
4146

4247
while (n >= 1024 && ++l) {
4348
n = n / 1024;
@@ -46,9 +51,9 @@ export const niceBytes = (x: string, showK8sUnits: boolean = false) => {
4651
//less than ten of KB or greater units
4752
const k8sUnitsN = ["B", ...k8sUnits];
4853
return (
49-
n.toFixed(n < 10 && l > 0 ? 1 : 0) +
50-
" " +
51-
(showK8sUnits ? k8sUnitsN[l] : units[l])
54+
n.toFixed(n < 10 && l > 0 ? 1 : 0) +
55+
" " +
56+
(showK8sUnits ? k8sUnitsN[l] : units[l])
5257
);
5358
};
5459

@@ -425,6 +430,11 @@ export const generatePoolName = (pools: IPool[]) => {
425430
export const niceDays = (secondsValue: string, timeVariant: string = "s") => {
426431
let seconds = parseFloat(secondsValue);
427432

433+
return niceDaysInt(seconds, timeVariant)
434+
};
435+
436+
export const niceDaysInt = (seconds: number, timeVariant: string = "s") => {
437+
428438
switch (timeVariant) {
429439
case "ns":
430440
seconds = Math.floor(seconds * 0.000000001);
@@ -453,7 +463,7 @@ export const niceDays = (secondsValue: string, timeVariant: string = "s") => {
453463
const diffDays = days - months * 30;
454464

455465
return `${months} month${Math.floor(months) === 1 ? "" : "s"} ${
456-
diffDays > 0 ? `${diffDays} day${diffDays > 1 ? "s" : ""}` : ""
466+
diffDays > 0 ? `${diffDays} day${diffDays > 1 ? "s" : ""}` : ""
457467
}`;
458468
}
459469

@@ -468,13 +478,13 @@ export const niceDays = (secondsValue: string, timeVariant: string = "s") => {
468478
}
469479

470480
return `${hours >= 1 ? `${hours} hour${hours > 1 ? "s" : ""}` : ""} ${
471-
minutes >= 1 && hours === 0
472-
? `${minutes} minute${minutes > 1 ? "s" : ""}`
473-
: ""
481+
minutes >= 1 && hours === 0
482+
? `${minutes} minute${minutes > 1 ? "s" : ""}`
483+
: ""
474484
} ${
475-
seconds >= 1 && minutes === 0 && hours === 0
476-
? `${seconds} second${seconds > 1 ? "s" : ""}`
477-
: ""
485+
seconds >= 1 && minutes === 0 && hours === 0
486+
? `${seconds} second${seconds > 1 ? "s" : ""}`
487+
: ""
478488
}`;
479489
};
480490

0 commit comments

Comments
 (0)