Dynamically adjust decmials in bitrate/filesize

This commit is contained in:
Drew DeVault 2017-12-29 01:35:07 -05:00
parent f6646451b2
commit b75f89ed7d
2 changed files with 34 additions and 18 deletions

View File

@ -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 formatBitrate(bitrate) {
const [rate, unit] = convertedRate(bitrate);
let places = 2;
if (rate >= 100) {
places = 0;
} else if (rate >= 10) {
places = 1;
}
return `${rate.toFixed(places)} ${unit}`;
}
export function formatAmount(amount) { export function formatAmount(amount) {
if (amount > Rates["GiB/s"]) { const units = {
return `${(amount / Rates["GiB/s"]).toFixed(2)} GiB`; "GiB/s": "GiB",
} else if (amount > Rates["MiB/s"]) { "MiB/s": "MiB",
return `${(amount / Rates["MiB/s"]).toFixed(2)} MiB`; "KiB/s": "KiB",
} else if (amount > Rates["KiB/s"]) { "B/s": "bytes"
return `${(amount / Rates["KiB/s"]).toFixed(2)} KiB`; };
} else { const [rate, unit] = convertedRate(amount);
return `${amount} bytes`; let places = 2;
if (rate >= 100) {
places = 0;
} else if (rate >= 10) {
places = 1;
} }
return `${rate.toFixed(places)} ${units[unit]}`;
} }

View File

@ -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>
); );
} }