"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var utils = {};

utils.formatSize = function (size, isNeedUnit) {
  size = size || 0;
  var s = parseInt(size, 10);
  var str = '';
  var unit = 'B';

  if (isNaN(s)) {
    throw new Error('formatSize param size invalid');
  }

  var index = 0;
  var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  var unitsLength = units.length;

  var fn = function fn() {
    var c = index + 1;

    if (s < Math.pow(1000, c)) {
      if (c === 1) {
        str = s;
      } else {
        str = Math.round(100 * (s / Math.pow(1024, index))) / 100;
      }

      unit = units[index];
    } else {
      index++;

      if (index < unitsLength - 1) {
        fn();
      } else {
        str = Math.round(100 * (s / Math.pow(1024, index))) / 100;
        unit = units[index];
      }
    }
  };

  fn();
  return isNeedUnit ? {
    s: str,
    unit: unit
  } : str + " " + unit;
};

var _default = utils;
exports["default"] = _default;
module.exports = exports.default;