-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.tsx
34 lines (31 loc) · 1013 Bytes
/
functions.tsx
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
function formatTime(duration) {
const hours = Math.floor(duration / 3600);
const minutes = Math.floor((duration % 3600) / 60);
const seconds = Math.round(duration % 60);
if (hours > 0) {
return `${hours}:${minutes < 10 ? "0" : ""}${minutes}:${
seconds < 10 ? "0" : ""
}${seconds}`;
} else if (minutes > 0) {
return `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
} else {
return `0:${seconds < 10 ? "0" : ""}${seconds}`;
}
}
function formatNumber(num, digits) {
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "K" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "G" },
{ value: 1e12, symbol: "T" },
{ value: 1e15, symbol: "P" },
{ value: 1e18, symbol: "E" },
];
const regexp = /\.0+$|(?<=\.[0-9]*[1-9])0+$/;
const item = lookup.findLast((item) => num >= item.value);
return item
? (num / item.value).toFixed(digits).replace(regexp, "").concat(item.symbol)
: "0";
}
export { formatTime, formatNumber };