UNPKG

@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.

44 lines (41 loc) 968 B
import { numberFormat } from './intl.js'; 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 = numberFormat(locale, { minimumFractionDigits: 0, maximumFractionDigits: maximumDecimals }).format(value); return `${formattedValue} ${formattedUnit}`; } export { formatFileSize }; //# sourceMappingURL=format-file-size.js.map