forked from Aj7Ay/Netflix-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
51 lines (42 loc) · 1.19 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
export const getRandomNumber = (maxNumber: number) =>
Math.floor(Math.random() * maxNumber);
export const formatMinuteToReadable = (minutes: number) => {
const h = Math.floor(minutes / 60);
const m = minutes - h * 60;
if (h > 0) {
return `${h}h ${m}m`;
} else {
return `${m}m`;
}
};
export const formatBytes = (bytes: number, decimals: number = 2) => {
if (!+bytes) return "0 Bytes";
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = [
"Bytes",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
"ZiB",
"YiB",
];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
};
export const formatTime = (current: number) => {
const h = Math.floor(current / 3600);
const m = Math.floor((current - h * 3600) / 60);
const s = Math.floor(current % 60);
const sString = s < 10 ? "0" + s.toString() : s.toString();
const mString = m < 10 ? "0" + m.toString() : m.toString();
if (h > 0) {
const hString = h < 10 ? "0" + h.toString() : h.toString();
return `${hString}:${mString}:${sString}`;
} else {
return `${mString}:${sString}`;
}
};