2017-12-15 21:39:18 +00:00
|
|
|
import Helper from '@ember/component/helper';
|
2017-10-17 01:45:07 +00:00
|
|
|
|
2020-09-11 06:29:07 +00:00
|
|
|
const UNITS = ['Bytes', 'KiB', 'MiB', 'GiB'];
|
2017-10-17 01:45:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bytes Formatter
|
|
|
|
*
|
|
|
|
* Usage: {{format-bytes bytes}}
|
|
|
|
*
|
|
|
|
* Outputs the bytes reduced to the largest supported unit size for which
|
|
|
|
* bytes is larger than one.
|
|
|
|
*/
|
2020-09-11 06:29:07 +00:00
|
|
|
export function reduceToLargestUnit(bytes) {
|
2017-10-20 00:14:55 +00:00
|
|
|
bytes || (bytes = 0);
|
2017-10-17 01:45:07 +00:00
|
|
|
let unitIndex = 0;
|
2017-10-17 17:17:07 +00:00
|
|
|
while (bytes >= 1024 && unitIndex < UNITS.length - 1) {
|
2017-10-17 01:45:07 +00:00
|
|
|
bytes /= 1024;
|
|
|
|
unitIndex++;
|
|
|
|
}
|
|
|
|
|
2020-09-11 06:29:07 +00:00
|
|
|
return [bytes, UNITS[unitIndex]];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatBytes([bytes]) {
|
|
|
|
const [number, unit] = reduceToLargestUnit(bytes);
|
|
|
|
return `${Math.floor(number)} ${unit}`;
|
2017-10-17 01:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Helper.helper(formatBytes);
|