all files / modules/utils/ formatByteSize.js

37.5% Statements 3/8
0% Branches 0/4
0% Functions 0/1
37.5% Lines 3/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15                        
const SUFFIXES = ["B", "K", "M", "G", "T"];
 
function formatByteSize(size) {
    const tier = size > 0 ? Math.floor(Math.log(size) / Math.log(1024)) : 0;
    let n = size / Math.pow(1024, tier);
 
    if (tier > 0) {
        n = Math.floor(n * 10) / 10; // Preserve only 1 digit after decimal.
    }
 
    return String(n) + SUFFIXES[tier];
}
 
module.exports = formatByteSize;