<commit_before>import _ from 'lodash';
import moment from 'moment';

import {packages, setPackages} from '../packages/packages.js'
import isScopedPackageName from 'is-scoped';

export default (function () {
  const DATE_FORMAT = 'YYYY-MM-DD';
  const end = moment().format(DATE_FORMAT);
  const start = moment().subtract(1, 'year').format('YYYY-MM-DD');

  function processDownloads(json) {
    return [for (module of (json.package ? [json] : _.values(json)))
      {
        name: module.package,
        // replace '-' with '/' to fix problem with ES5 coercing it to UTC
        downloads: module.downloads
          .map(entry => ({day: new Date(entry.day.replace(/\-/g, '/')), count: entry.downloads}))
      }];
  }

  async function fetchPackages(...packageNames) {
    const url = `https://api.npmjs.org/downloads/range/${start}:${end}/${packageNames.join()}`;
    const response = await fetch(url);
    const json = await response.json();
    return processDownloads(json);
  }

  async function batchFetchPackages(...packageNames) {
    const { scopedPackageNames, nonScopedPackageNames } = _.groupBy(packageNames, name => isScopedPackageName(name) ? 'scopedPackageNames' : 'nonScopedPackageNames');
    const [ scopedStats, nonScopedStats ] = [
      await Promise.all(scopedPackageNames.map((packageName) => fetchPackages(packageName))),
      await (nonScopedPackageNames && fetchPackages(...nonScopedPackageNames))
    ];
    return _.flatten(_.compact(_.concat(scopedStats, nonScopedStats)));
  }

  let modules = {};
<commit_msg>🐛 handle when no scoped package names present<commit_after>import _ from 'lodash';
import moment from 'moment';

import {packages, setPackages} from '../packages/packages.js'
import isScopedPackageName from 'is-scoped';

export default (function () {
  const DATE_FORMAT = 'YYYY-MM-DD';
  const end = moment().format(DATE_FORMAT);
  const start = moment().subtract(1, 'year').format('YYYY-MM-DD');

  function processDownloads(json) {
    return [for (module of (json.package ? [json] : _.values(json)))
      {
        name: module.package,
        // replace '-' with '/' to fix problem with ES5 coercing it to UTC
        downloads: module.downloads
          .map(entry => ({day: new Date(entry.day.replace(/\-/g, '/')), count: entry.downloads}))
      }];
  }

  async function fetchPackages(...packageNames) {
    const url = `https://api.npmjs.org/downloads/range/${start}:${end}/${packageNames.join()}`;
    const response = await fetch(url);
    const json = await response.json();
    return processDownloads(json);
  }

  async function batchFetchPackages(...packageNames) {
    const { scopedPackageNames, nonScopedPackageNames } = _.groupBy(packageNames, name => isScopedPackageName(name) ? 'scopedPackageNames' : 'nonScopedPackageNames');
    const [ scopedStats, nonScopedStats ] = [
      await (scopedPackageNames && Promise.all(scopedPackageNames.map((packageName) => fetchPackages(packageName)))),
      await (nonScopedPackageNames && fetchPackages(...nonScopedPackageNames))
    ];
    return _.flatten(_.compact(_.concat(scopedStats, nonScopedStats)));
  }

  let modules = {};

{'fraction_non_alphanumeric': 0.14980916030534353, 'fraction_numerical': 0.001272264631043257, 'mean_word_length': 3.9294670846394983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6580981', 'n_tokens_mistral': 964, 'n_tokens_neox': 950, 'n_words': 208}