This function is simple, it gets a number of bytes and returns its representation in either Bytes, KB, MB, GB and TB.
As simple as it is, I am sure there are other (and perhaps better ways) to write it.
function kmgtbytes (num) {
if (num > 0 ){
if (num < 1024) { return [num, "Bytes"] }
if (num < 1048576) { return [num/1024, "KB"] }
if (num < 1073741824) { return [num/1024/1024, "MB"] }
if (num < 1099511600000) { return [num/1024/1024/1024, "GB"] }
return [num/1024/1024/1024/1024, "TB"]
}
return num
}