Dynamically adjust decmials in bitrate/filesize
This commit is contained in:
parent
f6646451b2
commit
b75f89ed7d
|
@ -14,26 +14,42 @@ export function convertFromBitrate(value, unit) {
|
||||||
return value / Rates[unit];
|
return value / Rates[unit];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatBitrate(bitrate) {
|
export function convertedRate(bitrate) {
|
||||||
if (bitrate > Rates["GiB/s"]) {
|
if (bitrate > Rates["GiB/s"]) {
|
||||||
return `${(bitrate / Rates["GiB/s"]).toFixed(2)} GiB/s`;
|
return [bitrate / Rates["GiB/s"], "GiB/s"];
|
||||||
} else if (bitrate > Rates["MiB/s"]) {
|
} else if (bitrate > Rates["MiB/s"]) {
|
||||||
return `${(bitrate / Rates["MiB/s"]).toFixed(2)} MiB/s`;
|
return [bitrate / Rates["MiB/s"], "MiB/s"];
|
||||||
} else if (bitrate > Rates["KiB/s"]) {
|
} else if (bitrate > Rates["KiB/s"]) {
|
||||||
return `${(bitrate / Rates["KiB/s"]).toFixed(2)} KiB/s`;
|
return [bitrate / Rates["KiB/s"], "KiB/s"];
|
||||||
} else {
|
} else {
|
||||||
return `${bitrate} B/s`;
|
return [bitrate, "B/s"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatAmount(amount) {
|
export function formatBitrate(bitrate) {
|
||||||
if (amount > Rates["GiB/s"]) {
|
const [rate, unit] = convertedRate(bitrate);
|
||||||
return `${(amount / Rates["GiB/s"]).toFixed(2)} GiB`;
|
let places = 2;
|
||||||
} else if (amount > Rates["MiB/s"]) {
|
if (rate >= 100) {
|
||||||
return `${(amount / Rates["MiB/s"]).toFixed(2)} MiB`;
|
places = 0;
|
||||||
} else if (amount > Rates["KiB/s"]) {
|
} else if (rate >= 10) {
|
||||||
return `${(amount / Rates["KiB/s"]).toFixed(2)} KiB`;
|
places = 1;
|
||||||
} else {
|
|
||||||
return `${amount} bytes`;
|
|
||||||
}
|
}
|
||||||
|
return `${rate.toFixed(places)} ${unit}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatAmount(amount) {
|
||||||
|
const units = {
|
||||||
|
"GiB/s": "GiB",
|
||||||
|
"MiB/s": "MiB",
|
||||||
|
"KiB/s": "KiB",
|
||||||
|
"B/s": "bytes"
|
||||||
|
};
|
||||||
|
const [rate, unit] = convertedRate(amount);
|
||||||
|
let places = 2;
|
||||||
|
if (rate >= 100) {
|
||||||
|
places = 0;
|
||||||
|
} else if (rate >= 10) {
|
||||||
|
places = 1;
|
||||||
|
}
|
||||||
|
return `${rate.toFixed(places)} ${units[unit]}`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,10 +77,10 @@ function File({ dispatch, file }) {
|
||||||
function Peer({ peer }) {
|
function Peer({ peer }) {
|
||||||
return (
|
return (
|
||||||
<div className="peer">
|
<div className="peer">
|
||||||
<div style={{flexGrow: 1}}>{peer.ip}</div>
|
<div style={{flexGrow: 5}}>{peer.ip}</div>
|
||||||
<div>{formatBitrate(peer.rate_up)} up</div>
|
<div style={{flexBasis: "18%"}}>{formatBitrate(peer.rate_up)} up</div>
|
||||||
<div>{formatBitrate(peer.rate_down)} down</div>
|
<div style={{flexBasis: "18%"}}>{formatBitrate(peer.rate_down)} down</div>
|
||||||
<div>has {`${(peer.availability * 100).toFixed(0)}%`}</div>
|
<div style={{flexBasis: "18%"}}>has {`${(peer.availability * 100).toFixed(0)}%`}</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user