All files bytes.ts

88.89% Statements 8/9
50% Branches 3/6
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                    40x 20x 20x 20x 20x 20x 20x    
 
/**
 * Format bytes to human readable format
 * @param fn - target function
 * @signature
 *    P.formatBytes(bytes)
 * @example
 *    P.formatBytes(12457150) // => 11.88MB
 * @category Utility, Pipe
 */
export function formatBytes(bytes: number, Edecimals = 2): string {
    Iif (bytes === 0) return '0 Bytes'
    const k = 1024
    const dm = decimals < 0 ? 0 : decimals
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i]
}