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 };