angular.module('quasardbGUIApp.filters', []).

filter('bytes', function() {
	return function(bytes, precision) {
		if (bytes==0 || isNaN(parseFloat(bytes)) || !isFinite(bytes)) {
			return '0';
		}
		
		if (typeof precision === 'undefined') {
			precision = 2;
		}
		
		var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB']; 
		var number = Math.floor(Math.log(bytes) / Math.log(1024));
		if (units[number] === 'bytes') {
			precision = 0;
		}
		return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) +  ' ' + units[number];
	};
});