2017-09-07 08:19:11 +02:00
|
|
|
export const Rates = {
|
2017-10-01 18:20:54 +02:00
|
|
|
"B/s": Math.pow(1024, 0),
|
2017-09-07 08:19:11 +02:00
|
|
|
"KiB/s": Math.pow(1024, 1),
|
|
|
|
"MiB/s": Math.pow(1024, 2),
|
|
|
|
"GiB/s": Math.pow(1024, 3),
|
|
|
|
"TiB/s": Math.pow(1024, 4),
|
|
|
|
};
|
|
|
|
|
|
|
|
export function convertToBitrate(value, unit) {
|
|
|
|
return value * Rates[unit];
|
|
|
|
}
|
|
|
|
|
2017-09-10 13:17:32 +02:00
|
|
|
export function convertFromBitrate(value, unit) {
|
|
|
|
return value / Rates[unit];
|
|
|
|
}
|
|
|
|
|
2017-12-29 07:35:07 +01:00
|
|
|
export function convertedRate(bitrate) {
|
2018-02-08 03:05:38 +01:00
|
|
|
if (bitrate > Rates["TiB/s"]) {
|
|
|
|
return [bitrate / Rates["TiB/s"], "TiB/s"];
|
|
|
|
} else if (bitrate > Rates["GiB/s"]) {
|
2017-12-29 07:35:07 +01:00
|
|
|
return [bitrate / Rates["GiB/s"], "GiB/s"];
|
2017-09-07 08:19:11 +02:00
|
|
|
} else if (bitrate > Rates["MiB/s"]) {
|
2017-12-29 07:35:07 +01:00
|
|
|
return [bitrate / Rates["MiB/s"], "MiB/s"];
|
2017-09-07 08:19:11 +02:00
|
|
|
} else if (bitrate > Rates["KiB/s"]) {
|
2017-12-29 07:35:07 +01:00
|
|
|
return [bitrate / Rates["KiB/s"], "KiB/s"];
|
2017-08-25 04:49:24 +02:00
|
|
|
} else {
|
2017-12-29 07:35:07 +01:00
|
|
|
return [bitrate, "B/s"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 20:26:29 +01:00
|
|
|
function places(n) {
|
2017-12-29 07:35:07 +01:00
|
|
|
let places = 2;
|
2017-12-29 20:26:29 +01:00
|
|
|
if (n >= 100) {
|
2017-12-29 07:35:07 +01:00
|
|
|
places = 0;
|
2017-12-29 20:26:29 +01:00
|
|
|
} else if (n >= 10) {
|
2017-12-29 07:35:07 +01:00
|
|
|
places = 1;
|
2017-12-29 20:26:29 +01:00
|
|
|
}
|
|
|
|
if (n.toFixed(places) === (0).toFixed(places)) {
|
2017-12-29 08:07:40 +01:00
|
|
|
places = 0;
|
2017-08-25 04:49:24 +02:00
|
|
|
}
|
2017-12-29 20:26:29 +01:00
|
|
|
return places;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatBitrate(bitrate) {
|
|
|
|
const [rate, unit] = convertedRate(bitrate);
|
|
|
|
return `${rate.toFixed(places(rate))} ${unit}`;
|
2017-08-25 04:49:24 +02:00
|
|
|
}
|
2017-09-10 14:21:08 +02:00
|
|
|
|
|
|
|
export function formatAmount(amount) {
|
2017-12-29 07:35:07 +01:00
|
|
|
const units = {
|
2018-02-08 03:05:38 +01:00
|
|
|
"TiB/s": "TiB",
|
2017-12-29 07:35:07 +01:00
|
|
|
"GiB/s": "GiB",
|
|
|
|
"MiB/s": "MiB",
|
|
|
|
"KiB/s": "KiB",
|
|
|
|
"B/s": "bytes"
|
|
|
|
};
|
|
|
|
const [rate, unit] = convertedRate(amount);
|
2017-12-29 20:26:29 +01:00
|
|
|
return `${rate.toFixed(places(rate))} ${units[unit]}`;
|
2017-09-10 14:21:08 +02:00
|
|
|
}
|