{"size": 1022, "ext": "js", "max_stars_repo_path": "src/utils/formatting.js", "max_stars_repo_name": "artivilla/lionshare-desktop", "max_stars_repo_stars_event_min_datetime": "2017-01-24T18:15:38.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T02:52:58.000Z", "max_issues_repo_path": "src/utils/formatting.js", "max_issues_repo_name": "artivilla/lionshare-desktop", "max_issues_repo_issues_event_min_datetime": "2017-01-22T20:20:01.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-06T18:54:02.000Z", "max_forks_repo_path": "src/utils/formatting.js", "max_forks_repo_name": "artivilla/lionshare-desktop", "max_forks_repo_forks_event_min_datetime": "2017-01-22T19:54:48.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T02:54:56.000Z"}
{"max_stars_count": 662, "max_issues_count": 81, "max_forks_count": 122, "avg_line_length": 24.9268292683, "max_line_length": 80, "alphanum_fraction": 0.5949119374}
import numeral from 'numeral';

const formatNumber = (amount, currency, options = {}) => {
  if (!(options.maximumFractionDigits || options.maximumFractionDigits === 0)) {
    options.maximumFractionDigits = Math.max(
      0,
      7 - parseInt(amount, 10).toString().length
    );
  }

  let value = new Intl.NumberFormat(navigator.language, {
    style: 'decimal',
    ...options,
  }).format(Math.abs(amount));

  if (currency) {
    value = `$${value}`;
  }

  if (options.directionSymbol) {
    const direction = amount >= 0.0 ? '+' : '-';
    value = `${direction}${value}`;
  }

  if (options.minPrecision) {
    // Set min precision
    value = value.replace(/\d+(?:,\d+)*(?:\.\d+)?/, match => {
      const matchValue = parseFloat(match.replace(/,/g, ''));
      if (matchValue >= 0.1 || options.directionSymbol) {
        match = numeral(matchValue).format('0,0.00');
      }
      return match;
    });
  }

  return value;
};

// eslint-disable-next-line import/prefer-default-export
export { formatNumber };
