feat: add components
This commit is contained in:
17
components/component-bytes.js
Normal file
17
components/component-bytes.js
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
var UNITS = ['bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
var UNITS_BYTES = [0, Math.pow(1024, 1), Math.pow(1024, 2), Math.pow(1024, 3), Math.pow(1024, 4)];
|
||||
|
||||
exports.showBytes = (bytes) => {
|
||||
for (var i = 0; i < (UNITS.length - 1); i++) {
|
||||
if ((bytes >= UNITS_BYTES[i]) && bytes < UNITS_BYTES[i + 1]) {
|
||||
if (i == 0) {
|
||||
return bytes + ((bytes <= 1) ? ' byte' : ' bytes');
|
||||
} else {
|
||||
return (parseInt(bytes * 100 / UNITS_BYTES[i]) / 100) + ' ' + UNITS[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (parseInt(bytes * 100 / UNITS_BYTES[i]) / 100) + ' ' + UNITS[i]; // i == UNITS.length - 1
|
||||
};
|
||||
Reference in New Issue
Block a user