util-helpers
Version:
16 lines (13 loc) • 726 B
JavaScript
import { isNaN } from 'ut2';
function bytesToSize(bytes, options) {
if (options === void 0) { options = {}; }
var _a = options.spaceMark, spaceMark = _a === void 0 ? ' ' : _a, _b = options.precision, precision = _b === void 0 ? 2 : _b;
var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
if (numBytes === 0 || isNaN(numBytes))
return "0".concat(spaceMark, "B");
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = Math.floor(Math.log(numBytes) / Math.log(k));
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(precision))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
}
export { bytesToSize as default };