UNPKG

@gravityforms/utils

Version:
39 lines (35 loc) 780 B
/** * @module formatFileSize * @description Converts a file size in bytes into a human-readable string. * * @since 1.0.0 * * @param {number} bytes File size in bytes. * * @return {string} Human-readable file size string. * * @example * import { formatFileSize } from "@gravityforms/utils"; * * formatFileSize(500); * // => "500 Bytes" * * formatFileSize(2048); * // => "2.0 KB" * * formatFileSize(1048576); * // => "1.0 MB" * */ export default function formatFileSize( bytes ) { if ( bytes < 1024 ) { return `${ bytes } Bytes`; } if ( bytes < 1048576 ) { return `${ ( bytes / 1024 ).toFixed( 1 ) } KB`; } if ( bytes < 1073741824 ) { return `${ ( bytes / 1048576 ).toFixed( 1 ) } MB`; } return `${ ( bytes / 1073741824 ).toFixed( 1 ) } GB`; }