open-nomad/ui/app/helpers/format-bytes.js
2020-10-15 02:54:13 -07:00

30 lines
671 B
JavaScript

import Helper from '@ember/component/helper';
const UNITS = ['Bytes', 'KiB', 'MiB', 'GiB'];
/**
* Bytes Formatter
*
* Usage: {{format-bytes bytes}}
*
* Outputs the bytes reduced to the largest supported unit size for which
* bytes is larger than one.
*/
export function reduceToLargestUnit(bytes) {
bytes || (bytes = 0);
let unitIndex = 0;
while (bytes >= 1024 && unitIndex < UNITS.length - 1) {
bytes /= 1024;
unitIndex++;
}
return [bytes, UNITS[unitIndex]];
}
export function formatBytes([bytes]) {
const [number, unit] = reduceToLargestUnit(bytes);
return `${Math.floor(number)} ${unit}`;
}
export default Helper.helper(formatBytes);