HEX
Server: Apache
System: Linux efa57bbe-abb1-400d-2985-3b056fbc2701.secureserver.net 6.1.147-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 24 12:33:32 EDT 2025 x86_64
User: root (0)
PHP: 8.0.30.4
Disabled: NONE
Upload Files
File: //var/www/wp-content/plugins/image-optimization/modules/optimization/assets/js/utils/index.js
import { __, sprintf } from '@wordpress/i18n';

export const formatFileSize = ( fileSizeInBytes, decimals = 2 ) => {
	const sizes = [
		// translators: %s: file size in bytes
		__( '%s Bytes', 'image-optimization' ),
		// translators: %s: file size in kilobytes
		__( '%s Kb', 'image-optimization' ),
		// translators: %s: file size in megabytes
		__( '%s Mb', 'image-optimization' ),
		// translators: %s: file size in gigabytes
		__( '%s Gb', 'image-optimization' ),
	];

	if ( ! fileSizeInBytes ) {
		// translators: %s: file size in bytes
		return sprintf( __( '%s Bytes', 'image-optimization' ), 0 );
	}

	const currentScale = Math.floor( Math.log( fileSizeInBytes ) / Math.log( 1024 ) );
	const formattedValue = parseFloat( ( fileSizeInBytes / Math.pow( 1024, currentScale ) ).toFixed( decimals ) );

	// eslint-disable-next-line @wordpress/valid-sprintf
	return sprintf( sizes[ currentScale ], formattedValue );
};