From 87117d08de7a03848718444866d2b3779f1dd3fb Mon Sep 17 00:00:00 2001 From: MMP0 Date: Sat, 3 Jun 2017 00:21:08 +0900 Subject: [PATCH] Update humanFileSize function --- public/js/torrents.js | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/public/js/torrents.js b/public/js/torrents.js index ca3e18df..7925e49a 100644 --- a/public/js/torrents.js +++ b/public/js/torrents.js @@ -38,20 +38,8 @@ document.addEventListener("DOMContentLoaded", function() { // if Torrents.CanRef } }) - -// Credits to mpen (StackOverflow) function humanFileSize(bytes, si) { - var thresh = si ? 1000 : 1024; - if(Math.abs(bytes) < thresh) { - return bytes + ' B'; - } - var units = si - ? ['kB','MB','GB','TB','PB','EB','ZB','YB'] - : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']; - var u = -1; - do { - bytes /= thresh; - ++u; - } while(Math.abs(bytes) >= thresh && u < units.length - 1); - return bytes.toFixed(1)+' '+units[u]; -} \ No newline at end of file + var k = si ? 1000 : 1024; + var i = ~~(Math.log(bytes) / Math.log(k)); + return i == 0 ? bytes + " B" : (bytes / Math.pow(k, i)).toFixed(1) + " " + "KMGTPEZY"[i - 1] + (si ? "" : "i") + "B"; +}