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