@liveblocks/react-ui
Version:
A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.
46 lines (42 loc) • 995 B
JavaScript
var intl = require('./intl.cjs');
const BASE = 1e3;
const UNITS = ["B", "KB", "MB", "GB"];
function formatFileSize(bytes, locale) {
if (bytes === 0) {
return `0 ${UNITS[1]}`;
}
let unit;
if (bytes === 0) {
unit = 1;
} else {
unit = Math.max(
1,
Math.min(
Math.floor(Math.log(Math.abs(bytes)) / Math.log(BASE)),
UNITS.length - 1
)
);
}
let value = bytes / BASE ** unit;
let maximumDecimals = 1;
if (unit === 1) {
if (value >= 10) {
maximumDecimals = 0;
}
if (value < 0.1 && value > 0) {
maximumDecimals = 2;
}
if (value < 0.01) {
value = 0.01;
}
}
const formattedUnit = UNITS[unit];
const formattedValue = intl.numberFormat(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: maximumDecimals
}).format(value);
return `${formattedValue} ${formattedUnit}`;
}
exports.formatFileSize = formatFileSize;
//# sourceMappingURL=format-file-size.cjs.map
;