PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 All 32 releases
image-optimization / modules / optimization / assets / js / utils / index.js

index.js in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.6, at modules/optimization/assets/js/utils/index.js

26 lines 922 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __, sprintf } from '@wordpress/i18n';
2
3 export const formatFileSize = ( fileSizeInBytes, decimals = 2 ) => {
4 const sizes = [
5 // translators: %s: file size in bytes
6 __( '%s Bytes', 'image-optimization' ),
7 // translators: %s: file size in kilobytes
8 __( '%s Kb', 'image-optimization' ),
9 // translators: %s: file size in megabytes
10 __( '%s Mb', 'image-optimization' ),
11 // translators: %s: file size in gigabytes
12 __( '%s Gb', 'image-optimization' ),
13 ];
14
15 if ( ! fileSizeInBytes ) {
16 // translators: %s: file size in bytes
17 return sprintf( __( '%s Bytes', 'image-optimization' ), 0 );
18 }
19
20 const currentScale = Math.floor( Math.log( fileSizeInBytes ) / Math.log( 1024 ) );
21 const formattedValue = parseFloat( ( fileSizeInBytes / Math.pow( 1024, currentScale ) ).toFixed( decimals ) );
22
23 // eslint-disable-next-line @wordpress/valid-sprintf
24 return sprintf( sizes[ currentScale ], formattedValue );
25 };
26