UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

23 lines (22 loc) 714 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Format bytes to human readable format * @param fn - target function * @signature * P.formatBytes(bytes) * @example * P.formatBytes(12457150) // => 11.88MB * @category Utility, Pipe */ function formatBytes(bytes, decimals) { if (decimals === void 0) { decimals = 2; } if (bytes === 0) return '0 Bytes'; var k = 1024; var dm = decimals < 0 ? 0 : decimals; var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; var i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i]; } exports.formatBytes = formatBytes;