github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/helpers/format-bytes.js (about) 1 import Helper from '@ember/component/helper'; 2 3 const UNITS = ['Bytes', 'KiB', 'MiB', 'GiB']; 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 reduceToLargestUnit(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 [bytes, UNITS[unitIndex]]; 22 } 23 24 export function formatBytes([bytes]) { 25 const [number, unit] = reduceToLargestUnit(bytes); 26 return `${Math.floor(number)} ${unit}`; 27 } 28 29 export default Helper.helper(formatBytes);