github.com/manicqin/nomad@v0.9.5/ui/app/helpers/format-bytes.js (about)

     1  import Helper from '@ember/component/helper';
     2  
     3  const UNITS = ['Bytes', 'KiB', 'MiB'];
     4  
     5  /**
     6   * Bytes Formatter
     7   *
     8   * Usage: {{format-bytes bytes}}
     9   *
    10   * Outputs the bytes reduced to the largest supported unit size for which
    11   * bytes is larger than one.
    12   */
    13  export function formatBytes([bytes]) {
    14    bytes || (bytes = 0);
    15    let unitIndex = 0;
    16    while (bytes >= 1024 && unitIndex < UNITS.length - 1) {
    17      bytes /= 1024;
    18      unitIndex++;
    19    }
    20  
    21    return `${Math.floor(bytes)} ${UNITS[unitIndex]}`;
    22  }
    23  
    24  export default Helper.helper(formatBytes);