2021-03-26 04:44:41 +00:00
|
|
|
|
export const BYTES_UNITS = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
|
|
|
|
|
export const HERTZ_UNITS = ['Hz', 'KHz', 'MHz', 'GHz', 'THz', 'PHz'];
|
|
|
|
|
|
|
|
|
|
const locale = window.navigator.locale || 'en';
|
|
|
|
|
const decimalFormatter = new Intl.NumberFormat(locale, {
|
|
|
|
|
maximumFractionDigits: 2,
|
|
|
|
|
});
|
|
|
|
|
const roundFormatter = new Intl.NumberFormat(locale, {
|
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
|
});
|
|
|
|
|
|
2021-03-26 23:57:14 +00:00
|
|
|
|
const unitReducer = (number = 0, interval, units, maxUnit, startingUnit) => {
|
|
|
|
|
if (startingUnit && units.indexOf(startingUnit) !== -1) {
|
|
|
|
|
units = units.slice(units.indexOf(startingUnit));
|
|
|
|
|
}
|
2021-03-26 04:44:41 +00:00
|
|
|
|
if (maxUnit && units.indexOf(maxUnit) !== -1) {
|
|
|
|
|
units = units.slice(0, units.indexOf(maxUnit) + 1);
|
|
|
|
|
}
|
2021-03-29 23:16:48 +00:00
|
|
|
|
|
|
|
|
|
// Reduce negative numbers by temporarily flipping them positive.
|
|
|
|
|
const negative = number < 0;
|
|
|
|
|
if (negative) {
|
|
|
|
|
number *= -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 04:44:41 +00:00
|
|
|
|
let unitIndex = 0;
|
|
|
|
|
while (number >= interval && unitIndex < units.length - 1) {
|
|
|
|
|
number /= interval;
|
|
|
|
|
unitIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 23:16:48 +00:00
|
|
|
|
if (negative) {
|
|
|
|
|
number *= -1;
|
|
|
|
|
}
|
2021-03-26 04:44:41 +00:00
|
|
|
|
return [number, units[unitIndex]];
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-26 23:57:14 +00:00
|
|
|
|
export function reduceBytes(bytes = 0, maxUnitSize, startingUnitSize) {
|
|
|
|
|
return unitReducer(bytes, 1024, BYTES_UNITS, maxUnitSize, startingUnitSize);
|
2021-03-26 04:44:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 05:21:00 +00:00
|
|
|
|
export function reduceHertz(hertz = 0, maxUnitSize, startingUnitSize) {
|
2021-03-26 23:57:14 +00:00
|
|
|
|
return unitReducer(hertz, 1000, HERTZ_UNITS, maxUnitSize, startingUnitSize);
|
2021-03-26 04:44:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// General purpose formatters meant to reduce units as much
|
|
|
|
|
// as possible.
|
2021-03-26 23:57:14 +00:00
|
|
|
|
export function formatBytes(bytes, startingUnitSize) {
|
|
|
|
|
const [number, unit] = reduceBytes(bytes, null, startingUnitSize);
|
2021-03-26 04:44:41 +00:00
|
|
|
|
return `${decimalFormatter.format(number)} ${unit}`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 23:57:14 +00:00
|
|
|
|
export function formatHertz(hertz, startingUnitSize) {
|
|
|
|
|
const [number, unit] = reduceHertz(hertz, null, startingUnitSize);
|
2021-03-26 04:44:41 +00:00
|
|
|
|
return `${decimalFormatter.format(number)} ${unit}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Specialized formatters meant to reduce units to the resolution
|
|
|
|
|
// the scheduler and job specs operate at.
|
2021-03-26 23:57:14 +00:00
|
|
|
|
export function formatScheduledBytes(bytes, startingUnitSize) {
|
|
|
|
|
const [number, unit] = reduceBytes(bytes, 'MiB', startingUnitSize);
|
2021-03-26 04:44:41 +00:00
|
|
|
|
return `${roundFormatter.format(number)} ${unit}`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 23:57:14 +00:00
|
|
|
|
export function formatScheduledHertz(hertz, startingUnitSize) {
|
|
|
|
|
const [number, unit] = reduceHertz(hertz, 'MHz', startingUnitSize);
|
2021-03-26 04:44:41 +00:00
|
|
|
|
return `${roundFormatter.format(number)} ${unit}`;
|
|
|
|
|
}
|
2021-11-04 22:48:40 +00:00
|
|
|
|
|
|
|
|
|
// Replaces the minus sign (−) with a hyphen (-).
|
|
|
|
|
// https://github.com/d3/d3-format/releases/tag/v2.0.0
|
|
|
|
|
export function replaceMinus(input) {
|
|
|
|
|
return input.replace('−', '-');
|
|
|
|
|
}
|