File "SizeStructure.js"

Full Path: /home/analogde/www/FormData/js/SizeStructure.js
File size: 759 bytes
MIME-type: text/plain
Charset: utf-8

var SizeStructure = function()
{
	this.SizeRange = ["Bits", "KB", "MB", "GB", "TB"];
	this.SpeedRange = ["Bit/s", "KB/s", "MB/s", "GB/s", "TB/s"];

	//Returns number of bytes as structure string E.g. 1076 as input returns 1.05KB  

	this.BytesToStructuredString = function(bytes)
	{
		if(bytes < 1)
			return "0 Bit";

		var i = Math.floor(Math.log(bytes) / Math.log(1024));
		return (bytes / Math.pow(1024, i)).toFixed(2) + this.SizeRange[i];
	};
	
	//Returns number of bytes/sec as structure string E.g. 1076 as input returns 1.05KB/s

	this.SpeedToStructuredString = function(speed)
	{
		if(speed < 1)
		return "0 Bit/s";

		var i = Math.floor(Math.log(speed) / Math.log(1024));
		return (speed / Math.pow(1024, i)).toFixed(2) + this.SpeedRange[i];

	};

}