<commit_before>#!/usr/bin/env node

var check = require('check-types');
var q = require('q');
var moment = require('moment');
var registry = require('npm-stats')();
var print = require('./src/print');
var sprintf = require('sprintf-js').sprintf;

var updateNotifier = require('update-notifier');
var notifier = updateNotifier();
if (notifier.update) {
  notifier.notify();
}

var username = process.argv[2];
if (!check.unemptyString(username)) {
  var pkg = require('./package.json');
  var info = pkg.name + ' - ' + pkg.description + '\n' +
    '  version: ' + pkg.version + '\n' +
    '  author: ' + JSON.stringify(pkg.author);
  console.log(info);
  console.log('need username:', pkg.name, '<npm username>');
  process.exit(-1);
}

var today = moment();
var monthAgo = today.subtract('months', 1);
var user = registry.user(username);
var list = q.nbind(user.list, user);

// name -> number of downloads
var counts = {};

list().then(function (data) {
  check.verify.array(data, 'expected data to be an array');
  console.log('user', username, 'has', data.length, 'registered modules');
  if (!data.length) {
    return;
  }
  // console.log(data);

  // data = data.slice(0, 5);
  var downloadFns = data.map(function (name) {
    var moduleName = registry.module(name);
    var downloads = q.nbind(moduleName.downloads, moduleName);

    return downloads;
  });

  var result = q();
  downloadFns.forEach(function (downloadFn, index) {
    var name = data[index];
    result = result.then(downloadFn).then(function (stats) {
      check.verify.array(stats, 'expected stats for ' + name + ' to be an array');
      stats = stats.filter(function (dateCount) {
        var date = moment(dateCount.date, 'YYYY-MM-DD');
        return date.isAfter(monthAgo);
      });

      var count = stats.reduce(function (sum, dateCount) {
        return sum + dateCount.value;
      }, 0);
      // console.log('stats', name, 'downloads', count);
      process.stdout.write(sprintf('stats %25s downloads %5d\r', name, count));
      counts[name] = count;
    });
  });

  result.done(function () {
    print(counts);
  });

}).catch(function (err) {
  console.error(err);
  process.exit(-2);
});<commit_msg>resetting line after \r<commit_after>#!/usr/bin/env node

var check = require('check-types');
var q = require('q');
var moment = require('moment');
var registry = require('npm-stats')();
var print = require('./src/print');
var sprintf = require('sprintf-js').sprintf;

var updateNotifier = require('update-notifier');
var notifier = updateNotifier();
if (notifier.update) {
  notifier.notify();
}

var username = process.argv[2];
if (!check.unemptyString(username)) {
  var pkg = require('./package.json');
  var info = pkg.name + ' - ' + pkg.description + '\n' +
    '  version: ' + pkg.version + '\n' +
    '  author: ' + JSON.stringify(pkg.author);
  console.log(info);
  console.log('need username:', pkg.name, '<npm username>');
  process.exit(-1);
}

var today = moment();
var monthAgo = today.subtract('months', 1);
var user = registry.user(username);
var list = q.nbind(user.list, user);

// name -> number of downloads
var counts = {};

list().then(function (data) {
  check.verify.array(data, 'expected data to be an array');
  console.log('user', username, 'has', data.length, 'registered modules');
  if (!data.length) {
    return;
  }
  // console.log(data);

  // data = data.slice(0, 5);
  var downloadFns = data.map(function (name) {
    var moduleName = registry.module(name);
    var downloads = q.nbind(moduleName.downloads, moduleName);

    return downloads;
  });

  var result = q();
  downloadFns.forEach(function (downloadFn, index) {
    var name = data[index];
    result = result.then(downloadFn).then(function (stats) {
      check.verify.array(stats, 'expected stats for ' + name + ' to be an array');
      stats = stats.filter(function (dateCount) {
        var date = moment(dateCount.date, 'YYYY-MM-DD');
        return date.isAfter(monthAgo);
      });

      var count = stats.reduce(function (sum, dateCount) {
        return sum + dateCount.value;
      }, 0);
      // console.log('stats', name, 'downloads', count);
      process.stdout.write(sprintf('stats %25s downloads %5d\r', name, count));
      counts[name] = count;
    });
  });

  result.done(function () {
    console.log(); // after process.stdout.write \r
    print(counts);
  });

}).catch(function (err) {
  console.error(err);
  process.exit(-2);
});
{'fraction_non_alphanumeric': 0.1792452830188679, 'fraction_numerical': 0.004492362982929021, 'mean_word_length': 3.497979797979798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3642076', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1436, 'n_words': 339}