UNPKG

util-helpers

Version:

一个基于业务场景的工具方法库

18 lines (14 loc) 737 B
'use strict'; var ut2 = require('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 || ut2.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 + ''; } module.exports = bytesToSize;