@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
27 lines • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyInstalled = exports.prettyActionPlan = exports.formatSize = void 0;
/**
* Formats a byte value into its correct size in kb or mb unit taking into
* the device block size. It accepts a formatting function to be able to
* customize the rounding if needed.
**/
const formatSize = (value = 0, blockSize, floor = false) => {
const units = ["bytes", "kbUnit", "mbUnit"];
const k = 1024; // 1kb unit
const bytes = Math.ceil(value / blockSize) * blockSize;
const i = Math.floor(Math.log(bytes) / Math.log(k)) || 1; // Nb Byte units were removed from UI
const rawSize = bytes / Math.pow(k, i);
const roundingPrecision = rawSize < 1 ? 1 : i > 1 ? 2 : 0;
const divider = Math.pow(10, roundingPrecision);
const toFormat = rawSize * divider;
let formattedSize = floor ? Math.floor(toFormat) : Math.ceil(toFormat);
formattedSize /= divider;
return [formattedSize.toFixed(roundingPrecision), units[i]];
};
exports.formatSize = formatSize;
const prettyActionPlan = (ops) => ops.map(op => (op.type === "install" ? "+" : "-") + op.name).join(", ");
exports.prettyActionPlan = prettyActionPlan;
const prettyInstalled = (items) => items.map(({ name, updated }) => name + (updated ? "" : " (outdated)")).join(", ");
exports.prettyInstalled = prettyInstalled;
//# sourceMappingURL=formatting.js.map