Files
redux-scraper/app/javascript/bundles/Main/utils/byteCountToHumanSize.ts
Dylan Knutson 127dd9be51 Add Bluesky file display components and utilities
- Add SkySection component for displaying Bluesky-specific file information
- Add byteCountToHumanSize utility for formatting file sizes
- Update PostFiles, FileCarousel, FileDetails, and DisplayedFile components
- Enhance posts helper with file display logic
- Update post model and view templates
- Remove deprecated file details sky section partial
2025-08-12 18:14:13 +00:00

24 lines
696 B
TypeScript

/**
* Converts a byte count to a human-readable size string.
*
* @param bytes - The number of bytes to convert
* @param decimals - Number of decimal places to show (default: 1)
* @returns A human-readable size string (e.g., "1.2 KB", "3.4 MB")
*/
export function byteCountToHumanSize(
bytes: number,
decimals: number = 1,
): string {
if (bytes === 0) return '0 B';
if (bytes < 0) return '0 B';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
const size = parseFloat((bytes / Math.pow(k, i)).toFixed(dm));
return `${size} ${sizes[i]}`;
}