Files
flood/client/source/scripts/util/formatData.js
2015-11-21 14:27:32 -08:00

48 lines
1021 B
JavaScript

const format = {
data: function(bytes, extraUnits, precision) {
precision = precision || 2;
let kilobyte = 1024,
megabyte = kilobyte * 1024,
gigabyte = megabyte * 1024,
terabyte = gigabyte * 1024,
value = '',
unit = '';
if ((bytes >= 0) && (bytes < kilobyte)) {
value = bytes;
unit = 'B';
} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
value = (bytes / kilobyte).toFixed(precision);
unit = 'kB';
} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
value = (bytes / megabyte).toFixed(precision);
unit = 'MB';
} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
value = (bytes / gigabyte).toFixed(precision);
unit = 'GB';
} else if (bytes >= terabyte) {
value = (bytes / terabyte).toFixed(precision);
unit = 'TB';
} else {
value = bytes;
unit = 'B';
}
if (extraUnits) {
unit += extraUnits;
}
return {
'value': value,
'unit': unit
};
}
};
export default format;