Move formatData util

This commit is contained in:
John Furrow
2015-11-21 14:27:32 -08:00
parent 898705748a
commit 61e23bddb5

View File

@@ -0,0 +1,47 @@
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;