<commit_before>'use strict';

var Promise = require('pinkie-promise');
var objectAssign = require('object-assign');
var openUrl = require('openurl').open;
var blessed = require('blessed');
var spinner = require('./spinner');
var Renderer = require('./renderer');
var renderer = new Renderer({
  onTableSelect: onTableSelect
});
var cache = {};
var options = {
  limit: 5,
  fetch: {
    json: true,
    headers: {
      'user-agent': 'https://github.com/rafaelrinaldi/hn-cli'
    }
  }
};
var api = require('./api')(options.fetch);

function limitResults(results, limit) {
  return results.slice(0, limit || 30);
}

function fetchTopStories() {
  spinner.start('Fetching top stories');
  return api.fetch(api.stories());
}

function parseTopStories(stories) {
  spinner.start('Loading top stories details');

  return Promise.all(
      stories.map(function(id) {
        return fetchStory(id);
      })
    );
}

function fetchStory(id) {
  return api.fetch(api.story(id));
}

function refresh() {
  return fetchTopStories()
    .then(function(response) {
      return limitResults(response.body, options.limit);
    })
    .then(function(response) {
      return parseTopStories(response);
    })
    .then(function(response) {
      return response.map(function(item) {
        return item.body;
      })
    })
    .then(function(response) {
      // Store data to the cache
      cache = objectAssign(cache, response);

      spinner.stop();

      return [[
        'Title',
        'Score',
        'Comments',
        'Author'
      ]].concat(
        response.map(function(item) {
          return [
            String(item.title),
            String(item.score),
            String(item.descendants),
            String(item.by)
          ];
        })
      );
    });
}

function onTableSelect(index) {
  var selected = cache[index - 1];
  openUrl(selected.url);
}

function render(data) {
  renderer.render(data);
}<commit_msg>refactor(readability): better naming<commit_after>'use strict';

var Promise = require('pinkie-promise');
var objectAssign = require('object-assign');
var openUrl = require('openurl').open;
var blessed = require('blessed');
var spinner = require('./spinner');
var Renderer = require('./renderer');
var renderer = new Renderer({
  onTableSelect: onTableSelect
});
var cache = {};
var options = {
  limit: 5,
  fetch: {
    json: true,
    headers: {
      'user-agent': 'https://github.com/rafaelrinaldi/hn-cli'
    }
  }
};
var api = require('./api')(options.fetch);

function limitResults(results, limit) {
  return results.slice(0, limit || 30);
}

function fetchTopStories() {
  spinner.start('Fetching top stories');
  return api.fetch(api.stories());
}

function fetchTopStoriesDetails(stories) {
  spinner.start('Loading top stories details');

  return Promise.all(
      stories.map(function(id) {
        return api.fetch(api.story(id));
      })
    );
}

function refresh() {
  return fetchTopStories()
    // Limit results before requests are fired
    .then(function(response) {
      return limitResults(response.body, options.limit);
    })
    // Fires all requests to top stories
    .then(function(response) {
      return fetchTopStoriesDetails(response);
    })
    // Returns a formatted array with the request response
    .then(function(response) {
      return response.map(function(item) {
        return item.body;
      })
    })
    .then(function(response) {
      // Finally loaded
      spinner.stop();

      // Store data to the cache so it can be used later
      return cache = response;
    })
    // Format the result to a data format compatible with the table widget
    .then(function(response) {
      return [[
        'Title',
        'Score',
        'Comments',
        'Author'
      ]].concat(
        response.map(function(item) {
          return [
            String(item.title),
            String(item.score),
            String(item.descendants),
            String(item.by)
          ];
        })
      );
    });
}

function onTableSelect(index) {
  var selected = cache[index - 1];
  openUrl(selected.url);
}

function render(data) {
  renderer.render(data);
}
{'fraction_non_alphanumeric': 0.1424446583253128, 'fraction_numerical': 0.002406159769008662, 'mean_word_length': 2.7315978456014363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1817900', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1248, 'n_words': 275}