format-size.js 470 B

1234567891011121314151617181920
  1. // used in report-size.yml
  2. export function formatBytes( bytes, decimals = 1 ) {
  3. if ( bytes === 0 ) return '0 B';
  4. const k = 1000;
  5. const dm = decimals < 0 ? 0 : decimals;
  6. const sizes = [ 'B', 'kB', 'MB', 'GB' ];
  7. const i = Math.floor( Math.log( bytes ) / Math.log( k ) );
  8. return parseFloat( ( bytes / Math.pow( k, i ) ).toFixed( dm ) ) + ' ' + sizes[ i ];
  9. }
  10. const n = Number( process.argv[ 2 ] );
  11. const formatted = formatBytes( n );
  12. console.log( formatted );
粤ICP备19079148号