'use strict';

let sizeComparisons = [{ str: 'B', val: 1024 }];
let strs = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
strs.forEach((el, i) => {
  sizeComparisons.push({ str: el, val: sizeComparisons[i].val * 1024 });
});
const convertToDecimal = (num, decimal) => {
  return Math.round(num * Math.pow(10, decimal)) / Math.pow(10, decimal);
};
const isNumber = (value) => {
  return typeof value === 'number';
};
const byteFormat = (bytes, roundToDecimal = 2) => {
  if (isNumber(roundToDecimal) && isFinite(roundToDecimal) && roundToDecimal % 1 === 0 && roundToDecimal >= 0 && isNumber(bytes) && isFinite(bytes)) {
    var i = 0;
    while (i < sizeComparisons.length - 1 && bytes >= sizeComparisons[i].val)
      i++;
    bytes /= i > 0 ? sizeComparisons[i - 1].val : 1;
    return convertToDecimal(bytes, roundToDecimal) + ' ' + sizeComparisons[i].str;
  }
  return 'NaN';
};
const stringContainsJson = (str) => {
  let parsedJson;
  try {
    // attempt to parse JSON object from provided string
    parsedJson = JSON.parse(str);
  }
  catch (err) {
    // not a valid JSON string
    return false;
  }
  return typeof parsedJson === 'object' && parsedJson !== null;
};
const defer = () => {
  let res;
  let rej;
  let promise = new Promise((resolve, reject) => {
    res = resolve;
    rej = reject;
  });
  promise.resolve = res;
  promise.reject = rej;
  return promise;
};

exports.byteFormat = byteFormat;
exports.defer = defer;
exports.stringContainsJson = stringContainsJson;
