UNPKG

@transloadit/prettier-bytes

Version:

> Even though this module is publicly accessible, we do not recommend using it in projects outside of [Transloadit](https://transloadit.com). We won't make any guarantees about its workings and can change things at any time, we won't adhere strictly to Se

24 lines 882 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prettierBytes = prettierBytes; const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; function prettierBytes(input, unit) { if (typeof input !== 'number' || Number.isNaN(input)) { throw new TypeError(`Expected a number, got ${typeof input}`); } const neg = input < 0; let num = Math.abs(input); if (neg) { num = -num; } if (num === 0) { return '0 B'; } const exponent = unit ? units.indexOf(unit) : Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1); const value = Number(num / 1024 ** exponent); const displayUnit = units[exponent]; return `${value >= 10 || value % 1 === 0 ? Math.round(value) : value.toFixed(1)} ${displayUnit}`; } //# sourceMappingURL=prettierBytes.js.map