<commit_before>(function () {
  'use strict';

  var moment = require('moment');
  var RatesUpdater = require('./RatesUpdater.js');

  // Set up updater object
  var updater = new RatesUpdater({
    onUpdate: onRatesUpdated,
    onError: function(err) {
      console.log('Error: ' + err);
    }
  });

  // Initialize ui
  chrome.browserAction.setBadgeBackgroundColor({color: [50, 140, 50, 255]});
  updateIcon();

  // Listen to update rates requests
  chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.name === 'rates:update') {
      updater.checkRates({force: true});
    }
  });


  /**
   * Fetch current rates from dolar-blue API.
   * Triggers chrome message 'rates:updated'.
   *
   * @param  {Object} rates New rates data
   * @return {undefined}
   */
  function onRatesUpdated(data) {
    // Update icon with new value
    updateIcon(data.rates.oficial.sell, data.rates.date);

    // Save rates and notify
    data.rates.date = data.rates.date.toISOString();
    chrome.storage.local.set({'rates': data.rates}, function() {
      chrome.runtime.sendMessage({event: 'rates:updated'}, function() {});
    });

    //Get accepts as a param an object which defines a default value
    chrome.storage.local.get({'historic': []}, function(result) {
      result.historic.push({
        date: moment(data.rates.date).format('DD/MM/YY'),
        sell: data.rates.oficial.sell,
        buy: data.rates.oficial.buy
      });
      chrome.storage.local.set({'historic': result.historic}, function() {});
    });
  }

  /**
   * Update browser icon.
   * If no value is passed, shows temporary text.
   *
   * @param  {String} value
   * @param  {Date} date
   * @return {undefined}<commit_msg>Don't save historic when data hasn't changed<commit_after>(function () {
  'use strict';

  var moment = require('moment');
  var RatesUpdater = require('./RatesUpdater.js');

  // Set up updater object
  var updater = new RatesUpdater({
    onUpdate: onRatesUpdated,
    onError: function(err) {
      console.log('Error: ' + err);
    }
  });

  // Initialize ui
  chrome.browserAction.setBadgeBackgroundColor({color: [50, 140, 50, 255]});
  updateIcon();

  // Listen to update rates requests
  chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.name === 'rates:update') {
      updater.checkRates({force: true});
    }
  });


  /**
   * Fetch current rates from dolar-blue API.
   * Triggers chrome message 'rates:updated'.
   *
   * @param  {Object} rates New rates data
   * @return {undefined}
   */
  function onRatesUpdated(data) {
    // Update icon with new value
    updateIcon(data.rates.oficial.sell, data.rates.date);

    // Save rates and notify
    data.rates.date = data.rates.date.toISOString();
    chrome.storage.local.set({'rates': data.rates}, function() {
      chrome.runtime.sendMessage({event: 'rates:updated'}, function() {});
    });

    //saves historic
    saveHistoric(data);
  }

  /**
   * Get stored historic data
   * @param  {Function}
   */
  function getHistoric(cb) {
    //Get accepts as a param an object which defines a default value
    chrome.storage.local.get({'historic': []}, function(result) {
      return cb(null, result.historic);
    });
  }

  /**
   * Returns whether or not data has changed and thus should be saved
   * @param  {Array} Historic data
   * @param  {Object} Current data
   */
  function shouldSaveHistoric(historic, data) {
    return  historic.length === 0 ||
            historic[historic.length - 1].sell != data.rates.oficial.sell ||
            historic[historic.length - 1].buy != data.rates.oficial.buy;
  }

  /**
   * Saves, if the condition is met, the new data into the historic
   * @param  {Object} Current data
   */
  function saveHistoric(data) {
    getHistoric(function(err, historic) {
      if(shouldSaveHistoric(historic, data)) {
        historic.push({
          date: moment(data.rates.date).format('DD/MM/YY'),
          sell: data.rates.oficial.sell,
          buy: data.rates.oficial.buy
        });
        chrome.storage.local.set({'historic': historic}, function() {});
      }
    });
  }

  /**
   * Update browser icon.
   * If no value is passed, shows temporary text.
   *
   * @param  {String} value
   * @param  {Date} date
   * @return {undefined}
{'fraction_non_alphanumeric': 0.14614499424626007, 'fraction_numerical': 0.005293440736478711, 'mean_word_length': 2.876895628902765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '2113201', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1329, 'n_words': 325}