import { STATISTICS } from "./constants";
/**
 * Given a number, will return a nice string, 1,024 will return 1KB, 100 will return 100 Bytes, and so on.
 *
 * The result is displayed with 2 decimal points of precision.
 */
export function bytesToSize(bytes) {
    if (bytes === 0)
        return "0 Bytes";
    // Size powers, in English
    const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
    // Determine the size power for the number of bytes
    const i = Math.floor(Math.log(bytes) / Math.log(1024));
    // Adjusted down to how many KB, MB, GB, etc...
    const bytesByScale = bytes / Math.pow(1024, i);
    // Return the size, rounded, with 2 decimal points of precision
    return `${Math.round(bytesByScale * 100) / 100} ${sizes[i]}`;
}
/**
 * Handler for Array.reduce. It takes the array of statistics and sums each value.
 * This outputs an object that has total CPU and memory consumption.
 */
export const statReducer = (prev, curr) => {
    const statistics = {};
    STATISTICS.forEach((stat) => {
        var _a, _b;
        if (typeof prev.statistics[stat.value] === "undefined" || typeof curr.statistics[stat.value] === "undefined") {
            statistics[stat.value] = undefined;
            return;
        }
        statistics[stat.value] = ((_a = prev.statistics[stat.value]) !== null && _a !== void 0 ? _a : 0) + ((_b = curr.statistics[stat.value]) !== null && _b !== void 0 ? _b : 0);
    });
    statistics.name = "Totals";
    return {
        visible: curr.visible,
        statistics,
    };
};
/**
 * Convenience function for rounding decimals to a given precision
 * Note that "precision" here means "number of digits to the right of the decimal point"
 */
export const roundDecimalToPrecision = (number, precision) => {
    // Example: number = .12345, precision = 2
    const shiftedNumber = number * Math.pow(10, precision); // example: 12.345
    const roundedShiftedNumber = Math.round(shiftedNumber); // example: 12
    return roundedShiftedNumber / Math.pow(10, precision); // example: .12
};
//# sourceMappingURL=helpers.js.map