Improve bytes formatting.

Also may or may not perform a bit better. Most likely the former.
This commit is contained in:
Nils Maier 2014-02-23 22:59:26 +01:00
parent 09120284c5
commit 0f4ddbd155

View File

@ -1,40 +1,47 @@
angular (function() {
.module('webui.filters.bytes', []) function fmtlen(len) {
.filter('changeLength', function() { len = +len; // coerce to number
return function(len, pref) { if (len <= 1024) {
len = parseFloat(len); return len.toFixed(0) + " B";
if (len <= (1<<10)) return len.toFixed(1) + " " + pref; }
else if(len <= (1<<20)) return (len/(1<<10)).toFixed(1) + " K" + pref; len /= 1024;
else if(len <= (1<<30)) return (len/(1<<20)).toFixed(1) + " M" + pref; if (len <= 1024) {
else return (len/(1<<30)).toFixed(1) + " G" + pref; return len.toFixed(1) + " KB"
}; }
}) len /= 1024;
.filter('blength', ['$filter', function(filter) { if (len <= 1024) {
return function(len) { return len.toFixed(2) + " MB";
return filter('changeLength')(len, 'B'); }
}; len /= 1024;
}]) return len.toFixed(3) + " GB";
.filter('bspeed', ['$filter', function(filter) {
return function(speed) {
return filter('changeLength')(speed, 'B/s');
};
}])
.filter('time', function() {
function pad(f) {
return ("0" + f).substr(-2);
} }
return function(time) { angular .module('webui.filters.bytes', [])
time = parseInt(time, 10); .filter('blength', ['$filter', function(filter) {
if (!time || !isFinite(time)) return "∞"; return fmtlen;
var secs = time % 60; }])
if (time < 60) return secs + "s"; .filter('bspeed', ['$filter', function(filter) {
var mins = Math.floor((time % 3600) / 60) return function(speed) {
if (time < 3600) return pad(mins) + ":" + pad(secs); return fmtlen(speed) + "/s";
var hrs = Math.floor((time % 86400) / 3600); };
if (time < 86400) return pad(hrs) + ":" + pad(mins) + ":" + pad(secs); }])
var days = Math.floor(time / 86400); .filter('time', function() {
return days + "::" + pad(hrs) + ":" + pad(mins) + ":" + pad(secs); function pad(f) {
}; return ("0" + f).substr(-2);
}); }
return function(time) {
time = parseInt(time, 10);
if (!time || !isFinite(time)) return "∞";
var secs = time % 60;
if (time < 60) return secs + "s";
var mins = Math.floor((time % 3600) / 60)
if (time < 3600) return pad(mins) + ":" + pad(secs);
var hrs = Math.floor((time % 86400) / 3600);
if (time < 86400) return pad(hrs) + ":" + pad(mins) + ":" + pad(secs);
var days = Math.floor(time / 86400);
return days + "::" + pad(hrs) + ":" + pad(mins) + ":" + pad(secs);
};
});
})();